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:
ChannelHandler,ChannelOutboundHandler
- Direct Known Subclasses:
BrotliEncoder,Bzip2Encoder,CompatibleMarshallingEncoder,CompatibleObjectEncoder,FastLzFrameEncoder,HAProxyMessageEncoder,Lz4FrameEncoder,LzfEncoder,LzmaFrameEncoder,MarshallingEncoder,ObjectEncoder,ProtobufVarint32LengthFieldPrepender,SnappyFrameEncoder,Socks4ClientEncoder,Socks4ServerEncoder,Socks5ClientEncoder,Socks5ServerEncoder,SocksMessageEncoder,TcpDnsQueryEncoder,ZlibEncoder,ZstdEncoder
public abstract class MessageToByteEncoder<I> extends ChannelOutboundHandlerAdapter
ChannelOutboundHandlerAdapterwhich 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); } }
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
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(java.lang.Class<? extends I> outboundMessageType)seeMessageToByteEncoder(Class, boolean)withtrueas boolean value.protectedMessageToByteEncoder(java.lang.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(java.lang.Object msg)Returnstrueif the given message should be handled.protected ByteBufallocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect)Allocate aByteBufwhich will be used as argument of#encode(ChannelHandlerContext, I, ByteBuf).protected abstract voidencode(ChannelHandlerContext ctx, I msg, ByteBuf out)Encode a message into aByteBuf.protected booleanisPreferDirect()voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.-
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
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
exceptionCaught, handlerAdded, handlerRemoved
-
-
-
-
Constructor Detail
-
MessageToByteEncoder
protected MessageToByteEncoder()
seeMessageToByteEncoder(boolean)withtrueas boolean parameter.
-
MessageToByteEncoder
protected MessageToByteEncoder(java.lang.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.
-
MessageToByteEncoder
protected MessageToByteEncoder(java.lang.Class<? extends I> outboundMessageType, boolean preferDirect)
Create a new instance
-
-
Method Detail
-
acceptOutboundMessage
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.ExceptionReturnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin theChannelPipeline.- Throws:
java.lang.Exception
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
allocateBuffer
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) throws java.lang.Exception
Allocate aByteBufwhich will be used as argument of#encode(ChannelHandlerContext, I, ByteBuf). Sub-classes may override this method to returnByteBufwith a perfect matchinginitialCapacity.- Throws:
java.lang.Exception
-
encode
protected abstract void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws java.lang.Exception
Encode 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:
java.lang.Exception- is thrown if an error occurs
-
isPreferDirect
protected boolean isPreferDirect()
-
-