Class ByteToMessageCodec<I>

  • All Implemented Interfaces:
    io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler, io.netty.channel.ChannelOutboundHandler

    public abstract class ByteToMessageCodec<I>
    extends io.netty.channel.ChannelDuplexHandler
    A Codec for on-the-fly encoding/decoding of bytes to messages and vise-versa. This can be thought of as a combination of ByteToMessageDecoder and MessageToByteEncoder. Be aware that sub-classes of ByteToMessageCodec MUST NOT annotated with {@link @Sharable}.
    • 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 and only if the specified message can be encoded by this codec.
      void channelInactive​(io.netty.channel.ChannelHandlerContext ctx)  
      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, io.netty.buffer.ByteBuf in, List<Object> out)  
      protected void decodeLast​(io.netty.channel.ChannelHandlerContext ctx, io.netty.buffer.ByteBuf in, List<Object> out)  
      protected abstract void encode​(io.netty.channel.ChannelHandlerContext ctx, I msg, io.netty.buffer.ByteBuf out)  
      void handlerAdded​(io.netty.channel.ChannelHandlerContext ctx)  
      void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)  
      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, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
      • Methods inherited from class io.netty.channel.ChannelHandlerAdapter

        ensureNotSharable, isSharable
    • Constructor Detail

      • ByteToMessageCodec

        protected ByteToMessageCodec​(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.
      • ByteToMessageCodec

        protected ByteToMessageCodec​(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 and only if the specified message can be encoded by this codec.
        Parameters:
        msg - the message
        Throws:
        Exception
      • 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
      • 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
      • 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
      • channelInactive

        public void channelInactive​(io.netty.channel.ChannelHandlerContext ctx)
                             throws Exception
        Specified by:
        channelInactive in interface io.netty.channel.ChannelInboundHandler
        Overrides:
        channelInactive in class io.netty.channel.ChannelInboundHandlerAdapter
        Throws:
        Exception
      • handlerAdded

        public void handlerAdded​(io.netty.channel.ChannelHandlerContext ctx)
                          throws Exception
        Specified by:
        handlerAdded in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerAdded in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception
      • handlerRemoved

        public void handlerRemoved​(io.netty.channel.ChannelHandlerContext ctx)
                            throws Exception
        Specified by:
        handlerRemoved in interface io.netty.channel.ChannelHandler
        Overrides:
        handlerRemoved in class io.netty.channel.ChannelHandlerAdapter
        Throws:
        Exception