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
ChannelOutboundHandlerAdapter which encodes message in a stream-like fashion from one message to an
ByteBuf.
Example implementation which encodes Integers to a ByteBuf.
public class IntegerEncoder extends MessageToByteEncoder<Integer> {
@Override
public void encode(ChannelHandlerContext ctx, Integer msg, ByteBuf out)
throws Exception {
out.writeInt(msg);
}
}
-
Nested Class Summary
Nested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.SharableModifier and TypeInterfaceDescriptionstatic @interfaceIndicates that the same instance of the annotatedChannelHandlercan be added to one or moreChannelPipelines multiple times without a race condition. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedseeMessageToByteEncoder(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
Modifier and TypeMethodDescriptionbooleanReturnstrueif the given message should be handled.protected ByteBufallocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) Allocate aByteBufwhich will be used as argument ofencode(ChannelHandlerContext, I, ByteBuf).protected abstract voidencode(ChannelHandlerContext ctx, I msg, ByteBuf out) Encode a message into aByteBuf.protected booleanvoidwrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.Methods inherited from class ChannelOutboundHandlerAdapter
bind, close, connect, deregister, disconnect, flush, readModifier and TypeMethodDescriptionvoidbind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) CallsChannelOutboundInvoker.bind(SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidclose(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidconnect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) CallsChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidderegister(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.deregister(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voiddisconnect(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.disconnect(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.read()to forward to the nextChannelOutboundHandlerin theChannelPipeline.Methods inherited from class ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharableModifier and TypeMethodDescriptionprotected voidvoidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) Deprecated.voidDo nothing by default, sub-classes may override this method.voidDo nothing by default, sub-classes may override this method.booleanReturntrueif the implementation isChannelHandler.Sharableand so can be added to differentChannelPipelines.
-
Constructor Details
-
MessageToByteEncoder
protected MessageToByteEncoder()seeMessageToByteEncoder(boolean)withtrueas boolean parameter. -
MessageToByteEncoder
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
Create a new instance
-
-
Method Details
-
acceptOutboundMessage
Returnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelOutboundHandlerin theChannelPipeline.- Throws:
Exception
-
write
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:
Exception- thrown if an error occurs
-
allocateBuffer
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) throws Exception Allocate aByteBufwhich will be used as argument ofencode(ChannelHandlerContext, I, ByteBuf). Sub-classes may override this method to returnByteBufwith a perfect matchinginitialCapacity.- Throws:
Exception
-
encode
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:
Exception- is thrown if an error occurs
-
isPreferDirect
protected boolean isPreferDirect()
-
ChannelInboundHandler