public abstract class MessageToByteEncoder<I> extends 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); } }
ChannelHandler.Sharable
限定符 | 构造器和说明 |
---|---|
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(java.lang.Class<? extends I> outboundMessageType)
see
MessageToByteEncoder(Class, boolean) with true as boolean value. |
protected |
MessageToByteEncoder(java.lang.Class<? extends I> outboundMessageType,
boolean preferDirect)
Create a new instance
|
限定符和类型 | 方法和说明 |
---|---|
boolean |
acceptOutboundMessage(java.lang.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 . |
protected boolean |
isPreferDirect() |
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline . |
bind, close, connect, deregister, disconnect, flush, read
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
exceptionCaught, handlerAdded, handlerRemoved
protected MessageToByteEncoder()
MessageToByteEncoder(boolean)
with true
as boolean parameter.protected MessageToByteEncoder(java.lang.Class<? extends I> outboundMessageType)
MessageToByteEncoder(Class, boolean)
with true
as boolean value.protected MessageToByteEncoder(boolean preferDirect)
protected MessageToByteEncoder(java.lang.Class<? extends I> outboundMessageType, boolean preferDirect)
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.Exception
true
if the given message should be handled. If false
it will be passed to the next
ChannelOutboundHandler
in the ChannelPipeline
.java.lang.Exception
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.write(Object, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
在接口中 ChannelOutboundHandler
write
在类中 ChannelOutboundHandlerAdapter
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesjava.lang.Exception
- thrown if an error occursprotected ByteBuf allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) throws java.lang.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
.java.lang.Exception
protected abstract void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws java.lang.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 writtenjava.lang.Exception
- is thrown if an error occursprotected boolean isPreferDirect()