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

public abstract Class MessageToByteEncoder<I>

extends ChannelOutboundHandlerAdapter
Class Inheritance
Known Direct Subclasses
io.netty.handler.codec.compression.BrotliEncoder, io.netty.handler.codec.compression.Bzip2Encoder, io.netty.handler.codec.compression.FastLzFrameEncoder, io.netty.handler.codec.compression.Lz4FrameEncoder, io.netty.handler.codec.compression.LzfEncoder, io.netty.handler.codec.compression.LzmaFrameEncoder, io.netty.handler.codec.compression.SnappyFrameEncoder, io.netty.handler.codec.compression.ZlibEncoder, io.netty.handler.codec.compression.ZstdEncoder, io.netty.handler.codec.marshalling.CompatibleMarshallingEncoder, io.netty.handler.codec.marshalling.MarshallingEncoder, io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender, io.netty.handler.codec.serialization.CompatibleObjectEncoder, io.netty.handler.codec.serialization.ObjectEncoder, io.netty.handler.codec.ByteToMessageCodec.Encoder
Imports
io.netty.buffer.ByteBuf, .Unpooled, io.netty.channel.ChannelHandlerContext, .ChannelOutboundHandler, .ChannelOutboundHandlerAdapter, .ChannelPipeline, .ChannelPromise, io.netty.util.ReferenceCountUtil, io.netty.util.internal.TypeParameterMatcher

ChannelOutboundHandlerAdapter which encodes message in a stream-like fashion from one message to an ByteBuf. Example implementation which encodes Integers to a ByteBuf.
    public class IntegerEncoder extends MessageToByteEncoder<Integer> {
        @Override
        public void encode(ChannelHandlerContext ctx, Integer msg, ByteBuf out)
                throws Exception {
            out.writeInt(msg);
        }
    }

Field Summary

Modifier and TypeField and Description
private final TypeParameterMatcher
private final boolean

Constructor Summary

AccessConstructor and Description
protected
MessageToByteEncoder()

see MessageToByteEncoder(boolean) with true as boolean parameter.

protected
MessageToByteEncoder(Class<? extends I> outboundMessageType)

see MessageToByteEncoder(Class, boolean) with true as boolean value.

protected
MessageToByteEncoder(boolean
true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.
preferDirect
)

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
,
boolean
true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.
preferDirect
)

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 ByteBuf
allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect)

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

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

Encode a message into a ByteBuf.

protected boolean
public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelOutboundHandlerAdapter.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
Inherited from io.netty.channel.ChannelOutboundHandlerAdapter:
bindcloseconnectderegisterdisconnectflushread

Field Detail

matcherback to summary
private final TypeParameterMatcher matcher
preferDirectback to summary
private final boolean preferDirect

Constructor Detail

MessageToByteEncoderback to summary
protected MessageToByteEncoder()

see MessageToByteEncoder(boolean) with true as boolean parameter.

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

see MessageToByteEncoder(Class, boolean) with true as boolean value.

MessageToByteEncoderback to summary
protected MessageToByteEncoder(boolean preferDirect)

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

Parameters
preferDirect:boolean

true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.

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

Create a new instance

Parameters
outboundMessageType:Class<? extends I>

The type of messages to match

preferDirect:boolean

true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.

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 ChannelOutboundHandler in the ChannelPipeline.

allocateBufferback to summary
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) throws Exception

Allocate a ByteBuf which will be used as argument of encode(ChannelHandlerContext, I, ByteBuf). Sub-classes may override this method to return ByteBuf with a perfect matching initialCapacity.

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

Encode a message into a ByteBuf. 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:ByteBuf

the ByteBuf into which the encoded message will be written

Exceptions
Exception:
is thrown if an error occurs
isPreferDirectback to summary
protected boolean isPreferDirect()
writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelOutboundHandlerAdapter.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelOutboundHandlerAdapter.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs