Netty provides a rich range of decoder abstract base classes, mainly divided into two categories:
- Decoding bytes to messages (Bytetomessagedecoder and Replayingdecoder)
- Decode message-to-message (Messagetomessagedecoder)
First, Bytetomessagedecoder
The Bytetomessagedecoder is used to convert bytes to information (or other byte sequences). Here's how:
In the following example, we will implement reading each integer from the inbound bytebuf and passing it to the next Channalinboundhandler in pipeline. The process is as follows:
The code is as follows:
1 /**2 * Read four bytes, decode into plastic3 * inherited from Bytetomessagedecoder4 *5 */6 Public classTointegerdecoderextendsBytetomessagedecoder {7 8 @Override9 protected voidDecode (Channelhandlercontext CTX, bytebuf in, list<object> out)throwsException {Ten if(In.readablebytes () >= 4) {//int is 4 bytes OneOut.add (In.readint ());//added to the list of decoded information A } - } - the}
Note that once a message is encoded or decoded, referencecountutil.release (message) is called automatically. If you need to use this reference later, you can call Referencecountutil.retain (message).
Second, Replayingdecoder
The above example needs to check if the buffer has enough bytes before reading the buffer's data, using Replayingdecoder without having to check it yourself. If there are enough bytes in the BYTEBUF, it will read normally, and if there are not enough bytes, it will stop decoding. as follows:
1 /**2 * Read four bytes, decode into plastic3 * Inherit from Replayingdecoder, no need to check buffer for sufficient bytes4 *5 */6 Public classToIntegerDecoder2extendsReplayingdecoder<void> {7 8 @Override9 protected voidDecode (Channelhandlercontext CTX, bytebuf in, list<object> out)throwsException {TenOut.add (In.readint ());//read shaping and add to the list of decoded information One } A -}
Third, Messagetomessagedecoder
used to decode from one message to another (for example, POJO to POJO). Similar to the above, the code is not posted.
Iv. handling too large frames in decoding
Netty are asynchronous schemas that need to have buffer bytes present in memory, knowing that you can decode them. Therefore, you cannot allow your decoder to cache too much data to avoid running out of available memory. Here's the solution:
1 /**2 * Handle too large frames when decoding3 *4 */5 Public classSafebytetomessagedecoderextendsBytetomessagedecoder {6 Private Static Final intMax_frame_size = 1024;7 8 @Override9 protected voidDecode (Channelhandlercontext CTX, bytebuf in, list<object> out)throwsException {Ten intreadable =in.readablebytes (); One if(Readable > Max_frame_size) {//buffer data is too large AIn.skipbytes (readable);//Ignore all readable bytes - //throw exception Notification This frame data is very long - Throw NewToolongframeexception ("Frame data extra Long"); the } - //TODO Data Encoding - } - +}
This protection is important, especially when you encode a protocol with a variable frame size.
Netty Introduction (vi) Decoder (decoder)