31 | * 将4个byte数字组成的数组合并为一个float数. 32 | *33 | * 34 | * @param arr 35 | * @return 36 | */ 37 | public static float byte4ToFloat(byte[] arr) { 38 | if (arr == null || arr.length != 4) { 39 | throw new IllegalArgumentException("byte数组必须不为空,并且是4位!"); 40 | } 41 | int i = byte4ToInt(arr); 42 | return Float.intBitsToFloat(i); 43 | } 44 | 45 | /** 46 | *
47 | * 将一个float数字转换为4个byte数字组成的数组. 48 | *49 | * 50 | * @param f 51 | * @return 52 | */ 53 | public static byte[] floatToByte4(float f) { 54 | int i = Float.floatToIntBits(f); 55 | return intToByte4(i); 56 | } 57 | 58 | /** 59 | *
60 | * 将八个byte数字组成的数组转换为一个double数字. 61 | *62 | * 63 | * @param arr 64 | * @return 65 | */ 66 | public static double byte8ToDouble(byte[] arr) { 67 | if (arr == null || arr.length != 8) { 68 | throw new IllegalArgumentException("byte数组必须不为空,并且是8位!"); 69 | } 70 | long l = byte8ToLong(arr); 71 | return Double.longBitsToDouble(l); 72 | } 73 | 74 | /** 75 | *
76 | * 将一个double数字转换为8个byte数字组成的数组. 77 | *78 | * 79 | * @param i 80 | * @return 81 | */ 82 | public static byte[] doubleToByte8(double i) { 83 | long j = Double.doubleToLongBits(i); 84 | return longToByte8(j); 85 | } 86 | 87 | /** 88 | *
89 | * 将一个char字符转换为两个byte数字转换为的数组. 90 | *91 | * 92 | * @param c 93 | * @return 94 | */ 95 | public static byte[] charToByte2(char c) { 96 | byte[] arr = new byte[2]; 97 | arr[0] = (byte) (c >> 8); 98 | arr[1] = (byte) (c & 0xff); 99 | return arr; 100 | } 101 | 102 | /** 103 | *
104 | * 将2个byte数字组成的数组转换为一个char字符. 105 | *106 | * 107 | * @param arr 108 | * @return 109 | */ 110 | public static char byte2ToChar(byte[] arr) { 111 | if (arr == null || arr.length != 2) { 112 | throw new IllegalArgumentException("byte数组必须不为空,并且是2位!"); 113 | } 114 | return (char) (((char) (arr[0] << 8)) | ((char) arr[1])); 115 | } 116 | 117 | /** 118 | *
119 | * 将一个16位的short转换为长度为2的8位byte数组. 120 | *121 | * 122 | * @param s 123 | * @return 124 | */ 125 | public static byte[] shortToByte2(Short s) { 126 | byte[] arr = new byte[2]; 127 | arr[0] = (byte) (s >> 8); 128 | arr[1] = (byte) (s & 0xff); 129 | return arr; 130 | } 131 | 132 | /** 133 | *
134 | * 长度为2的8位byte数组转换为一个16位short数字. 135 | *136 | * 137 | * @param arr 138 | * @return 139 | */ 140 | public static short byte2ToShort(byte[] arr) { 141 | if (arr != null && arr.length != 2) { 142 | throw new IllegalArgumentException("byte数组必须不为空,并且是2位!"); 143 | } 144 | return (short) (((short) arr[0] << 8) | ((short) arr[1] & 0xff)); 145 | } 146 | 147 | /** 148 | *
149 | * 将short转换为长度为16的byte数组. 150 | * 实际上每个8位byte只存储了一个0或1的数字 151 | * 比较浪费. 152 | *153 | * 154 | * @param s 155 | * @return 156 | */ 157 | public static byte[] shortToByte16(short s) { 158 | byte[] arr = new byte[16]; 159 | for (int i = 15; i >= 0; i--) { 160 | arr[i] = (byte) (s & 1); 161 | s >>= 1; 162 | } 163 | return arr; 164 | } 165 | 166 | public static short byte16ToShort(byte[] arr) { 167 | if (arr == null || arr.length != 16) { 168 | throw new IllegalArgumentException("byte数组必须不为空,并且长度为16!"); 169 | } 170 | short sum = 0; 171 | for (int i = 0; i < 16; ++i) { 172 | sum |= (arr[i] << (15 - i)); 173 | } 174 | return sum; 175 | } 176 | 177 | /** 178 | *
179 | * 将32位int转换为由四个8位byte数字. 180 | *181 | * 182 | * @param sum 183 | * @return 184 | */ 185 | public static byte[] intToByte4(int sum) { 186 | byte[] arr = new byte[4]; 187 | arr[0] = (byte) (sum >> 24); 188 | arr[1] = (byte) (sum >> 16); 189 | arr[2] = (byte) (sum >> 8); 190 | arr[3] = (byte) (sum & 0xff); 191 | return arr; 192 | } 193 | 194 | /** 195 | *
196 | * 将长度为4的8位byte数组转换为32位int. 197 | *198 | * 199 | * @param arr 200 | * @return 201 | */ 202 | public static int byte4ToInt(byte[] arr) { 203 | if (arr == null || arr.length != 4) { 204 | throw new IllegalArgumentException("byte数组必须不为空,并且是4位!"); 205 | } 206 | return (int) (((arr[0] & 0xff) << 24) | ((arr[1] & 0xff) << 16) | ((arr[2] & 0xff) << 8) | ((arr[3] & 0xff))); 207 | } 208 | 209 | /** 210 | *
211 | * 将长度为8的8位byte数组转换为64位long. 212 | *213 | *
214 | * 0xff对应16进制,f代表1111,0xff刚好是8位 byte[] 215 | * arr,byte[i]&0xff刚好满足一位byte计算,不会导致数据丢失. 如果是int计算. int[] arr,arr[i]&0xffff 216 | * 217 | * @param arr 218 | * @return 219 | */ 220 | public static long byte8ToLong(byte[] arr) { 221 | if (arr == null || arr.length != 8) { 222 | throw new IllegalArgumentException("byte数组必须不为空,并且是8位!"); 223 | } 224 | return (long) (((long) (arr[0] & 0xff) << 56) | ((long) (arr[1] & 0xff) << 48) | ((long) (arr[2] & 0xff) << 40) 225 | | ((long) (arr[3] & 0xff) << 32) | ((long) (arr[4] & 0xff) << 24) | ((long) (arr[5] & 0xff) << 16) 226 | | ((long) (arr[6] & 0xff) << 8) | ((long) (arr[7] & 0xff))); 227 | } 228 | 229 | /** 230 | * 将一个long数字转换为8个byte数组组成的数组. 231 | */ 232 | public static byte[] longToByte8(long sum) { 233 | byte[] arr = new byte[8]; 234 | arr[0] = (byte) (sum >> 56); 235 | arr[1] = (byte) (sum >> 48); 236 | arr[2] = (byte) (sum >> 40); 237 | arr[3] = (byte) (sum >> 32); 238 | arr[4] = (byte) (sum >> 24); 239 | arr[5] = (byte) (sum >> 16); 240 | arr[6] = (byte) (sum >> 8); 241 | arr[7] = (byte) (sum & 0xff); 242 | return arr; 243 | } 244 | 245 | /** 246 | *
247 | * 将int转换为32位byte. 248 | * 实际上每个8位byte只存储了一个0或1的数字 249 | * 比较浪费. 250 | *251 | * 252 | * @param num 253 | * @return 254 | */ 255 | public static byte[] intToByte32(int num) { 256 | byte[] arr = new byte[32]; 257 | for (int i = 31; i >= 0; i--) { 258 | // &1 也可以改为num&0x01,表示取最地位数字. 259 | arr[i] = (byte) (num & 1); 260 | // 右移一位. 261 | num >>= 1; 262 | } 263 | return arr; 264 | } 265 | 266 | /** 267 | *
268 | * 将长度为32的byte数组转换为一个int类型值. 269 | * 每一个8位byte都只存储了0或1的数字. 270 | *271 | * 272 | * @param arr 273 | * @return 274 | */ 275 | public static int byte32ToInt(byte[] arr) { 276 | if (arr == null || arr.length != 32) { 277 | throw new IllegalArgumentException("byte数组必须不为空,并且长度是32!"); 278 | } 279 | int sum = 0; 280 | for (int i = 0; i < 32; ++i) { 281 | sum |= (arr[i] << (31 - i)); 282 | } 283 | return sum; 284 | } 285 | 286 | /** 287 | *
288 | * 将长度为64的byte数组转换为一个long类型值. 289 | * 每一个8位byte都只存储了0或1的数字. 290 | *291 | * 292 | * @param arr 293 | * @return 294 | */ 295 | public static long byte64ToLong(byte[] arr) { 296 | if (arr == null || arr.length != 64) { 297 | throw new IllegalArgumentException("byte数组必须不为空,并且长度是64!"); 298 | } 299 | long sum = 0L; 300 | for (int i = 0; i < 64; ++i) { 301 | sum |= ((long) arr[i] << (63 - i)); 302 | } 303 | return sum; 304 | } 305 | 306 | /** 307 | *
308 | * 将一个long值转换为长度为64的8位byte数组. 309 | * 每一个8位byte都只存储了0或1的数字. 310 | *311 | * 312 | * @param sum 313 | * @return 314 | */ 315 | public static byte[] longToByte64(long sum) { 316 | byte[] arr = new byte[64]; 317 | for (int i = 63; i >= 0; i--) { 318 | arr[i] = (byte) (sum & 1); 319 | sum >>= 1; 320 | } 321 | return arr; 322 | } 323 | 324 | /** 325 | * @param num:要获取二进制值的数 326 | * @param index:倒数第一位为0,依次类推 327 | */ 328 | public static int get(int num, int index) { 329 | return (num & (0x1 << index)) >> index; 330 | } 331 | 332 | } -------------------------------------------------------------------------------- /lib/src/main/java/com/blanke/xsocket/utils/CharsetUtil.java: -------------------------------------------------------------------------------- 1 | package com.blanke.xsocket.utils; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | 5 | public class CharsetUtil { 6 | public static final String UTF_8 = "UTF-8"; 7 | public static final String GBK = "GBK"; 8 | 9 | 10 | public static byte[] stringToData(String string, String charsetName) { 11 | if (string != null) { 12 | try { 13 | return string.getBytes(charsetName); 14 | } catch (UnsupportedEncodingException e) { 15 | e.printStackTrace(); 16 | } 17 | } 18 | return null; 19 | } 20 | 21 | public static String dataToString(byte[] data, String charsetName) { 22 | if (data != null) { 23 | try { 24 | return new String(data, charsetName); 25 | } catch (UnsupportedEncodingException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | return null; 30 | } 31 | } -------------------------------------------------------------------------------- /lib/src/main/java/com/blanke/xsocket/utils/ExceptionUtils.java: -------------------------------------------------------------------------------- 1 | package com.blanke.xsocket.utils; 2 | 3 | /** 4 | */ 5 | public class ExceptionUtils { 6 | private static final String TAG = "XAndroidSocket"; 7 | 8 | public static void throwException(String message) { 9 | throw new IllegalStateException(TAG + " : " + message); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /lib/src/main/java/com/blanke/xsocket/utils/StringValidationUtils.java: -------------------------------------------------------------------------------- 1 | package com.blanke.xsocket.utils; 2 | 3 | /** 4 | * 输入检查 5 | * 正则检查String是否符合某些格式 6 | */ 7 | public class StringValidationUtils { 8 | 9 | public final static String RegexIP = "^(25[0-5]|2[0-4][0-9]|1{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|1{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|1{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|1{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$"; 10 | public final static String RegexPort = "^6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3}$"; 11 | public final static String RegexAllChinese = "^[\\u4e00-\\u9fa5]*$"; 12 | public final static String RegexPhoneNumber = "^(((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8})|((\\d{3,4}-)?\\d{7,8}(-\\d{1,4})?)$"; 13 | public final static String RegexEmail = "w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*"; 14 | 15 | public static boolean validateRegex(String string, String regex) { 16 | if (string == null) { 17 | return false; 18 | } 19 | return string.matches(regex); 20 | } 21 | } -------------------------------------------------------------------------------- /lib/src/main/java/com/blanke/xsocket/utils/XSocketLog.java: -------------------------------------------------------------------------------- 1 | package com.blanke.xsocket.utils; 2 | 3 | import android.util.Log; 4 | 5 | public class XSocketLog { 6 | 7 | public static boolean debug = false; 8 | public static final String TAG = "XSocket"; 9 | 10 | public static void debug(boolean debug1) { 11 | debug = debug1; 12 | } 13 | 14 | public static void d(String tag, Object o) { 15 | if (debug) { 16 | Log.d(TAG, tag + " " + o.toString()); 17 | } 18 | } 19 | 20 | public static void e(String tag, Object o) { 21 | if (debug) { 22 | Log.e(TAG, tag + " " + o.toString()); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /lib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 |