- 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 ofByteToMessageDecoder
andMessageToByteEncoder
. Be aware that sub-classes ofByteToMessageCodec
MUST NOT annotated with {@link @Sharable}.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ByteToMessageCodec()
seeByteToMessageCodec(BufferAllocator)
withtrue
as boolean parameter.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.protected
ByteToMessageCodec(Class<? extends I> outboundMessageType)
seeByteToMessageCodec(Class, BufferAllocator)
withtrue
as boolean value.protected
ByteToMessageCodec(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 boolean
acceptOutboundMessage(Object msg)
Returnstrue
if and only if the specified message can be encoded by this codec.void
channelInactive(ChannelHandlerContext ctx)
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.void
channelRead(ChannelHandlerContext ctx, Object msg)
Invoked when the currentChannel
has read a message from the peer.void
channelReadComplete(ChannelHandlerContext ctx)
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
.protected abstract void
decode(ChannelHandlerContext ctx, Buffer in)
protected void
decodeLast(ChannelHandlerContext ctx, Buffer in)
protected abstract void
encode(ChannelHandlerContext ctx, I msg, Buffer out)
void
handlerAdded(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was added to the actual context and it's ready to handle events.void
handlerRemoved(ChannelHandlerContext ctx)
Gets called after theChannelHandler
was removed from the actual context and it doesn't handle events anymore.boolean
isSharable()
Returnstrue
if 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)
withtrue
as boolean parameter.
-
ByteToMessageCodec
protected ByteToMessageCodec(Class<? extends I> outboundMessageType)
seeByteToMessageCodec(Class, BufferAllocator)
withtrue
as 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:ChannelHandler
Returnstrue
if 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
Returnstrue
if 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:ChannelHandler
Invoked when the currentChannel
has read a message from the peer.- Throws:
Exception
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandler
Called once a write operation is made. The write operation will write the messages through theChannelPipeline
. Those are then ready to be flushed to the actualChannel
onceChannel.flush()
is called.- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to write- Returns:
- the
Future
which will be notified once the operation completes.
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Invoked when the last message read by the current read operation has been consumed byChannelHandler.channelRead(ChannelHandlerContext, Object)
. IfChannelOption.AUTO_READ
is off, no further attempt to read an inbound data from the currentChannel
will be made untilChannelHandlerContext.read()
is called.- Throws:
Exception
-
channelInactive
public void channelInactive(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
TheChannel
of theChannelHandlerContext
was registered is now inactive and reached its end of lifetime.- Throws:
Exception
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandler
Gets called after theChannelHandler
was 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:ChannelHandler
Gets called after theChannelHandler
was 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)
-
-