public class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder
ByteBuf
。
当解码具有表示消息正文或整个消息长度的整数标题字段的二进制消息时,它特别有用。
LengthFieldBasedFrameDecoder
有许多配置参数,因此它可以解码长度字段的任何消息,这在专有的客户端 - 服务器协议中经常见到。 这里有一些例子会给你关于哪个选项做什么的基本想法。
lengthFieldOffset = 0
lengthFieldLength = 2
lengthAdjustment = 0
initialBytesToStrip = 0 (= do not strip header)
BEFORE DECODE (14 bytes) AFTER DECODE (14 bytes)
+--------+----------------+ +--------+----------------+
| Length | Actual Content |----->| Length | Actual Content |
| 0x000C | "HELLO, WORLD" | | 0x000C | "HELLO, WORLD" |
+--------+----------------+ +--------+----------------+
ByteBuf.readableBytes()
来获取内容的长度,所以您可能希望通过指定initialBytesToStrip来剥离长度字段。
在这个例子中,我们指定了2 ,这与长度字段的长度相同,以去掉前两个字节。
lengthFieldOffset = 0
lengthFieldLength = 2
lengthAdjustment = 0
initialBytesToStrip = 2 (= the length of the Length field)
BEFORE DECODE (14 bytes) AFTER DECODE (12 bytes)
+--------+----------------+ +----------------+
| Length | Actual Content |----->| Actual Content |
| 0x000C | "HELLO, WORLD" | | "HELLO, WORLD" |
+--------+----------------+ +----------------+
lengthFieldOffset = 0
lengthFieldLength = 2
lengthAdjustment = -2 (= the length of the Length field)
initialBytesToStrip = 0
BEFORE DECODE (14 bytes) AFTER DECODE (14 bytes)
+--------+----------------+ +--------+----------------+
| Length | Actual Content |----->| Length | Actual Content |
| 0x000E | "HELLO, WORLD" | | 0x000E | "HELLO, WORLD" |
+--------+----------------+ +--------+----------------+
lengthFieldOffset = 2 (= the length of Header 1)
lengthFieldLength = 3
lengthAdjustment = 0
initialBytesToStrip = 0
BEFORE DECODE (17 bytes) AFTER DECODE (17 bytes)
+----------+----------+----------------+ +----------+----------+----------------+
| Header 1 | Length | Actual Content |----->| Header 1 | Length | Actual Content |
| 0xCAFE | 0x00000C | "HELLO, WORLD" | | 0xCAFE | 0x00000C | "HELLO, WORLD" |
+----------+----------+----------------+ +----------+----------+----------------+
lengthFieldOffset = 0
lengthFieldLength = 3
lengthAdjustment = 2 (= the length of Header 1)
initialBytesToStrip = 0
BEFORE DECODE (17 bytes) AFTER DECODE (17 bytes)
+----------+----------+----------------+ +----------+----------+----------------+
| Length | Header 1 | Actual Content |----->| Length | Header 1 | Actual Content |
| 0x00000C | 0xCAFE | "HELLO, WORLD" | | 0x00000C | 0xCAFE | "HELLO, WORLD" |
+----------+----------+----------------+ +----------+----------+----------------+
lengthFieldOffset = 1 (= the length of HDR1)
lengthFieldLength = 2
lengthAdjustment = 1 (= the length of HDR2)
initialBytesToStrip = 3 (= the length of HDR1 + LEN)
BEFORE DECODE (16 bytes) AFTER DECODE (13 bytes)
+------+--------+------+----------------+ +------+----------------+
| HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
| 0xCA | 0x000C | 0xFE | "HELLO, WORLD" | | 0xFE | "HELLO, WORLD" |
+------+--------+------+----------------+ +------+----------------+
lengthFieldOffset = 1
lengthFieldLength = 2
lengthAdjustment = -3 (= the length of HDR1 + LEN, negative)
initialBytesToStrip = 3
BEFORE DECODE (16 bytes) AFTER DECODE (13 bytes)
+------+--------+------+----------------+ +------+----------------+
| HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
| 0xCA | 0x0010 | 0xFE | "HELLO, WORLD" | | 0xFE | "HELLO, WORLD" |
+------+--------+------+----------------+ +------+----------------+
LengthFieldPrepender
ByteToMessageDecoder.Cumulator
ChannelHandler.Sharable
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
Constructor and Description |
---|
LengthFieldBasedFrameDecoder(java.nio.ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast)
创建一个新的实例。
|
LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength)
创建一个新的实例。
|
LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip)
创建一个新的实例。
|
LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast)
创建一个新的实例。
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
decode(ChannelHandlerContext ctx, ByteBuf in)
从 ByteBuf 中创建一个框架并将其返回。
|
protected void |
decode(ChannelHandlerContext ctx, ByteBuf in, java.util.List<java.lang.Object> out)
解码从一个 ByteBuf 到另一个。
|
protected ByteBuf |
extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length)
提取指定缓冲区的子区域。
|
protected long |
getUnadjustedFrameLength(ByteBuf buf, int offset, int length, java.nio.ByteOrder order)
将缓冲区的指定区域解码为未调整的帧长度。
|
actualReadableBytes, callDecode, channelInactive, channelRead, channelReadComplete, decodeLast, discardSomeReadBytes, handlerRemoved, handlerRemoved0, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggered
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught
ensureNotSharable, handlerAdded, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerAdded
public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength)
maxFrameLength
- 帧的最大长度。
如果帧的长度大于此值,则会抛出TooLongFrameException
。
lengthFieldOffset
- 长度字段的偏移量
lengthFieldLength
- 长度字段的长度
public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip)
maxFrameLength
- 帧的最大长度。
如果帧的长度大于此值,则会抛出TooLongFrameException
。
lengthFieldOffset
- 长度字段的偏移量
lengthFieldLength
- 长度字段的长度
lengthAdjustment
- 要添加到长度字段值的补偿值
initialBytesToStrip
- 从解码帧中
initialBytesToStrip
的第一个字节数
public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast)
maxFrameLength
- 帧的最大长度。
如果帧的长度大于此值,则会抛出TooLongFrameException
。
lengthFieldOffset
- 长度字段的偏移量
lengthFieldLength
- 长度字段的长度
lengthAdjustment
- 要添加到长度字段值的补偿值
initialBytesToStrip
- 从解码帧中
initialBytesToStrip
的第一个字节数
failFast
-如果true,一个TooLongFrameException
被只要解码器注意到该帧的长度将超过maxFrameLength不管整个帧是否已被读出抛出。
如果false,一个TooLongFrameException
超过maxFrameLength整个框架已经被读取之后被抛出。
public LengthFieldBasedFrameDecoder(java.nio.ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast)
byteOrder
- 长度字段的
ByteOrder
maxFrameLength
- 帧的最大长度。
如果帧的长度大于此值,则会抛出TooLongFrameException
。
lengthFieldOffset
- 长度字段的偏移量
lengthFieldLength
- 长度字段的长度
lengthAdjustment
- 要添加到长度字段值的补偿值
initialBytesToStrip
- 从解码帧中
initialBytesToStrip
的第一个字节数
failFast
-如果true,一个TooLongFrameException
被只要解码器注意到该帧的长度将超过maxFrameLength不管整个帧是否已被读出抛出。
如果false,一个TooLongFrameException
超过maxFrameLength整个框架已经被读取之后被抛出。
protected final void decode(ChannelHandlerContext ctx, ByteBuf in, java.util.List<java.lang.Object> out) throws java.lang.Exception
ByteToMessageDecoder
decode
在课程
ByteToMessageDecoder
ctx
- 这是ByteToMessageDecoder
所属的ChannelHandlerContext
in
- 从中读取数据的ByteBuf
out
- 应该添加解码消息的
List
java.lang.Exception
- 发生错误时抛出
protected java.lang.Object decode(ChannelHandlerContext ctx, ByteBuf in) throws java.lang.Exception
ByteBuf
中创建一个框架并将其返回。
ctx
- ByteToMessageDecoder
属于的ChannelHandlerContext
in
- 从中读取数据的ByteBuf
ByteBuf
代表帧或null
。
java.lang.Exception
protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, java.nio.ByteOrder order)
readerIndex
, writerIndex
和缓冲器的内容。)
DecoderException
- 如果未能解码指定区域
protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length)
如果您确定在当前的decode(ChannelHandlerContext, ByteBuf)
调用返回后没有访问帧及其内容,则甚至可以通过返回切片的子区域(即return buffer.slice(index, length) )来避免内存复制。 将提取的帧转换为对象时通常很有用。 请参阅源代码ObjectDecoder
以了解如何覆盖此方法以避免内存复制。
Copyright © 2008–2018 The Netty Project. All rights reserved.