- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.ByteToMessageCodec<I>
-
- All Implemented Interfaces:
ChannelHandler
public abstract class ByteToMessageCodec<I> extends ChannelHandlerAdapter
A Codec for on-the-fly encoding/decoding of bytes to messages and vise-versa. This can be thought of as a combination ofByteToMessageDecoderandMessageToByteEncoder. Be aware that sub-classes ofByteToMessageCodecMUST NOT annotated with {@link @Sharable}.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedByteToMessageCodec()seeByteToMessageCodec(BufferAllocator)withtrueas boolean parameter.protectedByteToMessageCodec(BufferAllocator allocator)Create a new instance which will try to detect the types to match out of the type parameter of the class.protectedByteToMessageCodec(Class<? extends I> outboundMessageType)seeByteToMessageCodec(Class, BufferAllocator)withtrueas boolean value.protectedByteToMessageCodec(Class<? extends I> outboundMessageType, BufferAllocator allocator)Create a new instance
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanacceptOutboundMessage(Object msg)Returnstrueif and only if the specified message can be encoded by this codec.voidchannelInactive(ChannelHandlerContext ctx)TheChannelof theChannelHandlerContextwas registered is now inactive and reached its end of lifetime.voidchannelRead(ChannelHandlerContext ctx, Object msg)Invoked when the currentChannelhas read a message from the peer.voidchannelReadComplete(ChannelHandlerContext ctx)Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object).protected abstract voiddecode(ChannelHandlerContext ctx, Buffer in)protected voiddecodeLast(ChannelHandlerContext ctx, Buffer in)protected abstract voidencode(ChannelHandlerContext ctx, I msg, Buffer out)voidhandlerAdded(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas added to the actual context and it's ready to handle events.voidhandlerRemoved(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.booleanisSharable()Returnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline.Future<Void>write(ChannelHandlerContext ctx, Object msg)Called once a write operation is made.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelExceptionCaught, channelInboundEvent, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
ByteToMessageCodec
protected ByteToMessageCodec()
seeByteToMessageCodec(BufferAllocator)withtrueas boolean parameter.
-
ByteToMessageCodec
protected ByteToMessageCodec(Class<? extends I> outboundMessageType)
seeByteToMessageCodec(Class, BufferAllocator)withtrueas boolean value.
-
ByteToMessageCodec
protected ByteToMessageCodec(BufferAllocator allocator)
Create a new instance which will try to detect the types to match out of the type parameter of the class.- Parameters:
allocator- The allocator to use for allocating buffers. Ifnull, the channel context allocator will be used.
-
ByteToMessageCodec
protected ByteToMessageCodec(Class<? extends I> outboundMessageType, BufferAllocator allocator)
Create a new instance- Parameters:
outboundMessageType- The type of messages to match.allocator- The allocator to use for allocating buffers. Ifnull, the channel context allocator will be used.
-
-
Method Detail
-
isSharable
public final boolean isSharable()
Description copied from interface:ChannelHandlerReturnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline. By default, this method returnsfalse. If this method returnsfalse, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg) throws Exception
Returnstrueif and only if the specified message can be encoded by this codec.- Parameters:
msg- the message- Throws:
Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Description copied from interface:ChannelHandlerInvoked when the currentChannelhas read a message from the peer.- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandlerCalled once a write operation is made. The write operation will write the messages through theChannelPipeline. Those are then ready to be flushed to the actualChannelonceChannel.flush()is called.- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to write- Returns:
- the
Futurewhich will be notified once the operation completes.
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerInvoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object). IfChannelOption.AUTO_READis off, no further attempt to read an inbound data from the currentChannelwill be made untilChannelHandlerContext.read()is called.- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerTheChannelof theChannelHandlerContextwas registered is now inactive and reached its end of lifetime.- Throws:
Exception
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas added to the actual context and it's ready to handle events.- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.- Throws:
Exception
-
encode
protected abstract void encode(ChannelHandlerContext ctx, I msg, Buffer out) throws Exception
- Throws:
Exception- See Also:
MessageToByteEncoder.encode(ChannelHandlerContext, Object, Buffer)
-
decode
protected abstract void decode(ChannelHandlerContext ctx, Buffer in) throws Exception
- Throws:
Exception- See Also:
ByteToMessageDecoder.decode(ChannelHandlerContext, Buffer)
-
decodeLast
protected void decodeLast(ChannelHandlerContext ctx, Buffer in) throws Exception
- Throws:
Exception- See Also:
ByteToMessageDecoder.decodeLast(ChannelHandlerContext, Buffer)
-
-