### 構造器參數 - maxFrameLength:指定解碼器所能處理的數據包的最大長度,超過該長度則拋出 TooLongFrameException 異常; - lengthFieldOffset:指定長度欄位的起始位置; - lengthFieldLength:指定長度欄位的長度:目前支持1( ...
構造器參數
- maxFrameLength:指定解碼器所能處理的數據包的最大長度,超過該長度則拋出 TooLongFrameException 異常;
- lengthFieldOffset:指定長度欄位的起始位置;
- lengthFieldLength:指定長度欄位的長度:目前支持1(byte)、2(short)、3(3個byte)、4(int)、8(Long)
- lengthAdjustment:指定長度欄位所表示的消息長度值與實際長度值之間的差值,可以用於調整解碼器的計算和提高靈活性。
- initialBytesToStrip:指定解碼器在將數據包分離出來後,跳過的位元組數,因為這些位元組通常不屬於消息體內容,而是協議頭或其他控制信息。
單元測試Demo
有關使用方法和方式參考測試Demo即可。
package com.netty.framework.core.channel;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
/**
* @author 左半邊是惡魔
* @date 2023/7/7
* @time 16:10
*/
public class LengthFieldBasedFrameDecoderTest {
private static final Logger LOGGER = LoggerFactory.getLogger(LengthFieldBasedFrameDecoderTest.class);
// 用於輸出內容
private final ChannelInboundHandlerAdapter handlerAdapter = new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
LOGGER.debug("handlerAdapter-接收到信息:{}", msg);
super.channelRead(ctx, msg);
}
};
/**
* 二進位數據結構
* lengthFieldOffset = 0
* lengthFieldLength = 4
* lengthAdjustment = 0
* initialBytesToStrip = 0 (不跳過長度欄位長度信息讀取)
* 編碼前 (16 bytes) 編碼後 (16 bytes)
* +------------+----------------+ +------------+----------------+
* | Length | Actual Content |----->| Length | Actual Content |
* | 0x0000000C | "Hello, World" | | 0x0000000C | "Hello, World" |
* +------------+----------------+ +------------+----------------+
*/
@Test
public void test00() {
class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
public LengthFieldDecoder() {
super(1024, 0, 4, 0, 0);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
LOGGER.debug("長度讀取前readerIndex={}", byteBuf.readerIndex());
// readInt會更新readerIndex值
int lengthField = byteBuf.readInt();
LOGGER.debug("長度欄位[LengthField]的值={},即(Hello, World)的二進位長度。", lengthField);
LOGGER.debug("長度讀取後readerIndex={}", byteBuf.readerIndex());
// toString方法根據readerIndex最新值開始解碼
return byteBuf.toString(StandardCharsets.UTF_8);
}
}
LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);
byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeInt(bytes.length);
byteBuf.writeBytes(bytes);
channel.writeInbound(byteBuf);
}
/**
* 二進位數據結構
* lengthFieldOffset = 0
* lengthFieldLength = 4
* lengthAdjustment = 0
* initialBytesToStrip = 4 (跳過長度欄位長度信息讀取)
* 編碼前 (16 bytes) 編碼後 (12 bytes)
* +------------+----------------+ +--------+--------+
* | Length | Actual Content |----->| Actual Content |
* | 0x0000000C | "Hello, World" | | "Hello, World" |
* +------------+----------------+ +--------+--------+
*/
@Test
public void test01() {
class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
public LengthFieldDecoder() {
super(1024, 0, 4, 0, 4);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
return byteBuf.toString(StandardCharsets.UTF_8);
}
}
LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);
byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeInt(bytes.length);
byteBuf.writeBytes(bytes);
channel.writeInbound(byteBuf);
}
/**
* 此處lengthField包含了消息頭的長度,即:Length = lengthFieldLength + 消息位元組長度(Hello, World的二進位長度)
* lengthFieldOffset = 0
* lengthFieldLength = 4
* lengthAdjustment = -4 (lengthField欄位的長度)
* initialBytesToStrip = 0
* 編碼前 (16 bytes) 編碼後 (16 bytes)
* +------------+----------------+ +------------+----------------+
* | Length | Actual Content |----->| Length | Actual Content |
* | 0x0000000G | "Hello, World" | | 0x0000000G | "Hello, World" |
* +------------+----------------+ +------------+----------------+
*/
@Test
public void test02() {
class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
public LengthFieldDecoder() {
super(1024, 0, 4, -4, 0);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
LOGGER.debug("長度讀取前readerIndex={}", byteBuf.readerIndex());
// readInt會更新readerIndex值
int lengthField = byteBuf.readInt();
LOGGER.debug("長度欄位[LengthField]的值={},即(Hello, World)的二進位長度。", lengthField);
LOGGER.debug("長度讀取後readerIndex={}", byteBuf.readerIndex());
// toString方法根據readerIndex最新值開始解碼
return byteBuf.toString(StandardCharsets.UTF_8);
}
}
LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);
byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
ByteBuf byteBuf = Unpooled.buffer();
// 4=int的位元組長度
byteBuf.writeInt(bytes.length + 4);
byteBuf.writeBytes(bytes);
channel.writeInbound(byteBuf);
}
/**
* lengthField在中間位置
* lengthFieldOffset = 2 (= the length of Header 1)
* lengthFieldLength = 4
* lengthAdjustment = 0
* initialBytesToStrip = 0
* <p>
* BEFORE DECODE (18 bytes) AFTER DECODE (18 bytes)
* +----------+----------+----------------+ +----------+----------+----------------+
* | Header 1 | Length | Actual Content |----->| Header 1 | Length | Actual Content |
* | 0xCAFE | 0x00000C | "Hello, World" | | 0xCAFE | 0x00000C | "Hello, World" |
* +----------+----------+----------------+ +----------+----------+----------------+
*/
@Test
public void test03() {
class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
public LengthFieldDecoder() {
super(1024, 2, 4, 0, 0);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
LOGGER.debug("長度讀取前readerIndex={}", byteBuf.readerIndex());
// 讀取header1
short header1 = byteBuf.readShort();
LOGGER.debug("header1={}", header1);
// readInt會更新readerIndex值
int lengthField = byteBuf.readInt();
LOGGER.debug("長度欄位[LengthField]的值={},即(Hello, World)的二進位長度。", lengthField);
LOGGER.debug("長度讀取後readerIndex={}", byteBuf.readerIndex());
// toString方法根據readerIndex最新值開始解碼
return byteBuf.toString(StandardCharsets.UTF_8);
}
}
LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);
byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeShort(99);
byteBuf.writeInt(bytes.length);
byteBuf.writeBytes(bytes);
channel.writeInbound(byteBuf);
}
/**
* lengthField在開始位置
* lengthFieldOffset = 0 (= the length of Header 1)
* lengthFieldLength = 4
* lengthAdjustment = 2
* initialBytesToStrip = 0
* BEFORE DECODE (18 bytes) AFTER DECODE (18 bytes)
* +----------+----------+----------------+ +----------+----------+----------------+
* | Length | Header 1 | Actual Content |----->| Length | Header 1 | Actual Content |
* | 0x00000C | 0xCAFE | "Hello, World" | | 0x00000C | 0xCAFE | "Hello, World" |
* +----------+----------+----------------+ +----------+----------+----------------+
*/
@Test
public void test04() {
class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
public LengthFieldDecoder() {
super(1024, 0, 4, 2, 0);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
LOGGER.debug("長度讀取前readerIndex={}", byteBuf.readerIndex());
// readInt會更新readerIndex值
int lengthField = byteBuf.readInt();
LOGGER.debug("長度欄位[LengthField]的值={},即(Hello, World)的二進位長度。", lengthField);
LOGGER.debug("長度讀取後readerIndex={}", byteBuf.readerIndex());
// 讀取header1
short header1 = byteBuf.readShort();
LOGGER.debug("header1={}", header1);
// toString方法根據readerIndex最新值開始解碼
return byteBuf.toString(StandardCharsets.UTF_8);
}
}
LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);
byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeInt(bytes.length);
byteBuf.writeShort(99);
byteBuf.writeBytes(bytes);
channel.writeInbound(byteBuf);
}
/**
* lengthFieldOffset = 1 (= the length of HDR1)
* lengthFieldLength = 4
* lengthAdjustment = 1 (= the length of HDR2)
* initialBytesToStrip = 5 (= the length of HDR1 + LEN)
* BEFORE DECODE (18 bytes) AFTER DECODE (13 bytes)
* +------+--------+------+----------------+ +------+----------------+
* | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
* | 0xCA | 0x000C | 0xFE | "Hello, World" | | 0xFE | "Hello, World" |
* +------+--------+------+----------------+ +------+----------------+
*/
@Test
public void test05() {
class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
public LengthFieldDecoder() {
super(1024, 1, 4, 1, 5);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
LOGGER.debug("長度讀取前readerIndex={}", byteBuf.readerIndex());
// 讀取header2
short header2 = byteBuf.readByte();
LOGGER.debug("header2={}", header2);
// toString方法根據readerIndex最新值開始解碼
return byteBuf.toString(StandardCharsets.UTF_8);
}
}
LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);
byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeByte(1);
byteBuf.writeInt(bytes.length);
byteBuf.writeByte(9);
byteBuf.writeBytes(bytes);
channel.writeInbound(byteBuf);
}
/**
* lengthFieldOffset = 1
* lengthFieldLength = 4 (整個消息的長度 = HDR1 + Length + HDR2 + Centent)
* lengthAdjustment = -5 (HDR1 + LEN的負值)
* initialBytesToStrip = 5
* BEFORE DECODE (18 bytes) AFTER DECODE (13 bytes)
* +------+--------+------+----------------+ +------+----------------+
* | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
* | 0xCA | 0x0010 | 0xFE | "Hello, World" | | 0xFE | "Hello, World" |
* +------+--------+------+----------------+ +------+----------------+
*/
@Test
public void test06() {
class LengthFieldDecoder extends LengthFieldBasedFrameDecoder {
public LengthFieldDecoder() {
super(1024, 1, 4, -5, 5);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
LOGGER.debug("長度讀取前readerIndex={}", byteBuf.readerIndex());
// 讀取header2
short header2 = byteBuf.readByte();
LOGGER.debug("header2={}", header2);
// toString方法根據readerIndex最新值開始解碼
return byteBuf.toString(StandardCharsets.UTF_8);
}
}
LengthFieldBasedFrameDecoder decoder = new LengthFieldDecoder();
EmbeddedChannel channel = new EmbeddedChannel(decoder, handlerAdapter);
byte[] bytes = "Hello, World".getBytes(StandardCharsets.UTF_8);
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeByte(1);
byteBuf.writeInt(bytes.length + 6);
byteBuf.writeByte(2);
byteBuf.writeBytes(bytes);
channel.writeInbound(byteBuf);
}
}