public abstract class MessageToByteEncoder<I>
extends io.netty.channel.ChannelOutboundHandlerAdapter
ChannelOutboundHandlerAdapter
which encodes message in a stream-like fashion from one message to an
ByteBuf
.
Example implementation which encodes Integer
s to a ByteBuf
.
public class IntegerEncoder extendsMessageToByteEncoder
<Integer
> {@Override
public void encode(ChannelHandlerContext
ctx,Integer
msg,ByteBuf
out) throwsException
{ out.writeInt(msg); } }
Modifier | Constructor and Description |
---|---|
protected |
MessageToByteEncoder()
see
MessageToByteEncoder(boolean) with true as boolean parameter. |
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.
|
protected |
MessageToByteEncoder(Class<? extends I> outboundMessageType)
see
MessageToByteEncoder(Class, boolean) with true as boolean value. |
protected |
MessageToByteEncoder(Class<? extends I> outboundMessageType,
boolean preferDirect)
Create a new instance
|
Modifier and Type | Method and Description |
---|---|
boolean |
acceptOutboundMessage(Object msg)
Returns
true if the given message should be handled. |
protected io.netty.buffer.ByteBuf |
allocateBuffer(io.netty.channel.ChannelHandlerContext ctx,
I msg,
boolean preferDirect)
Allocate a
ByteBuf which will be used as argument of #encode(ChannelHandlerContext, I, ByteBuf) . |
protected abstract void |
encode(io.netty.channel.ChannelHandlerContext ctx,
I msg,
io.netty.buffer.ByteBuf out)
Encode a message into a
ByteBuf . |
protected boolean |
isPreferDirect() |
void |
write(io.netty.channel.ChannelHandlerContext ctx,
Object msg,
io.netty.channel.ChannelPromise promise) |
bind, close, connect, deregister, disconnect, flush, read
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
protected MessageToByteEncoder()
MessageToByteEncoder(boolean)
with true
as boolean parameter.protected MessageToByteEncoder(Class<? extends I> outboundMessageType)
MessageToByteEncoder(Class, boolean)
with true
as boolean value.protected MessageToByteEncoder(boolean preferDirect)
preferDirect
- 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.protected MessageToByteEncoder(Class<? extends I> outboundMessageType, boolean preferDirect)
outboundMessageType
- The type of messages to matchpreferDirect
- 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.public boolean acceptOutboundMessage(Object msg) throws Exception
true
if the given message should be handled. If false
it will be passed to the next
ChannelOutboundHandler
in the ChannelPipeline
.Exception
public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception
write
in interface io.netty.channel.ChannelOutboundHandler
write
in class io.netty.channel.ChannelOutboundHandlerAdapter
Exception
protected io.netty.buffer.ByteBuf allocateBuffer(io.netty.channel.ChannelHandlerContext ctx, I msg, boolean preferDirect) throws Exception
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
.Exception
protected abstract void encode(io.netty.channel.ChannelHandlerContext ctx, I msg, io.netty.buffer.ByteBuf out) throws Exception
ByteBuf
. This method will be called for each written message that can be handled
by this encoder.ctx
- the ChannelHandlerContext
which this MessageToByteEncoder
belongs tomsg
- the message to encodeout
- the ByteBuf
into which the encoded message will be writtenException
- is thrown if an error occursprotected boolean isPreferDirect()
Copyright © 2008–2019 The Netty Project. All rights reserved.