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:
ChannelHandler,ChannelInboundHandler,ChannelOutboundHandler
- Direct Known Subclasses:
Http2StreamFrameToHttpObjectCodec,HttpContentEncoder,SpdyHttpResponseStreamIdHandler
public abstract class MessageToMessageCodec<INBOUND_IN,OUTBOUND_IN> extends ChannelDuplexHandler
A 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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
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(ChannelHandlerContext ctx, Object msg)CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.protected abstract voiddecode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out)protected abstract voidencode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)voidwrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.-
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, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
-
-
-
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 class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadin interfaceChannelInboundHandler- Overrides:
channelReadin classChannelInboundHandlerAdapter- Throws:
Exception
-
write
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
Description copied from class:ChannelDuplexHandlerCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelDuplexHandler- 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
-
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(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception
- Throws:
Exception- See Also:
MessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)
-
decode
protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out) throws Exception
- Throws:
Exception- See Also:
MessageToMessageDecoder.decode(ChannelHandlerContext, Object, List)
-
-