From 1fd64b07ddb99c2d9cc8a358b71aceb6a2c81492 Mon Sep 17 00:00:00 2001 From: ‘liusuyi’ <1951119284@qq.com> Date: 星期三, 05 七月 2023 15:58:31 +0800 Subject: [PATCH] 修改nettyTCP客户端断开重连 增加数据库连接池 修改日志打印 --- src/main/java/com/ard/utils/ByteUtils.java | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/ard/utils/ByteUtils.java b/src/main/java/com/ard/utils/ByteUtils.java index 507ddcc..33c7856 100644 --- a/src/main/java/com/ard/utils/ByteUtils.java +++ b/src/main/java/com/ard/utils/ByteUtils.java @@ -1,10 +1,9 @@ package com.ard.utils; import javax.xml.bind.DatatypeConverter; +import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.zip.CRC32; /** * @Description: 瀛楄妭宸ュ叿绫� @@ -16,20 +15,20 @@ public class ByteUtils { /** * Byte瀛楄妭杞琀ex + * * @param b 瀛楄妭 * @return Hex */ - public static String byteToHex(byte b) - { + public static String byteToHex(byte b) { String hexString = Integer.toHexString(b & 0xFF); //鐢变簬鍗佸叚杩涘埗鏄敱0~9銆丄~F鏉ヨ〃绀�1~16锛屾墍浠ュ鏋淏yte杞崲鎴怘ex鍚庡鏋滄槸<16,灏变細鏄竴涓瓧绗︼紙姣斿A=10锛夛紝閫氬父鏄娇鐢ㄤ袱涓瓧绗︽潵琛ㄧず16杩涘埗浣嶇殑, //鍋囧涓�涓瓧绗︾殑璇濓紝閬囧埌瀛楃涓�11锛岃繖鍒板簳鏄�1涓瓧鑺傦紝杩樻槸1鍜�1涓や釜瀛楄妭锛屽鏄撴贩娣嗭紝濡傛灉鏄ˉ0锛岄偅涔�1鍜�1琛ュ厖鍚庡氨鏄�0101锛�11灏辫〃绀虹函绮圭殑11 - if (hexString.length() < 2) - { + if (hexString.length() < 2) { hexString = new StringBuilder(String.valueOf(0)).append(hexString).toString(); } return hexString.toUpperCase(); } + /** * byte鏁扮粍杞琭loat */ @@ -37,6 +36,7 @@ ByteBuffer buffer = ByteBuffer.wrap(bytes); return buffer.getFloat(); } + /** * byte鏁扮粍杞暣鍨� */ @@ -79,4 +79,98 @@ return littleEndianBytes; } + /** + * 灏忕杞ぇ绔� + */ + public static byte[] toBigEndian(byte[] littleEndianBytes) { + byte[] bigEndianBytes = new byte[littleEndianBytes.length]; + for (int i = 0; i < littleEndianBytes.length; i++) { + bigEndianBytes[i] = littleEndianBytes[littleEndianBytes.length - 1 - i]; + } + return bigEndianBytes; + } + + /** + * int杞琤yte鏁扮粍 + */ + public static byte[] decimalToBytes(int decimalValue) { + ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); + buffer.putInt(decimalValue); + return buffer.array(); + } + + /** + * byte鏁扮粍鎷兼帴 + */ + public static byte[] appendArrays(byte[]... arrays) { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + try { + for (byte[] array : arrays) { + outputStream.write(array); + } + } catch (Exception e) { + e.printStackTrace(); + } + return outputStream.toByteArray(); + } + + /** + * byte鏁扮粍CRC32鏍¢獙 + */ + public static byte[] parseCrc32(byte[] bytes) { + CRC32 crc32 = new CRC32(); + crc32.update(bytes); + bytes = ByteUtils.decimalToBytes((int) crc32.getValue()); + bytes = ByteUtils.toBigEndian(bytes); + // System.out.print("鏍¢獙:"+DatatypeConverter.printHexBinary(bytes));//鎵撳嵃crc32鐨勬牎楠屽�� + return bytes; + } + + /** + * 鍘婚櫎鍖呭ご鍖呭熬 + * 鍒樿嫃涔� + * 2023/7/4 11:24 + */ + public static byte[] removeHeaderAndFooter(byte[] data, int headerLength, int footerLength) { + int payloadLength = data.length - headerLength - footerLength; + byte[] payload = new byte[payloadLength]; + System.arraycopy(data, headerLength, payload, 0, payloadLength); + return payload; + } + + /** + * 鍘婚櫎鍖呭ご鍖呭熬crc鏍¢獙 + * 鍒樿嫃涔� + * 2023/7/4 11:24 + */ + public static byte[] removeHeaderFooterAndCRC(byte[] data, int headerLength, int footerLength, int crcLength) { + int payloadLength = data.length - headerLength - footerLength - crcLength; + byte[] payload = new byte[payloadLength]; + System.arraycopy(data, headerLength, payload, 0, payloadLength); + return payload; + } + + /** + * 鑾峰彇鍚庨潰鐨勫瓧鑺傛暟缁� + * 鍒樿嫃涔� + * 2023/7/4 11:24 + */ + public static byte[] getLastBytes(byte[] data, int count) { + int startIndex = data.length - count; + byte[] lastBytes = new byte[count]; + System.arraycopy(data, startIndex, lastBytes, 0, count); + return lastBytes; + } + + /** + * 鎵惧埌00鐨勭储寮曚綅缃� + */ + public static int findIndexOfDoubleZero(byte[] bytes) { + for (int i = 0; i < bytes.length - 1; i++) { + if (bytes[i] == 0x00) { + return i; + } + } + return -1; + } } -- Gitblit v1.9.3