public abstract class MessageToByteEncoder<I> extends ChannelOutboundHandlerAdapter
ChannelOutboundHandlerAdapter从一条消息到ByteBuf以类似于流的方式编码消息。
示例实施方式,其编码Integer s到一个ByteBuf 。
public class IntegerEncoder extends MessageToByteEncoder<Integer> {
@Override
public void encode(ChannelHandlerContext ctx, Integer msg, ByteBuf out)
throws 异常 {
out.writeInt(msg);
}
}
ChannelHandler.Sharable| Modifier | Constructor and Description |
|---|---|
protected |
MessageToByteEncoder()
参见
MessageToByteEncoder(boolean)和
true作为布尔参数。
|
protected |
MessageToByteEncoder(boolean preferDirect)
创建一个新实例,它将尝试检测类型以匹配该类的类型参数。
|
protected |
MessageToByteEncoder(java.lang.Class<? extends I> outboundMessageType)
请参阅
MessageToByteEncoder(Class, boolean)和
true作为布尔值。
|
protected |
MessageToByteEncoder(java.lang.Class<? extends I> outboundMessageType, boolean preferDirect)
创建一个新的实例
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
acceptOutboundMessage(java.lang.Object msg)
如果应该处理给定的消息,则返回
true 。
|
protected ByteBuf |
allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect)
分配 ByteBuf ,其将被用作参数的#encode(ChannelHandlerContext, I, ByteBuf) 。
|
protected abstract void |
encode(ChannelHandlerContext ctx, I msg, ByteBuf out)
将消息编码成 ByteBuf 。
|
protected boolean |
isPreferDirect() |
void |
write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)
|
bind, close, connect, deregister, disconnect, flush, readensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaught, handlerAdded, handlerRemovedprotected MessageToByteEncoder()
MessageToByteEncoder(boolean)和
true作为布尔参数。
protected MessageToByteEncoder(java.lang.Class<? extends I> outboundMessageType)
MessageToByteEncoder(Class, boolean)和
true作为布尔值。
protected MessageToByteEncoder(boolean preferDirect)
public boolean acceptOutboundMessage(java.lang.Object msg)
throws java.lang.Exception
true 。
如果false将传递到ChannelPipeline中的下一个ChannelOutboundHandler 。
java.lang.Exception
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.write(Object, ChannelPromise)转发到ChannelPipeline中的下一个ChannelOutboundHandler 。
子类可以重写此方法来更改行为。
write在界面
ChannelOutboundHandler
write在课堂上
ChannelOutboundHandlerAdapter
ctx - 为其写入操作的ChannelHandlerContext
msg - 写信息
promise - 在操作完成后通知ChannelPromise
java.lang.Exception - 发生错误时抛出
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) throws java.lang.Exception
ByteBuf ,它将用作#encode(ChannelHandlerContext, I, ByteBuf)参数。
子类可以覆盖此方法以返回ByteBuf,并具有完美匹配initialCapacity 。
java.lang.Exception
protected abstract void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws java.lang.Exception
ByteBuf 。
这个方法将被编码器处理的每个书面信息调用。
ctx - 这MessageToByteEncoder所属的ChannelHandlerContext
msg - 要编码的消息
out - 将编写消息写入其中的ByteBuf
java.lang.Exception - 发生错误时抛出
protected boolean isPreferDirect()
Copyright © 2008–2018 The Netty Project. All rights reserved.