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

public abstract Class MessageToByteEncoder<I>

extends ChannelHandlerAdapter
Class Inheritance
Known Direct Subclasses
io.netty5.handler.codec.ByteToMessageCodec.Encoder
Imports
io.netty5.buffer.Buffer, io.netty5.channel.ChannelHandler, .ChannelHandlerAdapter, .ChannelHandlerContext, .ChannelPipeline, io.netty5.util.concurrent.Future, io.netty5.util.internal.TypeParameterMatcher

ChannelHandler which encodes message in a stream-like fashion from one message to a Buffer.

Example implementation which encodes Integers to a Buffer.

    public class IntegerEncoder extends MessageToByteEncoder<Integer> {
        @Override
        public void encode(ChannelHandlerContext ctx, Integer msg, Buffer out)
                throws Exception {
            out.writeInt(msg);
        }
    }

Field Summary

Modifier and TypeField and Description
private final TypeParameterMatcher

Constructor Summary

AccessConstructor and Description
protected
MessageToByteEncoder()

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

protected
MessageToByteEncoder(Class<? extends I>
The type of messages to match.
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 abstract Buffer
allocateBuffer(ChannelHandlerContext ctx, I
the message to be encoded
msg
)

Allocate a Buffer which will be used as argument of encode(ChannelHandlerContext, I, Buffer).

protected abstract void
encode(ChannelHandlerContext ctx, I
the message to encode
msg
,
Buffer
the Buffer into which the encoded message will be written
out
)

Encode a message into a Buffer.

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.

Field Detail

matcherback to summary
private final TypeParameterMatcher matcher

Constructor Detail

MessageToByteEncoderback to summary
protected MessageToByteEncoder()

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

MessageToByteEncoderback to summary
protected MessageToByteEncoder(Class<? extends I> outboundMessageType)

Create a new instance.

Parameters
outboundMessageType:Class<? extends I>

The type of messages to match.

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.

allocateBufferback to summary
protected abstract Buffer allocateBuffer(ChannelHandlerContext ctx, I msg) throws Exception

Allocate a Buffer which will be used as argument of encode(ChannelHandlerContext, I, Buffer).

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToByteEncoder belongs to

msg:I

the message to be encoded

encodeback to summary
protected abstract void encode(ChannelHandlerContext ctx, I msg, Buffer out) throws Exception

Encode a message into a Buffer. This method will be called for each written message that can be handled by this encoder.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToByteEncoder belongs to

msg:I

the message to encode

out:Buffer

the Buffer into which the encoded message will be written

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