From 2e98b20bea4463e4465e3c19059d0744a09aec06 Mon Sep 17 00:00:00 2001
From: zhongrj <646384940@qq.com>
Date: Tue, 27 Jun 2023 14:34:41 +0800
Subject: [PATCH] gb28181版本升级-补充
---
src/main/java/com/genersoft/iot/vmp/netty/config/MyDecoder.java | 132 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 132 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/netty/config/MyDecoder.java b/src/main/java/com/genersoft/iot/vmp/netty/config/MyDecoder.java
new file mode 100644
index 0000000..0d2488d
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/netty/config/MyDecoder.java
@@ -0,0 +1,132 @@
+package com.genersoft.iot.vmp.netty.config;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * 自定义解析
+ */
+@Component
+public class MyDecoder extends ByteToMessageDecoder {
+
+ @Override
+ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
+ //创建字节数组,buffer.readableBytes可读字节长度
+ byte[] b = new byte[buffer.readableBytes()];
+ //复制内容到字节数组b
+ buffer.readBytes(b);
+ //字节数组转字符串
+ String str = new String(b);
+
+ System.out.println(str);
+
+ out.add(bytesToHexString(b));
+ }
+
+ public String bytesToHexString(byte[] bArray) {
+ StringBuffer sb = new StringBuffer(bArray.length);
+ String sTemp;
+ for (int i = 0; i < bArray.length; i++) {
+ sTemp = Integer.toHexString(0xFF & bArray[i]);
+ if (sTemp.length() < 2)
+ sb.append(0);
+ sb.append(sTemp.toUpperCase());
+ }
+ return sb.toString();
+ }
+
+ public static String toHexString1(byte[] b) {
+ StringBuffer buffer = new StringBuffer();
+ for (int i = 0; i < b.length; ++i) {
+ buffer.append(toHexString1(b[i]));
+ }
+ return buffer.toString();
+ }
+
+ public static String toHexString1(byte b) {
+ String s = Integer.toHexString(b & 0xFF);
+ if (s.length() == 1) {
+ return "0" + s;
+ } else {
+ return s;
+ }
+ }
+
+ /**
+ * 16进制字符串转换为字符串
+ *
+ * @param s
+ * @return
+ */
+ public static String hexStringToString(String s) {
+ if (s == null || s.equals("")) {
+ return null;
+ }
+ s = s.replace(" ", "");
+ byte[] baKeyword = new byte[s.length() / 2];
+ for (int i = 0; i < baKeyword.length; i++) {
+ try {
+ baKeyword[i] = (byte) (0xff & Integer.parseInt(
+ s.substring(i * 2, i * 2 + 2), 16));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ try {
+ s = new String(baKeyword, "gbk");
+ new String();
+ } catch (Exception e1) {
+ e1.printStackTrace();
+ }
+ return s;
+ }
+
+
+ /**
+ * Hex字符串转byte
+ * @param inHex 待转换的Hex字符串
+ * @return 转换后的byte
+ */
+ public static byte hexToByte(String inHex){
+ return (byte)Integer.parseInt(inHex,16);
+ }
+
+ /**
+ * Hex字符串转byte
+ * @param inHex 待转换的Hex字符串
+ * @return 转换后的byte
+ */
+ public static short hexTohort(String inHex){
+ return (short)Integer.parseInt(inHex,16);
+ }
+
+
+ /**
+ * hex字符串转byte数组
+ * @param inHex 待转换的Hex字符串
+ * @return 转换后的byte数组结果
+ */
+ public static byte[] hexToByteArray(String inHex){
+ int hexlen = inHex.length();
+ byte[] result;
+ if (hexlen % 2 == 1){
+ //奇数
+ hexlen++;
+ result = new byte[(hexlen/2)];
+ inHex="0"+inHex;
+ }else {
+ //偶数
+ result = new byte[(hexlen/2)];
+ }
+ int j=0;
+ for (int i = 0; i < hexlen; i+=2){
+ result[j]=hexToByte(inHex.substring(i,i+2));
+ j++;
+ }
+ return result;
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3