Class ByteArrayDecoder

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

    public class ByteArrayDecoder
    extends MessageToMessageDecoder<io.netty.buffer.ByteBuf>
    Decodes a received ByteBuf into an array of bytes. A typical setup for TCP/IP would be:
     ChannelPipeline pipeline = ...;
    
     // Decoders
     pipeline.addLast("frameDecoder",
                      new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
     pipeline.addLast("bytesDecoder",
                      new ByteArrayDecoder());
    
     // Encoder
     pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
     pipeline.addLast("bytesEncoder", new ByteArrayEncoder());
     
    and then you can use an array of bytes instead of a ByteBuf as a message:
     void channelRead(ChannelHandlerContext ctx, byte[] bytes) {
         ...
     }
     
    • Nested Class Summary

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

        io.netty.channel.ChannelHandler.Sharable
    • Constructor Detail

      • ByteArrayDecoder

        public ByteArrayDecoder()
    • Method Detail

      • decode

        protected void decode​(io.netty.channel.ChannelHandlerContext ctx,
                              io.netty.buffer.ByteBuf msg,
                              List<Object> out)
                       throws Exception
        Description copied from class: MessageToMessageDecoder
        Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.
        Specified by:
        decode in class MessageToMessageDecoder<io.netty.buffer.ByteBuf>
        Parameters:
        ctx - the ChannelHandlerContext which this MessageToMessageDecoder belongs to
        msg - the message to decode to an other one
        out - the List to which decoded messages should be added
        Throws:
        Exception - is thrown if an error occurs