Module io.netty5.codec
Package io.netty5.handler.codec
Class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN>
- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN>
-
- All Implemented Interfaces:
ChannelHandler
- Direct Known Subclasses:
Http2StreamFrameToHttpObjectCodec
,HttpContentEncoder
public abstract class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN> extends ChannelHandlerAdapter
A Codec for on-the-fly encoding/decoding of message. This can be thought of as a combination ofMessageToMessageDecoder
andMessageToMessageEncoder
. Here is an example of aMessageToMessageCodec
which just decode fromInteger
toLong
and encode fromLong
toInteger
.public class NumberCodec extends
Be aware that you need to callMessageToMessageCodec
<Integer
,Long
> {@Override
publicLong
decode(ChannelHandlerContext
ctx,Integer
msg, List<Object> out) throwsException
{ out.add(msg.longValue()); }@Override
publicInteger
encode(ChannelHandlerContext
ctx,Long
msg, List<Object> out) throwsException
{ out.add(msg.intValue()); } }ReferenceCounted.retain()
on messages that are just passed through if they are of typeReferenceCounted
. This is needed as theMessageToMessageCodec
will callReferenceCounted.release()
on encoded / decoded messages.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
MessageToMessageCodec()
Create a new instance which will try to detect the types to decode and encode out of the type parameter of the class.protected
MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType)
Create a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
acceptInboundMessage(Object msg)
Returnstrue
if and only if the specified message can be decoded by this codec.boolean
acceptOutboundMessage(Object msg)
Returnstrue
if and only if the specified message can be encoded by this codec.void
channelRead(ChannelHandlerContext ctx, Object msg)
Invoked when the currentChannel
has read a message from the peer.protected void
decode(ChannelHandlerContext ctx, INBOUND_IN msg)
protected void
decodeAndClose(ChannelHandlerContext ctx, INBOUND_IN msg)
protected void
encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)
protected void
encodeAndClose(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)
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, channelInactive, channelInboundEvent, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, handlerAdded, handlerRemoved, isSharable, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
MessageToMessageCodec
protected MessageToMessageCodec()
Create a new instance which will try to detect the types to decode and encode out of the type parameter of the class.
-
MessageToMessageCodec
protected MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType)
Create a new instance.- Parameters:
inboundMessageType
- The type of messages to decodeoutboundMessageType
- The type of messages to encode
-
-
Method Detail
-
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.
-
acceptInboundMessage
public boolean acceptInboundMessage(Object msg) throws Exception
Returnstrue
if and only if the specified message can be decoded by this codec.- Parameters:
msg
- the message- Throws:
Exception
-
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
-
encode
protected void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception
- Throws:
Exception
- See Also:
MessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)
-
encodeAndClose
protected void encodeAndClose(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception
-
decode
protected void decode(ChannelHandlerContext ctx, INBOUND_IN msg) throws Exception
- Throws:
Exception
- See Also:
MessageToMessageDecoder.decode(ChannelHandlerContext, Object)
-
decodeAndClose
protected void decodeAndClose(ChannelHandlerContext ctx, INBOUND_IN msg) throws Exception
-
-