Package io.netty.handler.codec
Class MessageToByteEncoder<I>
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelOutboundHandlerAdapter
-
- io.netty.handler.codec.MessageToByteEncoder<I>
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelOutboundHandler
- Direct Known Subclasses:
CompatibleObjectEncoder,ObjectEncoder
public abstract class MessageToByteEncoder<I> extends io.netty.channel.ChannelOutboundHandlerAdapterChannelOutboundHandlerAdapterwhich encodes message in a stream-like fashion from one message to anByteBuf. Example implementation which encodesIntegers to aByteBuf.public class IntegerEncoder extendsMessageToByteEncoder<Integer> {@Overridepublic void encode(ChannelHandlerContextctx,Integermsg,ByteBufout) throwsException{ out.writeInt(msg); } }
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageToByteEncoder()seeMessageToByteEncoder(boolean)withtrueas boolean parameter.protectedMessageToByteEncoder(boolean preferDirect)Create a new instance which will try to detect the types to match out of the type parameter of the class.protectedMessageToByteEncoder(Class<? extends I> outboundMessageType)seeMessageToByteEncoder(Class, boolean)withtrueas boolean value.protectedMessageToByteEncoder(Class<? extends I> outboundMessageType, boolean preferDirect)Create a new instance
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanacceptOutboundMessage(Object msg)Returnstrueif the given message should be handled.protected io.netty.buffer.ByteBufallocateBuffer(io.netty.channel.ChannelHandlerContext ctx, I msg, boolean preferDirect)Allocate aByteBufwhich will be used as argument of#encode(ChannelHandlerContext, I, ByteBuf).protected abstract voidencode(io.netty.channel.ChannelHandlerContext ctx, I msg, io.netty.buffer.ByteBuf out)Encode a message into aByteBuf.protected booleanisPreferDirect()voidwrite(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)-
Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
-
-
-
-
Constructor Detail
-
MessageToByteEncoder
protected MessageToByteEncoder()
seeMessageToByteEncoder(boolean)withtrueas boolean parameter.
-
MessageToByteEncoder
protected MessageToByteEncoder(Class<? extends I> outboundMessageType)
seeMessageToByteEncoder(Class, boolean)withtrueas boolean value.
-
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.- Parameters:
preferDirect-trueif a directByteBufshould be tried to be used as target for the encoded messages. Iffalseis used it will allocate a heapByteBuf, which is backed by an byte array.
-
MessageToByteEncoder
protected MessageToByteEncoder(Class<? extends I> outboundMessageType, boolean preferDirect)
Create a new instance- Parameters:
outboundMessageType- The type of messages to matchpreferDirect-trueif a directByteBufshould be tried to be used as target for the encoded messages. Iffalseis used it will allocate a heapByteBuf, which is backed by an byte array.
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg) throws Exception
Returnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin theChannelPipeline.- Throws:
Exception
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
writein interfaceio.netty.channel.ChannelOutboundHandler- Overrides:
writein classio.netty.channel.ChannelOutboundHandlerAdapter- Throws:
Exception
-
allocateBuffer
protected io.netty.buffer.ByteBuf allocateBuffer(io.netty.channel.ChannelHandlerContext ctx, I msg, boolean preferDirect) throws ExceptionAllocate aByteBufwhich will be used as argument of#encode(ChannelHandlerContext, I, ByteBuf). Sub-classes may override this method to returnByteBufwith a perfect matchinginitialCapacity.- Throws:
Exception
-
encode
protected abstract void encode(io.netty.channel.ChannelHandlerContext ctx, I msg, io.netty.buffer.ByteBuf out) throws ExceptionEncode a message into aByteBuf. This method will be called for each written message that can be handled by this encoder.- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToByteEncoderbelongs tomsg- the message to encodeout- theByteBufinto which the encoded message will be written- Throws:
Exception- is thrown if an error occurs
-
isPreferDirect
protected boolean isPreferDirect()
-
-