Top Description Inners Fields Constructors Methods
io.netty5.handler.codec

public abstract Class ByteToMessageDecoder

extends ChannelHandlerAdapter
Class Inheritance
Known Direct Subclasses
io.netty5.handler.codec.DelimiterBasedFrameDecoder, io.netty5.handler.codec.FixedLengthFrameDecoder, io.netty5.handler.codec.LengthFieldBasedFrameDecoder, io.netty5.handler.codec.LineBasedFrameDecoder, io.netty5.handler.codec.compression.DecompressionHandler
Imports
io.netty5.buffer.Buffer, .BufferAllocator, .CompositeBuffer, io.netty5.channel.ChannelHandler, .ChannelHandlerAdapter, .ChannelHandlerContext, .ChannelOption, .ChannelOutboundInvoker, .ChannelPipeline, .ChannelShutdownDirection, .ReadBufferAllocator, io.netty5.channel.internal.DelegatingChannelHandlerContext, io.netty5.util.Send, io.netty5.util.internal.StringUtil, java.util.Arrays

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()));
        }
    }

Frame detection

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.

Pitfalls

Be aware that sub-classes of ByteToMessageDecoder MUST NOT annotated with @Sharable.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
private static class
public static interface
private static class

Field Summary

Modifier and TypeField and Description
public static final ByteToMessageDecoder.Cumulator
COMPOSITE_CUMULATOR

Cumulate Buffers by add them to a CompositeBuffer and so do no memory copy whenever possible.

private ByteToMessageDecoder.ByteToMessageDecoderContext
private Buffer
private final ByteToMessageDecoder.Cumulator
private final int
private boolean
firedChannelRead

This flag is used to determine if we need to call ChannelOutboundInvoker#read(ReadBufferAllocator) to consume more data when ChannelOption#AUTO_READ is false.

private boolean
public static final ByteToMessageDecoder.Cumulator
MERGE_CUMULATOR

Cumulate Buffers by merge them into one Buffer's, using memory copies.

private int
private boolean

Constructor Summary

AccessConstructor and Description
protected
protected

Method Summary

Modifier and TypeMethod 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.ByteToMessageDecoderContext ctx, Buffer
the Buffer from which to read data
in
)

Called once data should be decoded from the given Buffer.

public void
channelInactive(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.channelInactive.

The Channel of the ChannelHandlerContext was registered is now inactive and reached its end of lifetime.
private void
pack-priv void
channelInputClosed(ByteToMessageDecoder.ByteToMessageDecoderContext ctx)

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.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.
public void
channelReadComplete(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.channelReadComplete.

Invoked when the last message read by the current read operation has been consumed by channelRead(ChannelHandlerContext, Object).
public void
channelShutdown(ChannelHandlerContext
the ChannelHandlerContext for which we notify about the completed shutdown.
ctx
,
ChannelShutdownDirection
the ChannelShutdownDirection of the completed shutdown.
direction
)

Overrides default io.netty5.channel.ChannelHandler.channelShutdown.

The Channel of the ChannelHandlerContext was shutdown in one direction.
protected abstract void
decode(ChannelHandlerContext ctx, Buffer
the Buffer from which to read data
in
)

Decode the from one Buffer to another.

protected void
decodeLast(ChannelHandlerContext ctx, Buffer in)

Is called one last time when the ChannelHandlerContext goes in-active.

pack-priv final void
decodeRemovalReentryProtection(ChannelHandlerContext ctx, Buffer
the Buffer from which to read data
in
)

Decode the from one Buffer to an other.

protected final void
public final void
handlerAdded(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

Gets called after the 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.netty5.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
protected void
handlerRemoved0(ChannelHandlerContext ctx)

Gets called after the ByteToMessageDecoder was removed from the actual context and it doesn't handle events anymore.

protected Buffer

Returns:

Internal Buffer if exists, else null.
internalBuffer
()

Returns the internal cumulative buffer of this decoder, if exists, else null.

public final boolean
isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline.
public boolean
isSingleDecode()

If true then only one message is decoded on each channelRead(ChannelHandlerContext, Object) call.

public void
setSingleDecode(boolean singleDecode)

If set then only one message is decoded on each channelRead(ChannelHandlerContext, Object) call.

Field Detail

COMPOSITE_CUMULATORback to summary
public static final ByteToMessageDecoder.Cumulator COMPOSITE_CUMULATOR

Cumulate Buffers by add them to a CompositeBuffer and so do no memory copy whenever possible. Be aware that CompositeBuffer use a more complex indexing implementation so depending on your use-case and the decoder implementation this may be slower than just use the MERGE_CUMULATOR.

contextback to summary
private ByteToMessageDecoder.ByteToMessageDecoderContext context
cumulationback to summary
private Buffer cumulation
cumulatorback to summary
private final ByteToMessageDecoder.Cumulator cumulator
discardAfterReadsback to summary
private final int discardAfterReads
firedChannelReadback to summary
private boolean firedChannelRead

This flag is used to determine if we need to call ChannelOutboundInvoker#read(ReadBufferAllocator) to consume more data when ChannelOption#AUTO_READ is false.

firstback to summary
private boolean first
MERGE_CUMULATORback to summary
public static final ByteToMessageDecoder.Cumulator MERGE_CUMULATOR

Cumulate Buffers by merge them into one Buffer's, using memory copies.

numReadsback to summary
private int numReads
singleDecodeback to summary
private boolean singleDecode

Constructor Detail

ByteToMessageDecoderback to summary
protected ByteToMessageDecoder()
ByteToMessageDecoderback to summary
protected ByteToMessageDecoder(ByteToMessageDecoder.Cumulator cumulator)

Method Detail

actualReadableBytesback 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 internalBuffer().readableBytes().

callDecodeback to summary
pack-priv void callDecode(ByteToMessageDecoder.ByteToMessageDecoderContext ctx, Buffer in)

Called once data should be decoded from the given Buffer. This method will call decode(ChannelHandlerContext, Buffer) as long as decoding should take place.

Parameters
ctx:ByteToMessageDecoder.ByteToMessageDecoderContext

the ChannelHandlerContext which this ByteToMessageDecoder belongs to

in:Buffer

the Buffer from which to read data

channelInactiveback to summary
public void channelInactive(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelInactive.

Doc from io.netty5.channel.ChannelHandler.channelInactive.

The Channel of the ChannelHandlerContext was registered is now inactive and reached its end of lifetime.

Annotations
@Override
channelInputClosedback to summary
private void channelInputClosed(ByteToMessageDecoder.ByteToMessageDecoderContext ctx, boolean callChannelInactive)
channelInputClosedback to summary
pack-priv void channelInputClosed(ByteToMessageDecoder.ByteToMessageDecoderContext ctx) throws Exception

Called when the input of the channel was closed which may be because it changed to inactive or because of shutdown.

channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Doc from io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.

Annotations
@Override
channelReadCompleteback to summary
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelReadComplete.

Doc from io.netty5.channel.ChannelHandler.channelReadComplete.

Invoked when the last message read by the current read operation has been consumed by channelRead(ChannelHandlerContext, Object). If ChannelOption#AUTO_READ is off, no further attempt to read an inbound data from the current Channel will be made until ChannelOutboundInvoker#read(ReadBufferAllocator) is called.

Annotations
@Override
channelShutdownback to summary
public void channelShutdown(ChannelHandlerContext ctx, ChannelShutdownDirection direction) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelShutdown.

Doc from io.netty5.channel.ChannelHandler.channelShutdown.

The Channel of the ChannelHandlerContext was shutdown in one direction. This might either be because the remote peer did cause a shutdown of one direction or the shutdown was requested explicit by us and was executed.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which we notify about the completed shutdown.

direction:ChannelShutdownDirection

the ChannelShutdownDirection of the completed shutdown.

Annotations
@Override
decodeback to summary
protected abstract void decode(ChannelHandlerContext ctx, Buffer in) throws Exception

Decode the from one Buffer to another. This method will be called till either the input Buffer has nothing to read when return from this method or till nothing was read from the input Buffer.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this ByteToMessageDecoder belongs to

in:Buffer

the Buffer from which to read data

Exceptions
Exception:
is thrown if an error occurs
decodeLastback to summary
protected void decodeLast(ChannelHandlerContext ctx, Buffer in) throws Exception

Is called one last time when the ChannelHandlerContext goes in-active. Which means the channelInactive(ChannelHandlerContext) was triggered. By default this will just call decode(ChannelHandlerContext, Buffer) but sub-classes may override this for some special cleanup operation.

decodeRemovalReentryProtectionback to summary
pack-priv final void decodeRemovalReentryProtection(ChannelHandlerContext ctx, Buffer in) throws Exception

Decode the from one Buffer to an other. This method will be called till either the input Buffer has nothing to read when return from this method or till nothing was read from the input Buffer.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this ByteToMessageDecoder belongs to

in:Buffer

the Buffer from which to read data

Exceptions
Exception:
is thrown if an error occurs
discardSomeReadBytesback to summary
protected final void discardSomeReadBytes()
handlerAddedback to summary
public final void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

Doc from io.netty5.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
handlerAdded0back to summary
protected void handlerAdded0(ChannelHandlerContext ctx) throws Exception
handlerRemovedback to summary
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.handlerRemoved.

Doc from io.netty5.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
handlerRemoved0back to summary
protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception

Gets called after the ByteToMessageDecoder was removed from the actual context and it doesn't handle events anymore.

internalBufferback to summary
protected Buffer internalBuffer()

Returns the internal cumulative buffer of this decoder, if exists, else null. 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.

Returns:Buffer

Internal Buffer if exists, else null.

isSharableback to summary
public final boolean isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Doc from io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline. By default, this method returns false. If this method returns false, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.

Annotations
@Override
isSingleDecodeback to summary
public boolean isSingleDecode()

If true then only one message is decoded on each channelRead(ChannelHandlerContext, Object) call. Default is false as this has performance impacts.

setSingleDecodeback to summary
public void setSingleDecode(boolean singleDecode)

If set then only one message is decoded on each channelRead(ChannelHandlerContext, Object) call. This may be useful if you need to do some protocol upgrade and want to make sure nothing is mixed up. Default is false as this has performance impacts.

io.netty5.handler.codec back to summary

pack-priv final Class ByteToMessageDecoder.ByteToMessageDecoderContext

extends DelegatingChannelHandlerContext
Class Inheritance

Field Summary

Modifier and TypeField and Description
private int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public ChannelHandlerContext
pack-priv int
pack-priv void
Inherited from io.netty5.channel.internal.DelegatingChannelHandlerContext:
bindbufferAllocatorchannelcloseconnectconnectdelegatingCtxderegisterdisconnectexecutorfireChannelActivefireChannelExceptionCaughtfireChannelInactivefireChannelInboundEventfireChannelReadCompletefireChannelRegisteredfireChannelShutdownfireChannelUnregisteredfireChannelWritabilityChangedflushhandlerisRemovednamenewFailedFuturenewPromisenewSucceededFuturenewSucceededFuturepipelinereadreadregistersendOutboundEventshutdownwritewriteAndFlush

Field Detail

fireChannelReadCalledback to summary
private int fireChannelReadCalled

Constructor Detail

ByteToMessageDecoderContextback to summary
private ByteToMessageDecoderContext(ChannelHandlerContext ctx)

Method Detail

fireChannelReadback to summary
public ChannelHandlerContext fireChannelRead(Object msg)

Overrides io.netty5.channel.internal.DelegatingChannelHandlerContext.fireChannelRead.

Implements io.netty5.channel.ChannelHandlerContext.fireChannelRead.

Doc from io.netty5.channel.ChannelInboundInvoker.fireChannelRead.

A Channel received a message. This will result in having the ChannelHandler#channelRead(ChannelHandlerContext, Object) method called of the next ChannelHandler contained in the ChannelPipeline of the Channel.

Annotations
@Override
fireChannelReadCallCountback to summary
pack-priv int fireChannelReadCallCount()
resetback to summary
pack-priv void reset()
io.netty5.handler.codec back to summary

private final Class ByteToMessageDecoder.CompositeBufferCumulator

extends Object
implements Cumulator
Class Inheritance
  • java.lang.Object
  • io.netty5.handler.codec.ByteToMessageDecoder.CompositeBufferCumulator
All Implemented Interfaces
io.netty5.handler.codec.ByteToMessageDecoder.Cumulator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Buffer
cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in)

Implements io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.cumulate.

Cumulate the given 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.netty5.handler.codec.ByteToMessageDecoder.Cumulator.discardSomeReadBytes.

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.
private static Send<Buffer>
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Constructor Detail

CompositeBufferCumulatorback to summary
private CompositeBufferCumulator()

Method Detail

cumulateback to summary
public Buffer cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in)

Implements io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.cumulate.

Doc from io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.cumulate.

Cumulate the given Buffers and return the Buffer that holds the cumulated bytes. The implementation is responsible to correctly handle the life-cycle of the given Buffers and so call Buffer#close() if a Buffer is fully consumed.

Annotations
@Override
discardSomeReadBytesback to summary
public Buffer discardSomeReadBytes(Buffer cumulation)

Implements io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.discardSomeReadBytes.

Doc from io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.discardSomeReadBytes.

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.

Parameters
cumulation:Buffer

The buffer we wish to trim already processed bytes from.

Returns:Buffer

A buffer where the bytes before the reader-offset have been removed.

Annotations
@Override
prepareInForComposeback to summary
private static Send<Buffer> prepareInForCompose(Buffer in)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
io.netty5.handler.codec back to summary

public Interface ByteToMessageDecoder.Cumulator

Known Direct Implementers
io.netty5.handler.codec.ByteToMessageDecoder.CompositeBufferCumulator, io.netty5.handler.codec.ByteToMessageDecoder.MergeCumulator

Cumulate Buffers.

Method Summary

Modifier and TypeMethod and Description
public Buffer
cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in)

Cumulate the given Buffers and return the Buffer that holds the cumulated bytes.

public Buffer

Returns:

A buffer where the bytes before the reader-offset have been removed.
discardSomeReadBytes
(Buffer
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.

Method Detail

cumulateback to summary
public Buffer cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in)

Cumulate the given Buffers and return the Buffer that holds the cumulated bytes. The implementation is responsible to correctly handle the life-cycle of the given Buffers and so call Buffer#close() if a Buffer is fully consumed.

discardSomeReadBytesback 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.

Parameters
cumulation:Buffer

The buffer we wish to trim already processed bytes from.

Returns:Buffer

A buffer where the bytes before the reader-offset have been removed.

io.netty5.handler.codec back to summary

private final Class ByteToMessageDecoder.MergeCumulator

extends Object
implements Cumulator
Class Inheritance
All Implemented Interfaces
io.netty5.handler.codec.ByteToMessageDecoder.Cumulator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Buffer
cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in)

Implements io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.cumulate.

Cumulate the given 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.netty5.handler.codec.ByteToMessageDecoder.Cumulator.discardSomeReadBytes.

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.
private static Buffer
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Constructor Detail

MergeCumulatorback to summary
private MergeCumulator()

Method Detail

cumulateback to summary
public Buffer cumulate(BufferAllocator alloc, Buffer cumulation, Buffer in)

Implements io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.cumulate.

Doc from io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.cumulate.

Cumulate the given Buffers and return the Buffer that holds the cumulated bytes. The implementation is responsible to correctly handle the life-cycle of the given Buffers and so call Buffer#close() if a Buffer is fully consumed.

Annotations
@Override
discardSomeReadBytesback to summary
public Buffer discardSomeReadBytes(Buffer cumulation)

Implements io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.discardSomeReadBytes.

Doc from io.netty5.handler.codec.ByteToMessageDecoder.Cumulator.discardSomeReadBytes.

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.

Parameters
cumulation:Buffer

The buffer we wish to trim already processed bytes from.

Returns:Buffer

A buffer where the bytes before the reader-offset have been removed.

Annotations
@Override
expandCumulationAndWriteback to summary
private static Buffer expandCumulationAndWrite(BufferAllocator alloc, Buffer oldCumulation, Buffer in)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override