Class 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.ChannelDuplexHandler
    A Codec for on-the-fly encoding/decoding of message. This can be thought of as a combination of MessageToMessageDecoder and MessageToMessageEncoder. Here is an example of a MessageToMessageCodec which just decode from Integer to Long and encode from Long to Integer.
         public class NumberCodec extends
                 MessageToMessageCodec<Integer, Long> {
             @Override
             public Long decode(ChannelHandlerContext ctx, Integer msg, List<Object> out)
                     throws Exception {
                 out.add(msg.longValue());
             }
    
             @Override
             public Integer encode(ChannelHandlerContext ctx, Long msg, List<Object> out)
                     throws Exception {
                 out.add(msg.intValue());
             }
         }
     
    Be aware that you need to call ReferenceCounted.retain() on messages that are just passed through if they are of type ReferenceCounted. This is needed as the MessageToMessageCodec will call ReferenceCounted.release() on encoded / decoded messages.
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

        io.netty.channel.ChannelHandler.Sharable
    • 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 Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      boolean acceptInboundMessage​(Object msg)
      Returns true if and only if the specified message can be decoded by this codec.
      boolean acceptOutboundMessage​(Object msg)
      Returns true if and only if the specified message can be encoded by this codec.
      void channelRead​(io.netty.channel.ChannelHandlerContext ctx, Object msg)  
      void channelReadComplete​(io.netty.channel.ChannelHandlerContext ctx)  
      protected abstract void decode​(io.netty.channel.ChannelHandlerContext ctx, INBOUND_IN msg, List<Object> out)  
      protected abstract void encode​(io.netty.channel.ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out)  
      void write​(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
      • 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 decode
        outboundMessageType - The type of messages to encode
    • Method Detail

      • channelRead

        public void channelRead​(io.netty.channel.ChannelHandlerContext ctx,
                                Object msg)
                         throws Exception
        Specified by:
        channelRead in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        channelRead in class io.netty.channel.ChannelInboundHandlerAdapter
        Throws:
        Exception
      • channelReadComplete

        public void channelReadComplete​(io.netty.channel.ChannelHandlerContext ctx)
                                 throws Exception
        Specified by:
        channelReadComplete in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        channelReadComplete in class io.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:
        write in interface io.netty.channel.ChannelOutboundHandler
        Overrides:
        write in class io.netty.channel.ChannelDuplexHandler
        Throws:
        Exception
      • acceptInboundMessage

        public boolean acceptInboundMessage​(Object msg)
                                     throws Exception
        Returns true 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
        Returns true if and only if the specified message can be encoded by this codec.
        Parameters:
        msg - the message
        Throws:
        Exception