|
@@ -0,0 +1,364 @@
|
|
|
+package com.tsi.app.common.utils;
|
|
|
+
|
|
|
+public class ByteUtils {
|
|
|
+
|
|
|
+ private static final int[] BYTE_MASKED_ARRAY = { 1, 2, 4, 8, 16, 32, 64, 128 };
|
|
|
+
|
|
|
+ public static short getShort(byte[] data, int start) {
|
|
|
+ return (short)(data[start ] << 8 |
|
|
|
+ data[start+1] & 0xFF);
|
|
|
+ }
|
|
|
+ public static short getShortLE(byte[] data, int start) {
|
|
|
+ return (short)(data[start+1] << 8 |
|
|
|
+ data[start ] & 0xFF);
|
|
|
+ }
|
|
|
+ public static int getUnsignedShort(byte[] data, int start) {
|
|
|
+ return data[start] << 8 & 0xFF00 | data[start+1] & 0x00FF;
|
|
|
+ }
|
|
|
+ public static int getUnsignedShortLE(byte[] data, int start) {
|
|
|
+ return data[start+1] << 8 & 0xFF00 | data[start] & 0x00FF;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getInt(byte[] data, int start) {
|
|
|
+ return data[start] << 24 & 0xFF000000 | data[start+1] << 16 & 0x00FF0000 | data[start+2] << 8 & 0x0000FF00 | data[start+3] & 0x000000FF;
|
|
|
+ }
|
|
|
+ public static int getIntLE(byte[] data, int start) {
|
|
|
+ return data[start+3] << 24 & 0xFF000000 | data[start+2] << 16 & 0x00FF0000 | data[start+1] << 8 & 0x0000FF00 | data[start] & 0x000000FF;
|
|
|
+ }
|
|
|
+ public static long getUnsignedInt(byte[] data, int start) {
|
|
|
+ return (long)(data[start] << 24 & 0xFF000000 | data[start+1] << 16 & 0x00FF0000 | data[start+2] << 8 & 0x0000FF00 | data[start+3] & 0x000000FF);
|
|
|
+ }
|
|
|
+ public static long getUnsignedIntLE(byte[] data, int start) {
|
|
|
+ return (long)(data[start+3] << 24 & 0xFF000000 | data[start+2] << 16 & 0x00FF0000 | data[start+1] << 8 & 0x0000FF00 | data[start] & 0x000000FF);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setUnsignedInt(byte[] data, int start, long val) {
|
|
|
+ data[start ] = (byte)((val >> 24) & 0x000000FF);
|
|
|
+ data[start+1] = (byte)((val >> 16) & 0x000000FF);
|
|
|
+ data[start+2] = (byte)((val >> 8 ) & 0x000000FF);
|
|
|
+ data[start+3] = (byte)((val ) & 0x000000FF);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setUnsignedShort(byte[] data, int start, int val) {
|
|
|
+ data[start ] = (byte)((val >> 8 ) & 0x000000FF);
|
|
|
+ data[start+1] = (byte)((val ) & 0x000000FF);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setUnsignedIntLE(byte[] data, int start, long val) {
|
|
|
+ data[start+3] = (byte)((val >> 24) & 0x000000FF);
|
|
|
+ data[start+2] = (byte)((val >> 16) & 0x000000FF);
|
|
|
+ data[start+1] = (byte)((val >> 8 ) & 0x000000FF);
|
|
|
+ data[start ] = (byte)((val ) & 0x000000FF);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setUnsignedShortLE(byte[] data, int start, int val) {
|
|
|
+ data[start+1] = (byte)((val >> 8 ) & 0x000000FF);
|
|
|
+ data[start ] = (byte)((val ) & 0x000000FF);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int byteToInt(byte srcValue)
|
|
|
+ {
|
|
|
+ return srcValue & 0xFF;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int shortToInt(short srcValue)
|
|
|
+ {
|
|
|
+ return srcValue & 0xFFFF;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long intToLong(int srcValue)
|
|
|
+ {
|
|
|
+ return srcValue & 0xFFFFFFFF;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int longToInt(long srcValue)
|
|
|
+ {
|
|
|
+ return (int)(srcValue & 0xFFFFFFFF);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteToBitString(byte srcValue)
|
|
|
+ {
|
|
|
+ return String.format("%8s", Long.toBinaryString(srcValue & 0xFF)).replace(' ', '0');
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String shortToBitString(short srcValue)
|
|
|
+ {
|
|
|
+ return String.format("%16s", Long.toBinaryString(srcValue & 0xFF)).replace(' ', '0');
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String intToBitString(int srcValue)
|
|
|
+ {
|
|
|
+ return String.format("%32s", Integer.toBinaryString(srcValue & 0xFF)).replace(' ', '0');
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String longToBitString(long srcValue)
|
|
|
+ {
|
|
|
+ return String.format("%64s", Long.toBinaryString(srcValue & 0xFF)).replace(' ', '0');
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int extractBits(byte srcValue, int beginBitDigit, int endBitDigit)
|
|
|
+ {
|
|
|
+ int maskedValue = 0;
|
|
|
+ for (int idx = beginBitDigit; idx <= endBitDigit; idx++) {
|
|
|
+ maskedValue += BYTE_MASKED_ARRAY[idx];
|
|
|
+ }
|
|
|
+ return (srcValue & maskedValue) >> beginBitDigit;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int extractBit(byte srcValue, int bitDigit)
|
|
|
+ {
|
|
|
+ return (srcValue & BYTE_MASKED_ARRAY[bitDigit]) == BYTE_MASKED_ARRAY[bitDigit] ? 1 : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int compareMaskedValue(byte srcValue, int maskValue, int compareValue)
|
|
|
+ {
|
|
|
+ int resultValue = byteToInt(srcValue) & maskValue;
|
|
|
+ return resultValue == compareValue ? 0 : resultValue > compareValue ? 1 : -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String convertCustomDateTime(String strDate)
|
|
|
+ {
|
|
|
+ String convertDate = strDate;
|
|
|
+ if (convertDate.lastIndexOf(".") != -1) {
|
|
|
+ convertDate = convertDate.substring(0, convertDate.lastIndexOf("."));
|
|
|
+ }
|
|
|
+ return convertDate.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] hexToByteArray(String hex)
|
|
|
+ {
|
|
|
+ if ((hex == null) || (hex.length() == 0)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ byte[] ba = new byte[hex.length() / 2];
|
|
|
+ for (int i = 0; i < ba.length; i++) {
|
|
|
+ ba[i] = ((byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16));
|
|
|
+ }
|
|
|
+ return ba;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteArrayToHex(byte[] ba)
|
|
|
+ {
|
|
|
+ return byteArrayToHex(ba, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] intToShortBytes(int length)
|
|
|
+ {
|
|
|
+ byte[] b3 = new byte[2];
|
|
|
+ if (length > 255)
|
|
|
+ {
|
|
|
+ b3[0] = ((byte)(length / 256));
|
|
|
+ b3[1] = ((byte)(length % 256));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ b3[0] = 0;
|
|
|
+ b3[1] = ((byte)length);
|
|
|
+ }
|
|
|
+ return b3;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getIntOnBit(int n, int offset, int length)
|
|
|
+ {
|
|
|
+ return n >> 32 - offset - length & (-1 << length ^ 0xFFFFFFFF);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteArrayToHex(byte[] ba, String seperator)
|
|
|
+ {
|
|
|
+ if ((ba == null) || (ba.length == 0)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuffer sb = new StringBuffer(ba.length * 2);
|
|
|
+ for (int x = 0; x < ba.length; x++)
|
|
|
+ {
|
|
|
+ String hexNumber = "0" + Integer.toHexString(0xFF & ba[x]).toUpperCase();
|
|
|
+
|
|
|
+ sb.append(hexNumber.substring(hexNumber.length() - 2));
|
|
|
+ //if (seperator != null && !seperator.isEmpty()) {
|
|
|
+ sb.append(seperator);
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
|
|
|
+ public static final int BIT_1 = 1;
|
|
|
+ public static final int BIT_2 = 3;
|
|
|
+ public static final int BIT_3 = 7;
|
|
|
+ public static final int BIT_4 = 15;
|
|
|
+ public static final int BIT_5 = 31;
|
|
|
+ public static final int BIT_6 = 63;
|
|
|
+ public static final int BIT_7 = 127;
|
|
|
+ public static final int BIT_8 = 255;
|
|
|
+ public static final int BIT_9 = 511;
|
|
|
+ public static final int BIT_10 = 1023;
|
|
|
+ public static final int BIT_11 = 2047;
|
|
|
+ public static final int BIT_12 = 4095;
|
|
|
+ public static final int BIT_13 = 8191;
|
|
|
+ public static final int BIT_14 = 16383;
|
|
|
+ public static final int BIT_15 = 32767;
|
|
|
+ public static final int BIT_16 = 65535;
|
|
|
+
|
|
|
+ public static String bytesToHex(byte[] bytes, int bundleSize, char seperator)
|
|
|
+ {
|
|
|
+ char[] hexChars = new char[bytes.length * 2 + bytes.length / bundleSize];
|
|
|
+ int j = 0;
|
|
|
+ for (int k = 1; j < bytes.length; k++)
|
|
|
+ {
|
|
|
+ int v = bytes[j] & 0xFF;
|
|
|
+ int start = j * 2 + j / bundleSize;
|
|
|
+
|
|
|
+ hexChars[start] = HEX_ARRAY[(v >>> 4)];
|
|
|
+ hexChars[(start + 1)] = HEX_ARRAY[(v & 0xF)];
|
|
|
+ if (k % bundleSize == 0) {
|
|
|
+ hexChars[(start + 2)] = seperator;
|
|
|
+ }
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ return new String(hexChars).trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String bytesToHex(byte[] bytes, int bundleSize)
|
|
|
+ {
|
|
|
+ return bytesToHex(bytes, bundleSize, ' ');
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteToHex(byte b)
|
|
|
+ {
|
|
|
+ String hexNumber = "0" + Integer.toHexString(0xFF & b).toUpperCase();
|
|
|
+ return hexNumber.substring(hexNumber.length() - 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int convertNotNullStringToInt(String data)
|
|
|
+ {
|
|
|
+ if (data == null || data.trim().length() == 0)
|
|
|
+ return 0;
|
|
|
+ return Long.valueOf(data).intValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static double convertNotNullStringToDouble(String data)
|
|
|
+ {
|
|
|
+ if (data == null || data.trim().length() == 0)
|
|
|
+ return 0.0D;
|
|
|
+ return Double.valueOf(data).doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String extractFilePath(String fullPath)
|
|
|
+ {
|
|
|
+ String resultPath = fullPath;
|
|
|
+ int lastPathIndex = resultPath.lastIndexOf("/") != -1 ? resultPath.lastIndexOf("/") : resultPath.lastIndexOf("\\");
|
|
|
+ return lastPathIndex != -1 ? resultPath.substring(0, lastPathIndex + 1) : resultPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String extractFileName(String fullPath)
|
|
|
+ {
|
|
|
+ String resultPath = fullPath;
|
|
|
+ int lastPathIndex = resultPath.lastIndexOf("/") != -1 ? resultPath.lastIndexOf("/") : resultPath.lastIndexOf("\\");
|
|
|
+ return lastPathIndex != -1 ? resultPath.substring(lastPathIndex + 1) : resultPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String lpad(String str, int len, String pad)
|
|
|
+ {
|
|
|
+ String result = str;
|
|
|
+ int templen = len - result.length();
|
|
|
+ for (int i = 0; i < templen; i++) {
|
|
|
+ result = pad + result;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getBitField(byte b, int field)
|
|
|
+ {
|
|
|
+ return (b & 1 << field) > 0 ? 1 : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getBitField(byte b, int field, int count)
|
|
|
+ {
|
|
|
+ if (count == 1) {
|
|
|
+ count = 1;
|
|
|
+ } else if (count == 2) {
|
|
|
+ count = 3;
|
|
|
+ } else if (count == 3) {
|
|
|
+ count = 7;
|
|
|
+ } else if (count == 4) {
|
|
|
+ count = 15;
|
|
|
+ } else if (count == 5) {
|
|
|
+ count = 31;
|
|
|
+ } else if (count == 6) {
|
|
|
+ count = 63;
|
|
|
+ } else if (count == 7) {
|
|
|
+ count = 127;
|
|
|
+ } else if (count == 8) {
|
|
|
+ count = 255;
|
|
|
+ } else if (count == 9) {
|
|
|
+ count = 511;
|
|
|
+ } else if (count == 10) {
|
|
|
+ count = 1023;
|
|
|
+ } else if (count == 11) {
|
|
|
+ count = 2047;
|
|
|
+ } else if (count == 12) {
|
|
|
+ count = 4095;
|
|
|
+ } else if (count == 13) {
|
|
|
+ count = 8191;
|
|
|
+ } else if (count == 14) {
|
|
|
+ count = 16383;
|
|
|
+ } else if (count == 15) {
|
|
|
+ count = 32767;
|
|
|
+ } else if (count == 16) {
|
|
|
+ count = 65535;
|
|
|
+ }
|
|
|
+ return b >> field & count;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte setBitField(byte b, int field, int value)
|
|
|
+ {
|
|
|
+ if ((value == 0) || (value == 1)) {
|
|
|
+ b = (byte)(b | value << field);
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte setBitField(byte b, int field, int count, int value)
|
|
|
+ {
|
|
|
+ if ((field + count <= 8) && (value < Math.pow(2.0D, count))) {
|
|
|
+ if (count == 1) {
|
|
|
+ count = 1;
|
|
|
+ } else if (count == 2) {
|
|
|
+ count = 3;
|
|
|
+ } else if (count == 3) {
|
|
|
+ count = 7;
|
|
|
+ } else if (count == 4) {
|
|
|
+ count = 15;
|
|
|
+ } else if (count == 5) {
|
|
|
+ count = 31;
|
|
|
+ } else if (count == 6) {
|
|
|
+ count = 63;
|
|
|
+ } else if (count == 7) {
|
|
|
+ count = 127;
|
|
|
+ } else if (count == 8) {
|
|
|
+ count = 255;
|
|
|
+ } else if (count == 9) {
|
|
|
+ count = 511;
|
|
|
+ } else if (count == 10) {
|
|
|
+ count = 1023;
|
|
|
+ } else if (count == 11) {
|
|
|
+ count = 2047;
|
|
|
+ } else if (count == 12) {
|
|
|
+ count = 4095;
|
|
|
+ } else if (count == 13) {
|
|
|
+ count = 8191;
|
|
|
+ } else if (count == 14) {
|
|
|
+ count = 16383;
|
|
|
+ } else if (count == 15) {
|
|
|
+ count = 32767;
|
|
|
+ } else if (count == 16) {
|
|
|
+ count = 65535;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ b = (byte)(b & (count << field ^ 0xFFFFFFFF));
|
|
|
+ b = (byte)(b | (value & count) << field);
|
|
|
+
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+}
|