Class MessageToByteEncoder<I>

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelOutboundHandler
    Direct Known Subclasses:
    CompatibleObjectEncoder, ObjectEncoder

    public abstract class MessageToByteEncoder<I>
    extends io.netty.channel.ChannelOutboundHandlerAdapter
    ChannelOutboundHandlerAdapter which encodes message in a stream-like fashion from one message to an ByteBuf. Example implementation which encodes Integers to a ByteBuf.
         public class IntegerEncoder extends MessageToByteEncoder<Integer> {
             @Override
             public void encode(ChannelHandlerContext ctx, Integer msg, ByteBuf out)
                     throws Exception {
                 out.writeInt(msg);
             }
         }
     
    • Nested Class Summary

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

        io.netty.channel.ChannelHandler.Sharable
    • 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 io.netty.buffer.ByteBuf allocateBuffer​(io.netty.channel.ChannelHandlerContext ctx, I msg, boolean preferDirect)
      Allocate a ByteBuf which will be used as argument of #encode(ChannelHandlerContext, I, ByteBuf).
      protected abstract void encode​(io.netty.channel.ChannelHandlerContext ctx, I msg, io.netty.buffer.ByteBuf out)
      Encode a message into a ByteBuf.
      protected boolean isPreferDirect()  
      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

      • MessageToByteEncoder

        protected MessageToByteEncoder​(boolean preferDirect)
        Create a new instance which will try to detect the types to match out of the type parameter of the class.
        Parameters:
        preferDirect - true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.
      • MessageToByteEncoder

        protected MessageToByteEncoder​(Class<? extends I> outboundMessageType,
                                       boolean preferDirect)
        Create a new instance
        Parameters:
        outboundMessageType - The type of messages to match
        preferDirect - true if a direct ByteBuf should be tried to be used as target for the encoded messages. If false is used it will allocate a heap ByteBuf, which is backed by an byte array.
    • 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
      • allocateBuffer

        protected io.netty.buffer.ByteBuf allocateBuffer​(io.netty.channel.ChannelHandlerContext ctx,
                                                         I msg,
                                                         boolean preferDirect)
                                                  throws Exception
        Allocate a ByteBuf which will be used as argument of #encode(ChannelHandlerContext, I, ByteBuf). Sub-classes may override this method to return ByteBuf with a perfect matching initialCapacity.
        Throws:
        Exception
      • encode

        protected abstract void encode​(io.netty.channel.ChannelHandlerContext ctx,
                                       I msg,
                                       io.netty.buffer.ByteBuf out)
                                throws Exception
        Encode a message into a ByteBuf. This method will be called for each written message that can be handled by this encoder.
        Parameters:
        ctx - the ChannelHandlerContext which this MessageToByteEncoder belongs to
        msg - the message to encode
        out - the ByteBuf into which the encoded message will be written
        Throws:
        Exception - is thrown if an error occurs
      • isPreferDirect

        protected boolean isPreferDirect()