Netty之數據解碼

来源:https://www.cnblogs.com/inverseEntropy/archive/2023/04/30/17365544.html
-Advertisement-
Play Games

JSP 的本質原理解析:"編寫的時候是JSP,心裡想解讀的是 java 源碼" @ 每博一文案 活明白的人,一生只做好了這兩件事: 每個瞬間都充滿了選擇和承擔,就算面前是一座獨木橋,也必須選擇是前進後退,亦或是留在原地此時此刻你所經歷的一切。 這是過往無數個選擇後的結果,哪些小的選擇匯聚在了一起,最 ...


一、概況

   作為Java世界使用最廣泛的網路通信框架Netty,其性能和效率是有目共睹的,好多大公司都在使用如蘋果、谷歌、Facebook、Twitter、阿裡巴巴等,所以不僅僅是因為Netty有高效的性能與效率,更重要的是:屏蔽了底層的複雜度,簡單易懂的編程模型,適應更廣泛的應用場景,以及活躍的開發者社區。
  本篇博客是作為Netty之數據編碼的續篇,上一篇以拋磚引玉的方式講解了怎麼使用Netty的核心緩衝區ByteBuf怎麼編碼存儲各種基本數據,本篇就是與之對應的怎麼從緩衝區ByteBuf中的編碼數據解碼出來,因為我們的Java代碼中處理數據一般不是按照位元組流來處理,所以需要解碼恢復出數據然後再進行處理。

二、代碼實現

1. 解碼工具類

  1 package com.ethan.cws.common.utils;
  2 
  3 import com.ethan.cws.common.enums.TypeEnum;
  4 import io.netty.buffer.ByteBuf;
  5 import io.netty.buffer.ByteBufUtil;
  6 import io.netty.util.CharsetUtil;
  7 
  8 import java.util.ArrayList;
  9 import java.util.Arrays;
 10 import java.util.List;
 11 
 12 /**
 13  * 解碼工具類
 14  *
 15  * @author ethancws
 16  * @date 
 17  */
 18 public final class DecodeUtils {
 19     
 20     /**
 21      * FEP data數據文件尾碼名
 22      */
 23     public final static String FILE_SUFFIX_EXTEND = ".xml";
 24     
 25     /**
 26      * 文件名
 27      */
 28     public final static String FILE_NAME = "Filename";
 29 
 30     private DecodeUtils() {
 31 
 32     }
 33 
 34     /**
 35      * 解碼
 36      *
 37      * @param symbol  符號
 38      * @param byteNum 位元組數
 39      * @param buff    數據
 40      * @param type    枚舉類型字元串
 41      * @param endian  編碼
 42      * @return 解碼數據
 43      */
 44     public static Object decode(String symbol, int byteNum, ByteBuf buff,
 45                                 String type, boolean endian) {
 46         Object value = null;
 47         //類型枚舉
 48         final TypeEnum typeEnum = TypeEnum.match(type);
 49         switch (typeEnum) {
 50             case TYPE_STRING:
 51             case TYPE_ENUM_STRING:
 52             case TYPE_DATE_STRING:
 53                 value = readString(byteNum, buff, symbol);
 54                 break;
 55             case TYPE_HEX_STRING:
 56             case TYPE_ENUM_HEX_STRING:
 57                 value = readHexString(byteNum, buff);
 58                 break;
 59             case TYPE_USHORT:
 60                 value = readUnSignShort(buff, endian);
 61                 break;
 62             case TYPE_SHORT:
 63                 value = readShort(buff, endian);
 64                 break;
 65             case TYPE_INT:
 66             case TYPE_ENUM_INT:
 67                 value = readInt(buff, endian);
 68                 break;
 69             case TYPE_UINT:
 70                 value = readUnSignInt(buff, endian);
 71                 break;
 72             case TYPE_BYTE:
 73             case TYPE_ENUM_BYTE:
 74                 value = readByte(buff);
 75                 break;
 76             case TYPE_UBYTE:
 77                 value = readUnSignByte(buff);
 78                 break;
 79             case TYPE_BIT:
 80                 value = readBit(byteNum, buff);
 81                 break;
 82             case TYPE_MULTI_BIT:
 83                 value = readMultiBit(byteNum, buff);
 84                 break;
 85             case TYPE_BCD8421:
 86                 value = readBcd8421(byteNum, buff);
 87                 break;
 88 
 89         }
 90 
 91         return value;
 92     }
 93 
 94     /**
 95      * 讀無符號byte
 96      *
 97      * @param buff 編碼數據
 98      * @return 解碼數據
 99      */
100     public static short readUnSignByte(ByteBuf buff) {
101         byte by = buff.readByte();
102         return (short) (by & 0x0FF);
103     }
104 
105     /**
106      * 讀byte
107      *
108      * @param buff 編碼數據
109      * @return 解碼數據
110      */
111     public static byte readByte(ByteBuf buff) {
112         return buff.readByte();
113     }
114 
115     /**
116      * 讀無符號int
117      *
118      * @param buff   編碼數據
119      * @param endian 位元組序
120      * @return 解碼數據
121      */
122     public static long readUnSignInt(ByteBuf buff, boolean endian) {
123         int intValue = endian ? buff.readIntLE() : buff.readInt();
124         return intValue & 0x0FFFFFFFFL;
125     }
126 
127     /**
128      * 讀int
129      *
130      * @param buff   編碼數據
131      * @param endian 位元組序
132      * @return 解碼數據
133      */
134     public static int readInt(ByteBuf buff, boolean endian) {
135         return endian ? buff.readIntLE() : buff.readInt();
136     }
137 
138     /**
139      * 讀short
140      *
141      * @param buff   編碼數據
142      * @param endian 位元組序
143      * @return 解碼數據
144      */
145     public static short readShort(ByteBuf buff, boolean endian) {
146         return endian ? buff.readShortLE() : buff.readShort();
147     }
148 
149     /**
150      * 讀無符號short
151      *
152      * @param buff   編碼數據
153      * @param endian 位元組序
154      * @return 解碼數據
155      */
156     public static int readUnSignShort(ByteBuf buff, boolean endian) {
157         short shortValue = endian ? buff.readShortLE() : buff.readShort();
158         return shortValue & 0x0FFFF;
159     }
160 
161     /**
162      * 讀Hex字元串
163      *
164      * @param num  位元組長度
165      * @param buff 編碼數據
166      * @return 字元串
167      */
168     public static String readHexString(int num, ByteBuf buff) {
169         String value = ByteBufUtil.hexDump(buff, 0, num);
170         readByteBuf(num, buff);
171         return value;
172     }
173 
174     /**
175      * 讀Hex字元串沒有數據緩衝區偏移
176      *
177      * @param num  位元組長度
178      * @param buff 編碼數據
179      * @return 字元串
180      */
181     public static String readHexStringWithoutOffset(int num, ByteBuf buff) {
182         return ByteBufUtil.hexDump(buff, 0, num);
183     }
184 
185     /**
186      * 獲取文件名稱
187      *
188      * @param fileName 字元
189      * @return 文件名稱
190      */
191     private static String acquireFileName(String fileName) {
192         String fileSuffixExtend = FILE_SUFFIX_EXTEND;
193         int index = fileName.lastIndexOf(fileSuffixExtend);
194         index += fileSuffixExtend.length();
195         fileName = fileName.substring(1, index);
196         return fileName;
197     }
198 
199     /**
200      * 讀字元串
201      *
202      * @param num    位元組長度
203      * @param buff   編碼數據
204      * @param symbol 編碼標識
205      * @return 字元串
206      */
207     public static String readString(int num, ByteBuf buff, String symbol) {
208         final CharSequence charSequence = buff.getCharSequence(0, num, CharsetUtil.UTF_8);
209         String value = charSequence.toString();
210         if (FILE_NAME.equals(symbol)) {
211             value = acquireFileName(value);
212         }
213         //移動讀指針
214         readByteBuf(num, buff);
215         return value;
216     }
217 
218 
219     /**
220      * 移動讀指針
221      *
222      * @param num  移動位元組數
223      * @param buff 數據緩衝區ByteBuf
224      */
225     private static void readByteBuf(int num, ByteBuf buff) {
226         assert num >= 1;
227         if (num == 1) {
228             buff.readByte();
229         } else {
230             buff.readBytes(num);
231         }
232     }
233 
234     /**
235      * 讀bit
236      *
237      * @param num  位元組長度
238      * @param buff 數據緩衝區ByteBuf
239      * @return bit位索引
240      */
241     public static int readBit(int num, ByteBuf buff) {
242         ByteBuf buffCopy = buff.copy(0, num);
243         int index = 0;
244         for (; num > 0; num--) {
245             byte b = buffCopy.readByte();
246             if (b != 0) {
247                 index += b / 2;
248                 --num;
249                 break;
250             }
251         }
252         index += num * 8;
253         //移動讀指針
254         readByteBuf(num, buff);
255         return index;
256     }
257 
258     /**
259      * 讀多位bit
260      *
261      * @param num  位元組長度
262      * @param buff 數據緩衝區ByteBuf
263      * @return 二進位數據為1的索引數組
264      */
265     public static int[] readMultiBit(int num, ByteBuf buff) {
266         ByteBuf buffCopy = buff.copy(0, num);
267         List<Integer> list = new ArrayList<>();
268         int size = num;
269         final int fixedNum = num;
270         for (; num > 0; num--) {
271             size--;
272             int b = readUnSignByte(buffCopy);
273             if (b != 0) {
274                 String str = Integer.toBinaryString(b);
275                 str = fullFillByteString(str);
276                 gatherIndexes(str, size, list);
277             }
278         }
279         //移動讀指針
280         readByteBuf(fixedNum, buff);
281         return Arrays.stream(list.toArray(new Integer[0])).mapToInt(Integer::valueOf).toArray();
282     }
283 
284     /**
285      * 補全byte二進位8位字元串
286      *
287      * @param str 字元串
288      * @return 補全8位後的字元串
289      */
290     private static String fullFillByteString(String str) {
291         int len = 8;
292         int length = str.length();
293         if (length < 8) {
294             StringBuilder strBuilder = new StringBuilder(str);
295             for (int i = 0; i < len - length; i++) {
296                 strBuilder.insert(0, "0");
297             }
298             str = strBuilder.toString();
299         }
300         return str;
301     }
302 
303     /**
304      * 收集索引存入List
305      *
306      * @param str  byte二進位字元串
307      * @param size 剩餘byte長度
308      * @param list 集合List
309      */
310     private static void gatherIndexes(String str, int size, List<Integer> list) {
311         int len = 8, lenFixed = 8;
312         for (char ch : str.toCharArray()) {
313             int totalIndex = 0;
314             len--;
315             if (ch == 48) {
316                 continue;
317             }
318             totalIndex = len + size * lenFixed;
319             list.add(totalIndex);
320         }
321     }
322 
323     /**
324      * 讀Bcd碼
325      *
326      * @param num  位元組長度
327      * @param buff 數據緩衝區ByteBuf
328      * @return Bcd碼解碼數據
329      */
330     public static String readBcd8421(int num, ByteBuf buff) {
331         return readHexString(num, buff);
332     }
333 }

 

2. 數據類型枚舉類

  1 package com.ethan.cws.common.enums;
  2 
  3 /**
  4  * 數據枚舉
  5  *
  6  * @author ethancws
  7  * @date 
  8  */
  9 public enum TypeEnum {
 10     /**
 11      * 字元串
 12      */
 13     TYPE_STRING("string"),
 14 
 15     /**
 16      * Binary-Coded Decimal
 17      * bcd碼 8421碼
 18      * 4位二進位數表示1位十進位數
 19      */
 20     TYPE_BCD8421("bcd8421"),
 21     /**
 22      * 時間字元串
 23      */
 24     TYPE_DATE_STRING("date_string"),
 25     /**
 26      * 枚舉byte
 27      */
 28     TYPE_ENUM_BYTE("enum|byte"),
 29 
 30     /**
 31      * 枚舉int
 32      */
 33     TYPE_ENUM_INT("enum|int"),
 34 
 35     /**
 36      * 枚舉字元串
 37      */
 38     TYPE_ENUM_STRING("enum|string"),
 39 
 40     /**
 41      * 枚舉HEX字元串
 42      */
 43     TYPE_ENUM_HEX_STRING("enum|hex_string"),
 44 
 45     /**
 46      * HEX字元串
 47      */
 48     TYPE_HEX_STRING("hex_string"),
 49 
 50     /**
 51      * -2^31~2^31-1
 52      * -2,147,483,648~2,147,483,647
 53      */
 54     TYPE_INT("int"),
 55     /**
 56      * 0~2^32
 57      * 0~4294967296L
 58      */
 59     TYPE_UINT("uint"),
 60     /**
 61      * -2^15~2^15-1
 62      * -32768~32767
 63      */
 64     TYPE_SHORT("short"),
 65     /**
 66      * 0~65535
 67      */
 68     TYPE_USHORT("ushort"),
 69     /**
 70      * -2^7~2^7-1
 71      * -128~127
 72      */
 73     TYPE_BYTE("byte"),
 74 
 75     /**
 76      * 0~256
 77      */
 78     TYPE_UBYTE("ubyte"),
 79 
 80     /**
 81      * 多位同選
 82      */
 83     TYPE_MULTI_BIT("multi_bit"),
 84     /**
 85      * 位
 86      */
 87     TYPE_BIT("bit");
 88 
 89     private String val;
 90 
 91     TypeEnum(String val) {
 92         this.val = val;
 93     }
 94 
 95 
 96     /**
 97      * 字元串匹配枚舉類型
 98      *
 99      * @param value 字元串
100      * @return 對應枚舉
101      	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近遇到公司的一個項目,需要將多張圖片合併成一個播放的視頻,找了很多資料和嘗試了工具,遇到很多的坑,這裡記下來,希望大家也能順利解決遇到的問題。 合併視頻,主要可以借用OpenCV 和 ffmpeg,這裡是嘗試用ffmpeg.exe的工具去實現圖片文件合併成視頻。 輸入存儲視頻文件的路徑,通過Pro ...
  • 痞子衡嵌入式半月刊: 第 76 期 這裡分享嵌入式領域有用有趣的項目/工具以及一些熱點新聞,農曆年分二十四節氣,希望在每個交節之日準時發佈一期。 本期刊是開源項目(GitHub: JayHeng/pzh-mcu-bi-weekly),歡迎提交 issue,投稿或推薦你知道的嵌入式那些事兒。 上期回顧 ...
  • (Linux安裝軟體) 前言 這期呢主要說一說Linux中包軟體管理相關命令,這一期的命令雖然只有兩個。但 軟體包的安裝和卸載都是我們平常最常用的,需要熟練掌握。 rpm和yum 是CentOS 主要的包軟體管理。 兩個命令各有用處,①yum需要互聯網,yum會去網上的yum源獲取所需的軟體包 ② ...
  • 前言 本篇文章主要介紹的關於本人在使用MySql記錄筆記的一些使用方法和經驗,溫馨提示,本文有點長,約1.5w字,幾十張圖片,建議收藏查看。 一、MySql安裝 下載地址:https://dev.mysql.com/downloads/ 在安裝MySql之前,查看是否以及安裝過MySql,如果已經安 ...
  • CSS知識點總結 文章內容可能較多且雜亂,可以查看頁面右方的目錄,以及使用Ctrl+F搜索頁面內容進行內容定位。 常用屬性 推薦搭配文檔使用,可以複製屬性名,到文檔查看該屬性對應的可選值。 👉MDN Web Docs 盒模型 寬度:width 高度:height 邊框:border 圓角:bord ...
  • 1、四層結構 viewer --> datasources(DataSourceCollection類型) --> datasource --> entities(EntityCollection類型) --> entity 需要學習的方向是:只需要註意每個層與層之間的關係和entity實例如何創建 ...
  • 一、前言 第二次在博客園上發佈面向對象程式設計題目集的總結博客。經過幾周的學習,面向對象的理念更加深入。雖然已經學了些面向對象程式設計,學好這部分內容還是有較大難度。 關於知識點 本次的題目集所體現的知識點已經不僅限於Java的語法知識,還需要考慮設計問題,不能看到題目就開始進行代碼編寫,需要考慮類 ...
  • B/S 結構系統的 緩存機制(Cookie) 以及基於 cookie 機制實現 oa 十天免登錄的功能 @ 每博一文案 嘿,大風揚起的沉沙中,每一粒都有它的必然性,而每個人的命運都有自己的因果, 為自己的選擇負責承擔或好或壞的結果。是成年人的必修課。 有人請教索羅斯投資的指導,我的父親一直追隨你炒股 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...