public abstract class MessageToByteEncoder<I> extends ChannelHandlerAdapter
ChannelHandlerAdapter
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); } }
ChannelHandler.Sharable, ChannelHandler.Skip
Modifier | Constructor and Description |
---|---|
protected |
MessageToByteEncoder() |
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) |
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 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 msg,
ByteBuf out)
Encode a message into a
ByteBuf . |
void |
write(ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise)
Calls
ChannelHandlerContext.write(Object) to forward
to the next ChannelHandler in the ChannelPipeline . |
bind, channelActive, channelInactive, channelRead, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, exceptionCaught, flush, handlerAdded, handlerRemoved, isSharable, read, userEventTriggered
protected MessageToByteEncoder()
#MessageToByteEncoder(boolean)} with {@code true} as boolean parameter.
protected MessageToByteEncoder(Class<? extends I> outboundMessageType)
#MessageToByteEncoder(Class, boolean)} with {@code true} as boolean value.
protected MessageToByteEncoder(boolean preferDirect)
public boolean acceptOutboundMessage(Object msg) throws Exception
true
if the given message should be handled. If false
it will be passed to the next
ChannelHandler
in the ChannelPipeline
.Exception
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
ChannelHandlerAdapter
ChannelHandlerContext.write(Object)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
in interface ChannelHandler
write
in class ChannelHandlerAdapter
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error accourprotected ByteBuf allocateBuffer(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 returna ByteBuf
with a perfect matching initialCapacity
.Exception
protected abstract void encode(ChannelHandlerContext ctx, I msg, 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 accourCopyright © 2008–2015 The Netty Project. All rights reserved.