ChannelInboundHandlerAdapter which decodes bytes in a stream-like fashion from one ByteBuf to an
other Message type.
For example here is an implementation which reads all readable bytes from
the input ByteBuf and create a new ByteBuf.
public class SquareDecoder extends ByteToMessageDecoder {
@Override
public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
throws Exception {
out.add(in.readBytes(in.readableBytes()));
}
}
Generally frame detection should be handled earlier in the pipeline by adding a
DelimiterBasedFrameDecoder, FixedLengthFrameDecoder, LengthFieldBasedFrameDecoder,
or LineBasedFrameDecoder.
If a custom frame decoder is required, then one needs to be careful when implementing
one with ByteToMessageDecoder. Ensure there are enough bytes in the buffer for a
complete frame by checking ByteBuf#readableBytes(). If there are not enough bytes
for a complete frame, return without modifying the reader index to allow more bytes to arrive.
To check for complete frames without modifying the reader index, use methods like ByteBuf#getInt(int).
One MUST use the reader index when using methods like ByteBuf#getInt(int).
For example calling in.getInt(0) is assuming the frame starts at the beginning of the buffer, which
is not always the case. Use in.getInt(in.readerIndex()) instead.
Be aware that sub-classes of ByteToMessageDecoder MUST NOT
annotated with @Sharable.
Some methods such as ByteBuf#readBytes(int) will cause a memory leak if the returned buffer
is not released or added to the out List. Use derived buffers like ByteBuf#readSlice(int)
to avoid leaking memory.
| Modifier and Type | Class and Description |
|---|---|
| public static interface | ByteToMessageDecoder.Cumulator
Cumulate |
| Modifier and Type | Field and Description |
|---|---|
| public static final ByteToMessageDecoder. | COMPOSITE_CUMULATOR
Cumulate |
| pack-priv ByteBuf | |
| private ByteToMessageDecoder. | |
| private byte | decodeState
A bitmask where the bits are defined as |
| private int | |
| private boolean | firedChannelRead
This flag is used to determine if we need to call |
| private boolean | |
| public static final ByteToMessageDecoder. | MERGE_CUMULATOR
Cumulate |
| private int | |
| private boolean | |
| private boolean | |
| private static final byte | |
| private static final byte | |
| private static final byte |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| protected int | actualReadableBytes()
Returns the actual number of readable bytes in the internal cumulative buffer of this decoder. |
| protected void | callDecode(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 addedCalled once data should be decoded from the given |
| public void | channelInactive(ChannelHandlerContext ctx)
Overrides io. Implements io. ChannelHandlerContext#fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
|
| private void | |
| pack-priv void | channelInputClosed(ChannelHandlerContext ctx, List<Object> out)
Called when the input of the channel was closed which may be because it changed to inactive or because of
|
| public void | channelRead(ChannelHandlerContext ctx, Object msg)
Overrides io. Implements io. ChannelHandlerContext#fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline.
|
| public void | channelReadComplete(ChannelHandlerContext ctx)
Overrides io. Implements io. ChannelHandlerContext#fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline.
|
| protected abstract 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 addedDecode the from one |
| protected void | decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
Is called one last time when the |
| pack-priv final void | decodeRemovalReentryProtection(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 addedDecode the from one |
| protected final void | |
| pack-priv static ByteBuf | |
| pack-priv static void | fireChannelRead(ChannelHandlerContext ctx, List<Object> msgs, int numElements)
Get |
| pack-priv static void | fireChannelRead(ChannelHandlerContext ctx, CodecOutputList msgs, int numElements)
Get |
| public final void | handlerRemoved(ChannelHandlerContext ctx)
Overrides io. Implements io. ChannelHandler was removed from the actual context and it doesn't handle events
anymore.
|
| protected void | handlerRemoved0(ChannelHandlerContext ctx)
Gets called after the |
| protected ByteBuf | |
| public boolean | isSingleDecode()
If |
| public void | setCumulator(ByteToMessageDecoder.
Set the |
| public void | setDiscardAfterReads(int discardAfterReads)
Set the number of reads after which |
| public void | setSingleDecode(boolean singleDecode)
If set then only one message is decoded on each |
| public void | userEventTriggered(ChannelHandlerContext ctx, Object evt)
Overrides io. Implements io. ChannelHandlerContext#fireUserEventTriggered(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline.
|
| COMPOSITE_CUMULATOR | back to summary |
|---|---|
| public static final ByteToMessageDecoder. Cumulate | |
| cumulation | back to summary |
|---|---|
| pack-priv ByteBuf cumulation | |
| cumulator | back to summary |
|---|---|
| private ByteToMessageDecoder. | |
| decodeState | back to summary |
|---|---|
| private byte decodeState A bitmask where the bits are defined as | |
| discardAfterReads | back to summary |
|---|---|
| private int discardAfterReads | |
| firedChannelRead | back to summary |
|---|---|
| private boolean firedChannelRead This flag is used to determine if we need to call | |
| first | back to summary |
|---|---|
| private boolean first | |
| MERGE_CUMULATOR | back to summary |
|---|---|
| public static final ByteToMessageDecoder. Cumulate | |
| numReads | back to summary |
|---|---|
| private int numReads | |
| selfFiredChannelRead | back to summary |
|---|---|
| private boolean selfFiredChannelRead | |
| singleDecode | back to summary |
|---|---|
| private boolean singleDecode | |
| STATE_CALLING_CHILD_DECODE | back to summary |
|---|---|
| private static final byte STATE_CALLING_CHILD_DECODE | |
| STATE_HANDLER_REMOVED_PENDING | back to summary |
|---|---|
| private static final byte STATE_HANDLER_REMOVED_PENDING | |
| STATE_INIT | back to summary |
|---|---|
| private static final byte STATE_INIT | |
| ByteToMessageDecoder | back to summary |
|---|---|
| protected ByteToMessageDecoder() | |
| actualReadableBytes | back to summary |
|---|---|
| protected int actualReadableBytes() Returns the actual number of readable bytes in the internal cumulative
buffer of this decoder. You usually do not need to rely on this value
to write a decoder. Use it only when you must use it at your own risk.
This method is a shortcut to | |
| callDecode | back to summary |
|---|---|
| protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) Called once data should be decoded from the given
| |
| channelInactive | back to summary |
|---|---|
| public void channelInactive(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| channelInputClosed | back to summary |
|---|---|
| private void channelInputClosed(ChannelHandlerContext ctx, boolean callChannelInactive) | |
| channelInputClosed | back to summary |
|---|---|
| pack-priv void channelInputClosed(ChannelHandlerContext ctx, List<Object> out) throws Exception Called when the input of the channel was closed which may be because it changed to inactive or because of
| |
| channelRead | back to summary |
|---|---|
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| channelReadComplete | back to summary |
|---|---|
| public void channelReadComplete(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| decode | back to summary |
|---|---|
| protected abstract void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception Decode the from one
| |
| decodeLast | back to summary |
|---|---|
| protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception Is called one last time when the | |
| decodeRemovalReentryProtection | back to summary |
|---|---|
| pack-priv final void decodeRemovalReentryProtection(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception Decode the from one
| |
| discardSomeReadBytes | back to summary |
|---|---|
| protected final void discardSomeReadBytes() | |
| expandCumulation | back to summary |
|---|---|
| pack-priv static ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf oldCumulation, ByteBuf in) | |
| fireChannelRead | back to summary |
|---|---|
| pack-priv static void fireChannelRead(ChannelHandlerContext ctx, List<Object> msgs, int numElements) Get | |
| fireChannelRead | back to summary |
|---|---|
| pack-priv static void fireChannelRead(ChannelHandlerContext ctx, CodecOutputList msgs, int numElements) Get | |
| handlerRemoved | back to summary |
|---|---|
| public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Gets called after the
| |
| handlerRemoved0 | back to summary |
|---|---|
| protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception Gets called after the | |
| internalBuffer | back to summary |
|---|---|
| protected ByteBuf internalBuffer() Returns the internal cumulative buffer of this decoder. You usually do not need to access the internal buffer directly to write a decoder. Use it only when you must use it at your own risk. | |
| isSingleDecode | back to summary |
|---|---|
| public boolean isSingleDecode() If | |
| setCumulator | back to summary |
|---|---|
| public void setCumulator(ByteToMessageDecoder. Set the | |
| setDiscardAfterReads | back to summary |
|---|---|
| public void setDiscardAfterReads(int discardAfterReads) Set the number of reads after which | |
| setSingleDecode | back to summary |
|---|---|
| public void setSingleDecode(boolean singleDecode) If set then only one message is decoded on each | |
| userEventTriggered | back to summary |
|---|---|
| public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
ByteBufs.
| Modifier and Type | Method and Description |
|---|---|
| public ByteBuf |
| cumulate | back to summary |
|---|---|
| public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) Cumulate the given | |