ChannelHandler which decodes bytes in a stream-like fashion from one Buffer to an
other Message type.
For example here is an implementation which reads all readable bytes from
the input Buffer, creates a new Buffer and forward it to the next ChannelHandler
in the ChannelPipeline.
public class SquareDecoder extends ByteToMessageDecoder {
@Override
public void decode(ChannelHandlerContext ctx, Buffer in)
throws Exception {
ctx.fireChannelRead(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 Buffer#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 Buffer#getInt(int).
One MUST use the reader index when using methods like Buffer#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.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| private static class | |
| public static interface | ByteToMessageDecoder.
Cumulate |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| public static final ByteToMessageDecoder. | COMPOSITE_CUMULATOR
Cumulate |
| private ByteToMessageDecoder. | |
| private Buffer | |
| private final ByteToMessageDecoder. | |
| private final 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 |
| Access | Constructor and Description |
|---|---|
| protected | |
| 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. |
| pack-priv void | callDecode(ByteToMessageDecoder.
the ctx,ChannelHandlerContext which this ByteToMessageDecoder belongs tothe in)Buffer from which to read dataCalled once data should be decoded from the given |
| public void | channelInactive(ChannelHandlerContext ctx)
Overrides default io. Channel of the ChannelHandlerContext was registered is now inactive and reached its
end of lifetime.
|
| private void | channelInputClosed(ByteToMessageDecoder.
|
| pack-priv void | channelInputClosed(ByteToMessageDecoder.
Called when the input of the channel was closed which may be because it changed to inactive or because of shutdown. |
| public void | channelRead(ChannelHandlerContext ctx, Object msg)
Overrides default io. Channel has read a message from the peer.
|
| public void | channelReadComplete(ChannelHandlerContext ctx)
Overrides default io. channelRead(ChannelHandlerContext, Object).
|
| public void | channelShutdown(ChannelHandlerContext
the ctx, ChannelShutdownDirection ChannelHandlerContext for which we notify about the completed shutdown.the direction)ChannelShutdownDirection of the completed shutdown.Overrides default io. Channel of the ChannelHandlerContext was shutdown in one direction.
|
| protected abstract void | decode(ChannelHandlerContext
the ctx, Buffer ChannelHandlerContext which this ByteToMessageDecoder belongs tothe in)Buffer from which to read dataDecode the from one |
| protected void | decodeLast(ChannelHandlerContext ctx, Buffer in)
Is called one last time when the |
| pack-priv final void | decodeRemovalReentryProtection(ChannelHandlerContext
the ctx, Buffer ChannelHandlerContext which this ByteToMessageDecoder belongs tothe in)Buffer from which to read dataDecode the from one |
| protected final void | |
| public final void | handlerAdded(ChannelHandlerContext ctx)
Overrides default io. ChannelHandler was added to the actual context and it's ready to handle events.
|
| protected void | |
| public final void | handlerRemoved(ChannelHandlerContext ctx)
Overrides default 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 Buffer | |
| public final boolean | isSharable()
Overrides default io. true if this handler is sharable and thus can be added
to more than one ChannelPipeline.
|
| public boolean | isSingleDecode()
If |
| public void | setSingleDecode(boolean singleDecode)
If set then only one message is decoded on each |
| COMPOSITE_CUMULATOR | back to summary |
|---|---|
| public static final ByteToMessageDecoder. Cumulate | |
| context | back to summary |
|---|---|
| private ByteToMessageDecoder. | |
| cumulation | back to summary |
|---|---|
| private Buffer cumulation | |
| cumulator | back to summary |
|---|---|
| private final ByteToMessageDecoder. | |
| discardAfterReads | back to summary |
|---|---|
| private final 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 | |
| singleDecode | back to summary |
|---|---|
| private boolean singleDecode | |
| ByteToMessageDecoder | back to summary |
|---|---|
| protected ByteToMessageDecoder() | |
| ByteToMessageDecoder | back to summary |
|---|---|
| protected ByteToMessageDecoder(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 |
|---|---|
| pack-priv void callDecode(ByteToMessageDecoder. Called once data should be decoded from the given
| |
| channelInactive | back to summary |
|---|---|
| public void channelInactive(ChannelHandlerContext ctx) throws Exception Overrides default io. Doc from io. The
| |
| channelInputClosed | back to summary |
|---|---|
| private void channelInputClosed(ByteToMessageDecoder. | |
| channelInputClosed | back to summary |
|---|---|
| pack-priv void channelInputClosed(ByteToMessageDecoder. Called when the input of the channel was closed which may be because it changed to inactive or because of shutdown. | |
| channelRead | back to summary |
|---|---|
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Overrides default io. Doc from io. Invoked when the current
| |
| channelReadComplete | back to summary |
|---|---|
| public void channelReadComplete(ChannelHandlerContext ctx) throws Exception Overrides default io. Doc from io. Invoked when the last message read by the current read operation has been consumed by
| |
| channelShutdown | back to summary |
|---|---|
| public void channelShutdown(ChannelHandlerContext ctx, ChannelShutdownDirection direction) throws Exception Overrides default io. Doc from io. The
| |
| decode | back to summary |
|---|---|
| protected abstract void decode(ChannelHandlerContext ctx, Buffer in) throws Exception Decode the from one
| |
| decodeLast | back to summary |
|---|---|
| protected void decodeLast(ChannelHandlerContext ctx, Buffer in) throws Exception Is called one last time when the | |
| decodeRemovalReentryProtection | back to summary |
|---|---|
| pack-priv final void decodeRemovalReentryProtection(ChannelHandlerContext ctx, Buffer in) throws Exception Decode the from one
| |
| discardSomeReadBytes | back to summary |
|---|---|
| protected final void discardSomeReadBytes() | |
| handlerAdded | back to summary |
|---|---|
| public final void handlerAdded(ChannelHandlerContext ctx) throws Exception Overrides default io. Doc from io. Gets called after the
| |
| handlerAdded0 | back to summary |
|---|---|
| protected void handlerAdded0(ChannelHandlerContext ctx) throws Exception | |
| handlerRemoved | back to summary |
|---|---|
| public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception Overrides default 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 Buffer internalBuffer() Returns the internal cumulative buffer of this decoder, if exists, else | |
| isSharable | back to summary |
|---|---|
| public final boolean isSharable() Overrides default io. Doc from io. Returns
| |
| isSingleDecode | back to summary |
|---|---|
| public boolean isSingleDecode() If | |
| setSingleDecode | back to summary |
|---|---|
| public void setSingleDecode(boolean singleDecode) If set then only one message is decoded on each | |
| Modifier and Type | Field and Description |
|---|---|
| private int |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public ChannelHandlerContext | fireChannelRead(Object msg)
Overrides io. Implements io. Channel received a message.
|
| pack-priv int | |
| pack-priv void |
| fireChannelReadCalled | back to summary |
|---|---|
| private int fireChannelReadCalled | |
| ByteToMessageDecoderContext | back to summary |
|---|---|
| private ByteToMessageDecoderContext(ChannelHandlerContext ctx) | |
| fireChannelRead | back to summary |
|---|---|
| public ChannelHandlerContext fireChannelRead(Object msg) Overrides io. Implements io. Doc from io. A
| |
| fireChannelReadCallCount | back to summary |
|---|---|
| pack-priv int fireChannelReadCallCount() | |
| reset | back to summary |
|---|---|
| pack-priv void reset() | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public Buffer | cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in)
Implements io. Buffers and return the Buffer that holds the cumulated bytes.
|
| public Buffer | discardSomeReadBytes(Buffer
The buffer we wish to trim already processed bytes from. cumulation)Implements io. |
| private static Send | |
| public String |
| CompositeBufferCumulator | back to summary |
|---|---|
| private CompositeBufferCumulator() | |
| cumulate | back to summary |
|---|---|
| public Buffer cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in) Implements io. Doc from io. Cumulate the given
| |
| discardSomeReadBytes | back to summary |
|---|---|
| public Buffer discardSomeReadBytes(Buffer cumulation) Implements io. Doc from io. Consume the given buffer and return a new buffer with the same readable data, but where any data before the read offset may have been removed. The returned buffer may be the same buffer instance as the buffer passed in. | |
| prepareInForCompose | back to summary |
|---|---|
| private static Send | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
Buffers.
| Modifier and Type | Method and Description |
|---|---|
| public Buffer | |
| public Buffer | Returns: A buffer where the bytes before the reader-offset have been removed.The buffer we wish to trim already processed bytes from. cumulation)Consume the given buffer and return a new buffer with the same readable data, but where any data before the read offset may have been removed. |
| cumulate | back to summary |
|---|---|
| public Buffer cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in) Cumulate the given | |
| discardSomeReadBytes | back to summary |
|---|---|
| public Buffer discardSomeReadBytes(Buffer cumulation) Consume the given buffer and return a new buffer with the same readable data, but where any data before the read offset may have been removed. The returned buffer may be the same buffer instance as the buffer passed in. | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public Buffer | cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in)
Implements io. Buffers and return the Buffer that holds the cumulated bytes.
|
| public Buffer | discardSomeReadBytes(Buffer
The buffer we wish to trim already processed bytes from. cumulation)Implements io. |
| private static Buffer | |
| public String |
| MergeCumulator | back to summary |
|---|---|
| private MergeCumulator() | |
| cumulate | back to summary |
|---|---|
| public Buffer cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in) Implements io. Doc from io. Cumulate the given
| |
| discardSomeReadBytes | back to summary |
|---|---|
| public Buffer discardSomeReadBytes(Buffer cumulation) Implements io. Doc from io. Consume the given buffer and return a new buffer with the same readable data, but where any data before the read offset may have been removed. The returned buffer may be the same buffer instance as the buffer passed in. | |
| expandCumulationAndWrite | back to summary |
|---|---|
| private static Buffer expandCumulationAndWrite(BufferAllocator alloc, Buffer oldCumulation, Buffer in) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |