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

public abstract Class MessageToMessageEncoder<I>

extends ChannelHandlerAdapter
Class Inheritance
Known Direct Subclasses
io.netty5.handler.codec.base64.Base64Encoder, io.netty5.handler.codec.bytes.ByteArrayEncoder, io.netty5.handler.codec.string.LineEncoder, io.netty5.handler.codec.string.StringEncoder, io.netty5.handler.codec.DatagramPacketEncoder, io.netty5.handler.codec.LengthFieldPrepender
Imports
io.netty5.channel.ChannelHandler, .ChannelHandlerAdapter, .ChannelHandlerContext, .ChannelPipeline, io.netty5.util.Resource, io.netty5.util.concurrent.Future, .Promise, .PromiseCombiner, io.netty5.util.internal.StringUtil, .TypeParameterMatcher, java.util.List

ChannelHandler which encodes from one message to another message For example here is an implementation which decodes an Integer to an String.
public class IntegerToStringEncoder extends
            MessageToMessageEncoder<Integer> {

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

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

Do not override both.

Field Summary

Modifier and TypeField and Description
private final TypeParameterMatcher

Constructor Summary

AccessConstructor and Description
protected
MessageToMessageEncoder()

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

protected
MessageToMessageEncoder(Class<? extends I>
The type of messages to match and so encode
outboundMessageType
)

Create a new instance

Method Summary

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

Returns true if the given message should be handled.

protected void
encode(ChannelHandlerContext ctx, I
the message to encode to another one.
msg
,
List<Object>
the List into which produced output messages should be added.
out
)

Encode from one message to another.

protected void
encodeAndClose(ChannelHandlerContext ctx, I
the message to encode to another one.
msg
,
List<Object>
the List into which produced output messages should be added.
out
)

Encode from one message to another.

public Future<Void>
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
)

Overrides default io.netty5.channel.ChannelHandler.write.

Called once a write operation is made.
private static void

Field Detail

matcherback to summary
private final TypeParameterMatcher matcher

Constructor Detail

MessageToMessageEncoderback to summary
protected MessageToMessageEncoder()

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

MessageToMessageEncoderback to summary
protected MessageToMessageEncoder(Class<? extends I> outboundMessageType)

Create a new instance

Parameters
outboundMessageType:Class<? extends I>

The type of messages to match and so encode

Method Detail

acceptOutboundMessageback to summary
public boolean acceptOutboundMessage(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.

encodeback to summary
protected void encode(ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception

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

The message will be disposed of after this call.

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

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to.

msg:I

the message to encode to another one.

out:List<Object>

the List into which produced output messages should be added.

Exceptions
Exception:
is thrown if an error occurs.
encodeAndCloseback to summary
protected void encodeAndClose(ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception

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

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 encode(ChannelHandlerContext, Object, List) method.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to.

msg:I

the message to encode to another one.

out:List<Object>

the List into which produced output messages should be added.

Exceptions
Exception:
is thrown if an error occurs.
writeback to summary
public Future<Void> write(ChannelHandlerContext ctx, Object msg)

Overrides default io.netty5.channel.ChannelHandler.write.

Doc from io.netty5.channel.ChannelHandler.write.

Called once a write operation is made. The write operation will write the messages through the ChannelPipeline. Those are then ready to be flushed to the actual Channel once Channel#flush() is called.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the write operation is made

msg:Object

the message to write

Returns:Future<Void>

the Future which will be notified once the operation completes.

Annotations
@Override
writePromiseCombinerback to summary
private static void writePromiseCombiner(ChannelHandlerContext ctx, CodecOutputList out, Promise<Void> promise)