From 14d7f0df63340ede75458c0e91b902376ef4dd64 Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期三, 20 十一月 2024 11:26:32 +0800 Subject: [PATCH] 1、优化代码;2、修改长级测试中发现的bug;3、通信中间件配置文件配置项修改 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java | 339 ++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 208 insertions(+), 131 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java index 7fe22e9..5f05861 100644 --- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java @@ -4,7 +4,50 @@ public class ByteUtil { - + + /** + * 灏嗗瓧鑺傛暟缁勫垎鍓� + * @param bytes 琚垎鍓叉暟缁� + * @param fromIndex 鍒嗗壊璧峰浣嶇疆 + * @param len 鍒嗗壊闀垮害 + * @return 杩斿洖 鍒嗗壊鍑烘潵鐨勬暟缁� + */ + public static byte[] bytesSplit(byte[] bytes, int fromIndex, int len){ + if(bytes == null){ + return null ; + }else{ + byte[] bs = new byte[len] ; + if(fromIndex + len > bytes.length){ + System.arraycopy(bytes, fromIndex, bs, 0, bytes.length - fromIndex) ; + }else{ + System.arraycopy(bytes, fromIndex, bs, 0, len) ; + } + return bs ; + } + } + /** + * 灏嗗瓧鑺傛暟缁勫垎鍓� + * @param bytes 琚垎鍓叉暟缁� + * @param fromIndex 鍒嗗壊璧峰浣嶇疆 + * @param len 鍒嗗壊闀垮害 + * @return 杩斿洖 鍒嗗壊鍑烘潵鐨勬暟缁� + */ + public static Object[] bytesSplit_(byte[] bytes, int fromIndex, int len){ + if(bytes == null){ + return null ; + }else{ + int realLen = len ; + byte[] bs = new byte[len] ; + if(fromIndex + len > bytes.length){ + System.arraycopy(bytes, fromIndex, bs, 0, bytes.length - fromIndex) ; + realLen = bytes.length - fromIndex ; + }else{ + System.arraycopy(bytes, fromIndex, bs, 0, len) ; + } + return new Object[]{bs, realLen} ; + } + } + /** * 灏嗗瓧鑺傛暟缁勫悎骞跺埌瀛楄妭鏁扮粍涓� * @param bGroup1 琚悎骞舵暟缁� @@ -310,7 +353,7 @@ long l = 0; long[] ls = new long[len] ; for(int i = 0 ; i < len; i++){ - ls[i] = bs[i] ; + ls[i] = bs[i] & 0xFF ; } for(int i = len-1 ; i >= 0; i--){ ls[(len - 1) - i] <<= 8 * i ; @@ -334,14 +377,14 @@ boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); if (b) { long s = 0; - long s0 = bs[from + 0] ;// 鏈�浣庝綅 - long s1 = bs[from + 1] ; - long s2 = bs[from + 2] ; - long s3 = bs[from + 3] ; - long s4 = bs[from + 4] ; - long s5 = bs[from + 5] ; - long s6 = bs[from + 6] ; - long s7 = bs[from + 7] ; + long s0 = bs[from + 0] & 0xFF ;// 鏈�浣庝綅 + long s1 = bs[from + 1] & 0xFF ; + long s2 = bs[from + 2] & 0xFF ; + long s3 = bs[from + 3] & 0xFF ; + long s4 = bs[from + 4] & 0xFF ; + long s5 = bs[from + 5] & 0xFF ; + long s6 = bs[from + 6] & 0xFF ; + long s7 = bs[from + 7] & 0xFF ; // s7涓嶅彉 s6 <<= 8; @@ -371,7 +414,7 @@ short len = (short)(end - from + 1) ; long[] ls = new long[len] ; for(short i = 0 ; i < len; i++){ - ls[i] = bs[from + i] ; + ls[i] = bs[from + i] & 0xFF ; } for(short i = (short)(len-1) ; i >= 0; i--){ ls[i] <<= 8 * (len - (i + 1)) ; @@ -400,7 +443,7 @@ long l = 0; long[] ls = new long[len] ; for(int i = 0 ; i < len; i++){ - ls[i] = bs[i] ; + ls[i] = bs[i] & 0xFF ; } for(int i = 0 ; i < len; i++){ ls[(len - 1) - i] <<= 8 * i ; @@ -424,14 +467,14 @@ boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); if (b) { long s = 0; - long s0 = bs[from] ;// 鏈�浣庝綅 - long s1 = bs[from + 1] ; - long s2 = bs[from + 2] ; - long s3 = bs[from + 3] ; - long s4 = bs[from + 4] ; - long s5 = bs[from + 5] ; - long s6 = bs[from + 6] ; - long s7 = bs[from + 7] ; + long s0 = bs[from] & 0xFF ;// 鏈�浣庝綅 + long s1 = bs[from + 1] & 0xFF ; + long s2 = bs[from + 2] & 0xFF ; + long s3 = bs[from + 3] & 0xFF ; + long s4 = bs[from + 4] & 0xFF ; + long s5 = bs[from + 5] & 0xFF ; + long s6 = bs[from + 6] & 0xFF ; + long s7 = bs[from + 7] & 0xFF ; // s0涓嶅彉 s1 <<= 8; @@ -462,7 +505,7 @@ short len = (short)(end - from + 1) ; long[] ls = new long[len] ; for(short i = 0 ; i < len; i++){ - ls[i] = bs[from + i] ; + ls[i] = bs[from + i] & 0xFF ; } for(short i = 0 ; i < len; i++){ ls[i] <<= 8 * i ; @@ -511,9 +554,9 @@ for (int i = 0; i < 4; i++) { bs[from + i] = Integer.valueOf(value & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄤ綆瀛楄妭 value = value >> 8; // 鍚戝彸绉�8浣� - if(value == 0){ - break ; - } + if(value == 0){ + break ; + } } } else { throw new Exception("int2Bytes鏃舵暟缁勮秺鐣�"); @@ -531,10 +574,10 @@ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); if (b) { int s = 0; - int s0 = bs[from + 0] ;// 鏈�浣庝綅 - int s1 = bs[from + 1] ; - int s2 = bs[from + 2] ; - int s3 = bs[from + 3] ; + int s0 = bs[from + 0] & 0xFF ;// 鏈�浣庝綅 + int s1 = bs[from + 1] & 0xFF ; + int s2 = bs[from + 2] & 0xFF ; + int s3 = bs[from + 3] & 0xFF ; // s3涓嶅彉 s2 <<= 8; @@ -558,10 +601,10 @@ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); if (b) { int s = 0; - int s0 = bs[from + 0] ;// 鏈�浣庝綅 - int s1 = bs[from + 1] ; - int s2 = bs[from + 2] ; - int s3 = bs[from + 3] ; + int s0 = bs[from + 0] & 0xFF ;// 鏈�浣庝綅 + int s1 = bs[from + 1] & 0xFF ; + int s2 = bs[from + 2] & 0xFF ; + int s3 = bs[from + 3] & 0xFF ; // s0涓嶅彉 s1 <<= 8; @@ -654,14 +697,13 @@ boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); if (b) { int s = 0; - int s0 = bs[from + 0] ; - int s1 = bs[from + 1] ; + int s0 = bs[from + 0] & 0xFF ; + int s1 = bs[from + 1] & 0xFF ; // s1涓嶅彉 s0 <<= 8; s = s0 | s1; - return (short) s; - + return (short)s ; } else { throw new Exception("byte2Short鏃舵暟缁勮秺鐣�"); } @@ -677,8 +719,8 @@ boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); if (b) { int s = 0; - int s0 = bs[from + 0] ; - int s1 = bs[from + 1] ; + int s0 = bs[from + 0] & 0xFF ; + int s1 = bs[from + 1] & 0xFF ; // s0涓嶅彉 s1 <<= 8; @@ -892,47 +934,107 @@ * @return 杩斿洖 String */ public static String bytes2Hex(byte[] src, boolean hasBlank){ - StringBuilder stringBuilder = new StringBuilder(""); - if (src == null || src.length <= 0) { - return null; - } - for (int i = 0; i < src.length; i++) { - int v = src[i] & 0xFF; - String str = Integer.toHexString(v); - if (str.length() < 2) { - str = "0" + str; - } - if (hasBlank) { - if (i == 0) { - stringBuilder.append(str); - } else { - stringBuilder.append(" " + str); - } - } else { - stringBuilder.append(str); - } - } - return stringBuilder.toString().toUpperCase(Locale.US); + return bytes2Hex_BE(src, hasBlank) ; } + + /** + * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓� + * + * @param src byte[] + * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅� + * @return 杩斿洖 String + */ + public static String bytes2Hex_BE(byte[] src, boolean hasBlank){ + StringBuilder stringBuilder = new StringBuilder(""); + if (src == null || src.length <= 0) { + return null; + } + for (int i = 0; i < src.length; i++) { + int v = src[i] & 0xFF; + String str = Integer.toHexString(v); + if (str.length() < 2) { + str = "0" + str; + } + if (hasBlank) { + if (i == 0) { + stringBuilder.append(str); + } else { + stringBuilder.append(" " + str); + } + } else { + stringBuilder.append(str); + } + } + return stringBuilder.toString().toUpperCase(Locale.US); + } + /** + * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓� + * + * @param src byte[] + * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅� + * @return 杩斿洖 String + */ + public static String bytes2Hex_LE(byte[] src, boolean hasBlank){ + StringBuilder stringBuilder = new StringBuilder(""); + if (src == null || src.length <= 0) { + return null; + } + for (int i = src.length - 1 ; i >= 0; i--) { + int v = src[i] & 0xFF; + String str = Integer.toHexString(v); + if (str.length() < 2) { + str = "0" + str; + } + if (hasBlank) { + if (i == 0) { + stringBuilder.append(str); + } else { + stringBuilder.append(" " + str); + } + } else { + stringBuilder.append(str); + } + } + return stringBuilder.toString().toUpperCase(Locale.US); + } /** * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓� * - * @param src byte[] + * @param bs byte[] * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅� * @param from 寮�濮嬩綅 * @param len 闀垮害 * @return 杩斿洖 String */ - public static String bytes2Hex(byte[] src, boolean hasBlank, int from, int len){ - if (src == null || src.length <= 0 || src.length < from + len) { + public static String bytes2Hex(byte[] bs, boolean hasBlank, int from, int len){ + if (bs == null || bs.length <= 0 || bs.length < from + len) { return null; } byte[] bb = new byte[len]; for (int i = 0 ; i < len; i++) { - bb[i] = src[from + i]; + bb[i] = bs[from + i]; } return bytes2Hex(bb, hasBlank) ; } + /** + * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓� + * + * @param bs byte[] + * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅� + * @param from 寮�濮嬩綅 + * @param len 闀垮害 + * @return 杩斿洖 String + */ + public static String bytes2Hex_LE(byte[] bs, boolean hasBlank, int from, int len){ + if (bs == null || bs.length <= 0 || bs.length < from + len) { + return null; + } + byte[] bb = new byte[len]; + for (int i = 0 ; i < len; i++) { + bb[i] = bs[from + i]; + } + return bytes2Hex_LE(bb, hasBlank) ; + } /** * 鍗佸叚杩涘埗杞瓧鑺傛暟缁� * @param hex the hex string @@ -975,78 +1077,29 @@ return fromIndex ; } - private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; /** - * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓� - * - * @param bytes 寰呰浆鎹yte[] - * @return 杩斿洖 杞崲鍚庣殑瀛楃涓� + * 鍗佸叚杩涘埗杞瓧鑺傛暟缁� + * @param hex the hex string + * @return 杩斿洖 byte[] */ - public static String bytesToHex(byte[] bytes) { - //涓�涓猙yte涓�8浣嶏紝鍙敤涓や釜鍗佸叚杩涘埗浣嶆爣璇� - char[] buf = new char[bytes.length * 2]; - int a = 0; - int index = 0; - for (byte b : bytes) { // 浣跨敤闄や笌鍙栦綑杩涜杞崲 - if (b < 0) { - a = 256 + b; - } else { - a = b; - } - - buf[index++] = HEX_CHAR[a / 16]; - buf[index++] = HEX_CHAR[a % 16]; + public static int hex2Bytes_LE(String hex, byte[] bs, int fromIndex) { + if (hex == null || hex.equals("")) { + return fromIndex; } - return new String(buf); - } - /** - * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓� - * - * @param bytes 寰呰浆鎹yte[] - * @return 杩斿洖 杞崲鍚庣殑瀛楃涓� - */ - public static String bytesToHex(byte[] bytes, int startIndex, int endIndex) { - byte[] bs = new byte[endIndex - startIndex + 1] ; - byte j = 0 ; - for(int i = startIndex; i <= endIndex; i++){ - bs[j++] = bytes[i] ; + hex = hex.toUpperCase(Locale.ENGLISH); + int length = hex.length() / 2; + char[] hexChars = hex.toCharArray(); + byte[] d = new byte[length]; + for (int i = 0; i < length; i++) { + int pos = i * 2; + d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } - //涓�涓猙yte涓�8浣嶏紝鍙敤涓や釜鍗佸叚杩涘埗浣嶆爣璇� - char[] buf = new char[bs.length * 2]; - int a = 0; - int index = 0; - for (byte b : bs) { // 浣跨敤闄や笌鍙栦綑杩涜杞崲 - if (b < 0) { - a = 256 + b; - } else { - a = b; - } - - buf[index++] = HEX_CHAR[a / 16]; - buf[index++] = HEX_CHAR[a % 16]; + for(int i = 0 ; i < d.length; i++){ + bs[fromIndex++] = d[(d.length - 1) - i] ; } - return new String(buf); + return fromIndex ; } - /** - * 灏�16杩涘埗瀛楃涓茶浆鎹负byte[] - * - * @param str 寰呰浆鎹㈠瓧绗︿覆 - * @return 杩斿洖 杞崲鍚庣殑byte[] - */ - public static byte[] hexToBytes(String str) { - if (str == null || "".equals(str.trim())) { - return new byte[0]; - } - - byte[] bytes = new byte[str.length() / 2]; - for (int i = 0; i < str.length() / 2; i++) { - String subStr = str.substring(i * 2, i * 2 + 2); - bytes[i] = (byte) Integer.parseInt(subStr, 16); - } - - return bytes; - } /** * Convert char to byte @@ -1093,6 +1146,31 @@ encodeBCD_LE(str, b, 0, b.length); return b; + } + + /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 鏁村舰杞垚BCD缂栫爜锛屽瓧鑺傞『搴忔槸鍊掔殑 + * @param i + * @param bs + * @param from + * @return 杩斿洖 + */ + public static void int2BCD_LE(int i, byte[] bs, int from)throws Exception { + String str = "" + i; + byte[] b = null; + if (str.length() % 2 == 0) { + b = new byte[str.length() / 2]; + } else { + b = new byte[(str.length() / 2) + 1]; + } + encodeBCD_LE(str, b, 0, b.length); + + int len = bs.length ; + int bLen = b.length ; + for(int j = 0; (j < len && j < bLen); j++){ + bs[from + j] = b[j] ; + } } /** @@ -1195,7 +1273,7 @@ } /** - * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘�� + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘�� * 瀛楃涓茶浆鎹㈡垚byte鏁扮粍 * @value bs byte[] * @value str String @@ -1306,8 +1384,7 @@ public static long BCD2Long_LE(byte[] b, int startIndex, int endIndex)throws Exception { String str = ""; str = decodeBCD_LE(b, startIndex, endIndex - startIndex + 1); - long n = Long.parseLong(str); - return n; + return Long.parseLong(str); } /** -- Gitblit v1.8.0