Package io.netty.handler.codec
Class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN>
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.codec.MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN>
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler,io.netty.channel.ChannelInboundHandler,io.netty.channel.ChannelOutboundHandler
public abstract class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN> extends io.netty.channel.ChannelDuplexHandlerA Codec for on-the-fly encoding/decoding of message. This can be thought of as a combination ofMessageToMessageDecoderandMessageToMessageEncoder. Here is an example of aMessageToMessageCodecwhich just decode fromIntegertoLongand encode fromLongtoInteger.public class NumberCodec extendsBe aware that you need to callMessageToMessageCodec<Integer,Long> {@OverridepublicLongdecode(ChannelHandlerContextctx,Integermsg, List<Object> out) throwsException{ out.add(msg.longValue()); }@OverridepublicIntegerencode(ChannelHandlerContextctx,Longmsg, 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 theMessageToMessageCodecwill callReferenceCounted.release()on encoded / decoded messages.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageToMessageCodec()Create a new instance which will try to detect the types to decode and encode out of the type parameter of the class.protectedMessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType)Create a new instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanacceptInboundMessage(Object msg)Returnstrueif and only if the specified message can be decoded by this codec.booleanacceptOutboundMessage(Object msg)Returnstrueif and only if the specified message can be encoded by this codec.voidchannelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg)voidchannelReadComplete(io.netty.channel.ChannelHandlerContext ctx)protected abstract voiddecode(io.netty.channel.ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out)protected abstract voidencode(io.netty.channel.ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)voidwrite(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
-
-
-
-
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(io.netty.channel.ChannelHandlerContext ctx, Object msg) throws Exception- Specified by:
channelReadin interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelReadin classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
channelReadComplete
public void channelReadComplete(io.netty.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelReadCompletein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelReadCompletein classio.netty.channel.ChannelInboundHandlerAdapter- Throws:
Exception
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception- Specified by:
writein interfaceio.netty.channel.ChannelOutboundHandler- Overrides:
writein classio.netty.channel.ChannelDuplexHandler- Throws:
Exception
-
acceptInboundMessage
public boolean acceptInboundMessage(Object msg) throws Exception
Returnstrueif 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
Returnstrueif and only if the specified message can be encoded by this codec.- Parameters:
msg- the message- Throws:
Exception
-
encode
protected abstract void encode(io.netty.channel.ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception- Throws:
Exception- See Also:
MessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)
-
decode
protected abstract void decode(io.netty.channel.ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out) throws Exception- Throws:
Exception- See Also:
MessageToMessageDecoder.decode(ChannelHandlerContext, Object, List)
-
-