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

public abstract Class MessageToMessageDecoder<I>

extends ChannelHandlerAdapter
Class Inheritance
Known Direct Subclasses
io.netty5.handler.codec.base64.Base64Decoder, io.netty5.handler.codec.bytes.ByteArrayDecoder, io.netty5.handler.codec.string.StringDecoder, io.netty5.handler.codec.DatagramPacketDecoder, io.netty5.handler.codec.MessageAggregator
Imports
io.netty5.channel.ChannelHandler, .ChannelHandlerAdapter, .ChannelHandlerContext, .ChannelPipeline, io.netty5.util.Resource, io.netty5.util.internal.TypeParameterMatcher

ChannelHandler which decodes from one message to another message. For example here is an implementation which decodes a String to an Integer which represent the length of the String.
public class StringToIntegerDecoder extends
            MessageToMessageDecoder<String> {

        @Override
        public void decode(ChannelHandlerContext ctx, String message,
                           List<Object> out) throws Exception {
            out.add(message.length());
        }
    }
Note that messages passed to decode(ChannelHandlerContext, Object) will be disposed of automatically.

To take control of the message lifetime, you should instead override the decodeAndClose(ChannelHandlerContext, Object) method.

Do not override both.

Field Summary

Modifier and TypeField and Description
private final TypeParameterMatcher

Constructor Summary

AccessConstructor and Description
protected
MessageToMessageDecoder()

Create a new instance which will try to detect the types to match out of the type parameter of the class.

protected
MessageToMessageDecoder(Class<? extends I>
The type of messages to match and so decode
inboundMessageType
)

Create a new instance

Method Summary

Modifier and TypeMethod and Description
public boolean
acceptInboundMessage(Object msg)

Returns true if the given message should be handled.

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.
protected void
decode(ChannelHandlerContext ctx, I
the message to decode to another one
msg
)

Decode from one message to another.

protected void
decodeAndClose(ChannelHandlerContext ctx, I
the message to decode to another one
msg
)

Decode from one message to another.

Field Detail

matcherback to summary
private final TypeParameterMatcher matcher

Constructor Detail

MessageToMessageDecoderback to summary
protected MessageToMessageDecoder()

Create a new instance which will try to detect the types to match out of the type parameter of the class.

MessageToMessageDecoderback to summary
protected MessageToMessageDecoder(Class<? extends I> inboundMessageType)

Create a new instance

Parameters
inboundMessageType:Class<? extends I>

The type of messages to match and so decode

Method Detail

acceptInboundMessageback to summary
public boolean acceptInboundMessage(Object msg) throws Exception

Returns true if the given message should be handled. If false it will be passed to the next ChannelHandler in the ChannelPipeline.

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
decodeback to summary
protected void decode(ChannelHandlerContext ctx, I msg) throws Exception

Decode from one message to another. This method will be called for each written message that can be handled by this decoder.

The message will be disposed of after this call.

Subclasses that wish to sometimes pass messages through, should instead override the decodeAndClose(ChannelHandlerContext, Object) method.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageDecoder belongs to

msg:I

the message to decode to another one

Exceptions
Exception:
is thrown if an error occurs
decodeAndCloseback to summary
protected void decodeAndClose(ChannelHandlerContext ctx, I msg) throws Exception

Decode from one message to another. This method will be called for each written message that can be handled by this decoder.

The message will not be automatically disposed of after this call. Instead, the responsibility of ensuring that messages are disposed of falls upon the implementor of this method.

Subclasses that wish to have incoming messages automatically disposed of should instead override the decodeAndClose(ChannelHandlerContext, Object) method.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageDecoder belongs to

msg:I

the message to decode to another one

Exceptions
Exception:
is thrown if an error occurs