LengthFieldBasedFrameDecoder 的构造器参数
- maxFrameLength:指定解码器所能处理的数据包的最大长度,超过该长度则抛出 TooLongFrameException 异常;
- lengthFieldOffset:指定长度字段的起始位置;
- lengthFieldLength:指定长度字段的长度:目前支持1(byte)、2(short)、3(3个byte)、4(int)、8(Long)
- lengthAdjustment:指定长度字段所表示的消息长度值与实际长度值之间的差值,可以用于调整解码器的计算和提高灵活性。
- initialBytesToStrip:指定解码器在将数据包分离出来后,跳过的字节数,因为这些字节通常不属于消息体内容,而是协议头或其他控制信息。
LengthFieldBasedFrameDecoder测试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);
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |