ByteBufs dynamically by the
value of the length field in the message. It is particularly useful when you
decode a binary message which has an integer header field that represents the
length of the message body or the whole message.
LengthFieldBasedFrameDecoder has many configuration parameters so
that it can decode any message with a length field, which is often seen in
proprietary client-server protocols. Here are some example that will give
you the basic idea on which option does what.
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(), you might want to strip the length
field by specifying initialBytesToStrip. In this example, we
specified 2, that is same with the length of the length field, to
strip the first two bytes.
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
| Modifier and Type | Field and Description |
|---|---|
| private final ByteOrder | |
| private long | |
| private boolean | |
| private final boolean | |
| private int | |
| private final int | |
| private final int | |
| private final int | |
| private final int | |
| private final int | |
| private final int | |
| private long |
| Access | Constructor and Description |
|---|---|
| public | LengthFieldBasedFrameDecoder(int
the maximum length of the frame. If the length of the frame is
greater than this value, maxFrameLength, int TooLongFrameException will be
thrown.the offset of the length field lengthFieldOffset, int the length of the length field lengthFieldLength)Creates a new instance. |
| public | LengthFieldBasedFrameDecoder(int
the maximum length of the frame. If the length of the frame is
greater than this value, maxFrameLength, int TooLongFrameException will be
thrown.the offset of the length field lengthFieldOffset, int the length of the length field lengthFieldLength, int the compensation value to add to the value of the length field lengthAdjustment, int the number of first bytes to strip out from the decoded frame initialBytesToStrip)Creates a new instance. |
| public | LengthFieldBasedFrameDecoder(int
the maximum length of the frame. If the length of the frame is
greater than this value, maxFrameLength, int TooLongFrameException will be
thrown.the offset of the length field lengthFieldOffset, int the length of the length field lengthFieldLength, int the compensation value to add to the value of the length field lengthAdjustment, int the number of first bytes to strip out from the decoded frame initialBytesToStrip, boolean If true, a failFast)TooLongFrameException is thrown as
soon as the decoder notices the length of the frame will exceed
maxFrameLength regardless of whether the entire frame
has been read. If false, a TooLongFrameException
is thrown after the entire frame that exceeds maxFrameLength
has been read.Creates a new instance. |
| public | LengthFieldBasedFrameDecoder(ByteOrder
the byteOrder, int ByteOrder of the length fieldthe maximum length of the frame. If the length of the frame is
greater than this value, maxFrameLength, int TooLongFrameException will be
thrown.the offset of the length field lengthFieldOffset, int the length of the length field lengthFieldLength, int the compensation value to add to the value of the length field lengthAdjustment, int the number of first bytes to strip out from the decoded frame initialBytesToStrip, boolean If true, a failFast)TooLongFrameException is thrown as
soon as the decoder notices the length of the frame will exceed
maxFrameLength regardless of whether the entire frame
has been read. If false, a TooLongFrameException
is thrown after the entire frame that exceeds maxFrameLength
has been read.Creates a new instance. |
| Modifier and Type | Method and Description |
|---|---|
| protected final void | decode(ChannelHandlerContext
the ctx, ByteBuf ChannelHandlerContext which this ByteToMessageDecoder belongs tothe in, List<Object> ByteBuf from which to read datathe out)List to which decoded messages should be addedImplements abstract io. ByteBuf to an other.
|
| protected Object | decode(ChannelHandlerContext
the ctx, ByteBuf ChannelHandlerContext which this ByteToMessageDecoder belongs tothe in)ByteBuf from which to read dataCreate a frame out of the |
| private void | |
| private void | |
| protected ByteBuf | extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length)
Extract the sub-region of the specified buffer. |
| private void | |
| private void | |
| private static void | |
| private static void | failOnFrameLengthLessThanLengthFieldEndOffset(ByteBuf in, long frameLength, int lengthFieldEndOffset)
|
| private static void | |
| protected long | getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order)
Decodes the specified region of the buffer into an unadjusted frame length. |
| byteOrder | back to summary |
|---|---|
| private final ByteOrder byteOrder | |
| bytesToDiscard | back to summary |
|---|---|
| private long bytesToDiscard | |
| discardingTooLongFrame | back to summary |
|---|---|
| private boolean discardingTooLongFrame | |
| failFast | back to summary |
|---|---|
| private final boolean failFast | |
| frameLengthInt | back to summary |
|---|---|
| private int frameLengthInt | |
| initialBytesToStrip | back to summary |
|---|---|
| private final int initialBytesToStrip | |
| lengthAdjustment | back to summary |
|---|---|
| private final int lengthAdjustment | |
| lengthFieldEndOffset | back to summary |
|---|---|
| private final int lengthFieldEndOffset | |
| lengthFieldLength | back to summary |
|---|---|
| private final int lengthFieldLength | |
| lengthFieldOffset | back to summary |
|---|---|
| private final int lengthFieldOffset | |
| maxFrameLength | back to summary |
|---|---|
| private final int maxFrameLength | |
| tooLongFrameLength | back to summary |
|---|---|
| private long tooLongFrameLength | |
| LengthFieldBasedFrameDecoder | back to summary |
|---|---|
| public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) Creates a new instance.
| |
| LengthFieldBasedFrameDecoder | back to summary |
|---|---|
| public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) Creates a new instance.
| |
| LengthFieldBasedFrameDecoder | back to summary |
|---|---|
| public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) Creates a new instance.
| |
| LengthFieldBasedFrameDecoder | back to summary |
|---|---|
| public LengthFieldBasedFrameDecoder(ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) Creates a new instance.
| |
| decode | back to summary |
|---|---|
| protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception Implements abstract io. Doc from io. Decode the from one
| |
| decode | back to summary |
|---|---|
| protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception Create a frame out of the
| |
| discardingTooLongFrame | back to summary |
|---|---|
| private void discardingTooLongFrame(ByteBuf in) | |
| exceededFrameLength | back to summary |
|---|---|
| private void exceededFrameLength(ByteBuf in, long frameLength) | |
| extractFrame | back to summary |
|---|---|
| protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) Extract the sub-region of the specified buffer. | |
| fail | back to summary |
|---|---|
| private void fail(long frameLength) | |
| failIfNecessary | back to summary |
|---|---|
| private void failIfNecessary(boolean firstDetectionOfTooLongFrame) | |
| failOnFrameLengthLessThanInitialBytesToStrip | back to summary |
|---|---|
| private static void failOnFrameLengthLessThanInitialBytesToStrip(ByteBuf in, long frameLength, int initialBytesToStrip) | |
| failOnFrameLengthLessThanLengthFieldEndOffset | back to summary |
|---|---|
| private static void failOnFrameLengthLessThanLengthFieldEndOffset(ByteBuf in, long frameLength, int lengthFieldEndOffset) | |
| failOnNegativeLengthField | back to summary |
|---|---|
| private static void failOnNegativeLengthField(ByteBuf in, long frameLength, int lengthFieldEndOffset) | |
| getUnadjustedFrameLength | back to summary |
|---|---|
| protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) Decodes the specified region of the buffer into an unadjusted frame length. The default implementation is
capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer. Override this method to
decode the length field encoded differently. Note that this method must not modify the state of the specified
buffer (e.g.
| |