Class MessageToMessageEncoder<I>

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelOutboundHandler
    Direct Known Subclasses:
    Base64Encoder, ByteArrayEncoder, DatagramPacketEncoder, LengthFieldPrepender, LineEncoder, StringEncoder

    public abstract class MessageToMessageEncoder<I>
    extends io.netty.channel.ChannelOutboundHandlerAdapter
    ChannelOutboundHandlerAdapter which encodes from one message to an other message For example here is an implementation which decodes an Integer to an String.
         public class IntegerToStringEncoder extends
                 MessageToMessageEncoder<Integer> {
    
             @Override
             public void encode(ChannelHandlerContext ctx, Integer message, List<Object> out)
                     throws Exception {
                 out.add(message.toString());
             }
         }
     
    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 MessageToMessageEncoder will call ReferenceCounted.release() on encoded 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 MessageToMessageEncoder()
      Create a new instance which will try to detect the types to match out of the type parameter of the class.
      protected MessageToMessageEncoder​(Class<? extends I> outboundMessageType)
      Create a new instance
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      boolean acceptOutboundMessage​(Object msg)
      Returns true if the given message should be handled.
      protected abstract void encode​(io.netty.channel.ChannelHandlerContext ctx, I msg, List<Object> out)
      Encode from one message to an other.
      void write​(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)  
      • Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter

        bind, close, connect, deregister, disconnect, flush, read
      • Methods inherited from class io.netty.channel.ChannelHandlerAdapter

        ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
      • Methods inherited from interface io.netty.channel.ChannelHandler

        exceptionCaught, handlerAdded, handlerRemoved
    • Constructor Detail

      • MessageToMessageEncoder

        protected MessageToMessageEncoder()
        Create a new instance which will try to detect the types to match out of the type parameter of the class.
      • MessageToMessageEncoder

        protected MessageToMessageEncoder​(Class<? extends I> outboundMessageType)
        Create a new instance
        Parameters:
        outboundMessageType - The type of messages to match and so encode
    • Method Detail

      • acceptOutboundMessage

        public boolean acceptOutboundMessage​(Object msg)
                                      throws Exception
        Returns true if the given message should be handled. If false it will be passed to the next ChannelOutboundHandler in the ChannelPipeline.
        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.ChannelOutboundHandlerAdapter
        Throws:
        Exception
      • encode

        protected abstract void encode​(io.netty.channel.ChannelHandlerContext ctx,
                                       I msg,
                                       List<Object> out)
                                throws Exception
        Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.
        Parameters:
        ctx - the ChannelHandlerContext which this MessageToMessageEncoder belongs to
        msg - the message to encode to an other one
        out - the List into which the encoded msg should be added needs to do some kind of aggregation
        Throws:
        Exception - is thrown if an error occurs