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(java.lang.Class<? extends INBOUND_IN> inboundMessageType, java.lang.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(java.lang.Object msg)Returnstrueif and only if the specified message can be decoded by this codec.booleanacceptOutboundMessage(java.lang.Object msg)Returnstrueif and only if the specified message can be encoded by this codec.voidchannelRead(ChannelHandlerContext ctx, java.lang.Object msg)CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelReadComplete(ChannelHandlerContext ctx)CallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.protected abstract voiddecode(ChannelHandlerContext ctx, INBOUND_IN msg, java.util.List<java.lang.Object> out)protected abstract voidencode(ChannelHandlerContext ctx, OUTBOUND_IN msg, java.util.List<java.lang.Object> out)voidwrite(ChannelHandlerContext ctx, java.lang.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, 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(java.lang.Class<? extends INBOUND_IN> inboundMessageType, java.lang.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, java.lang.Object msg) throws java.lang.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:
java.lang.Exception
-
channelReadComplete
public void channelReadComplete(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadCompletein interfaceChannelInboundHandler- Overrides:
channelReadCompletein classChannelInboundHandlerAdapter- Throws:
java.lang.Exception
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.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:
java.lang.Exception- thrown if an error occurs
-
acceptInboundMessage
public boolean acceptInboundMessage(java.lang.Object msg) throws java.lang.ExceptionReturnstrueif and only if the specified message can be decoded by this codec.- Parameters:
msg- the message- Throws:
java.lang.Exception
-
acceptOutboundMessage
public boolean acceptOutboundMessage(java.lang.Object msg) throws java.lang.ExceptionReturnstrueif and only if the specified message can be encoded by this codec.- Parameters:
msg- the message- Throws:
java.lang.Exception
-
encode
protected abstract void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Throws:
java.lang.Exception- See Also:
MessageToMessageEncoder.encode(ChannelHandlerContext, Object, List)
-
decode
protected abstract void decode(ChannelHandlerContext ctx, INBOUND_IN msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
- Throws:
java.lang.Exception- See Also:
MessageToMessageDecoder.decode(ChannelHandlerContext, Object, List)
-
-