Class ByteBufList

  • All Implemented Interfaces:
    io.netty.util.ReferenceCounted

    public class ByteBufList
    extends io.netty.util.AbstractReferenceCounted
    ByteBufList is a holder of a sequence of ByteBuf objects.

    This class doesn't trying to mimic the ByteBuf, but rather exposes itself just like a regular object which will need to be encoded on the channel. There are 2 utility encoders:

    • ENCODER: regular encode that will write all the buffers in the ByteBufList on the channel

    Example:

     bootstrap.handler(new ChannelInitializer<SocketChannel>() {
         public void initChannel(SocketChannel ch) throws Exception {
             ChannelPipeline pipeline = ch.pipeline();
             pipeline.addLast("bytebufList", ByteBufList.ENCODER);
             pipeline.addLast("mainhandler", MyHandler.class);
         }
     });
     

    ByteBufList is pooling the instances and uses ref-counting to release them.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(io.netty.buffer.ByteBuf buf)
      Append a ByteBuf at the end of this ByteBufList.
      byte[] array()
      Returns a reference to the array backing this ByteBufList.
      int arrayOffset()
      Returns the offset of the first byte within the backing byte array of this buffer.
      static ByteBufList clone​(ByteBufList other)
      Get a new ByteBufList instance from the pool that is the clone of an already existing instance.
      static io.netty.buffer.ByteBuf coalesce​(ByteBufList list)  
      protected void deallocate()  
      static ByteBufList get()  
      static ByteBufList get​(io.netty.buffer.ByteBuf b1)
      Get a new ByteBufList from the pool and assign 1 buffer to it.
      static ByteBufList get​(io.netty.buffer.ByteBuf b1, io.netty.buffer.ByteBuf b2)
      Get a new ByteBufList from the pool and assign 2 buffers to it.
      io.netty.buffer.ByteBuf getBuffer​(int index)
      Get access to a particular buffer in the list.
      int getBytes​(byte[] dst)
      Write bytes from the current ByteBufList into a byte array.
      boolean hasArray()
      Returns true if this buffer has a single backing byte array.
      void prepend​(io.netty.buffer.ByteBuf buf)
      Prepend a ByteBuf at the beginning of this ByteBufList.
      int readableBytes()  
      ByteBufList retain()  
      int size()  
      byte[] toArray()
      Creates a copy of the readable content of the internal buffers and returns the copy.
      io.netty.util.ReferenceCounted touch​(java.lang.Object hint)  
      • Methods inherited from class io.netty.util.AbstractReferenceCounted

        refCnt, release, release, retain, setRefCnt, touch
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • get

        public static ByteBufList get​(io.netty.buffer.ByteBuf b1,
                                      io.netty.buffer.ByteBuf b2)
        Get a new ByteBufList from the pool and assign 2 buffers to it.

        The buffers b1 and b2 lifecycles are now managed by the ByteBufList: when the ByteBufList is deallocated, b1 and b2 will be released as well.

        Parameters:
        b1 - first buffer
        b2 - second buffer
        Returns:
        a ByteBufList instance from the pool
      • get

        public static ByteBufList get​(io.netty.buffer.ByteBuf b1)
        Get a new ByteBufList from the pool and assign 1 buffer to it.

        The buffer b1 lifecycle is now managed by the ByteBufList: when the ByteBufList is deallocated, b1 will be released as well.

        Parameters:
        b1 - first buffer
        Returns:
        a ByteBufList instance from the pool
      • add

        public void add​(io.netty.buffer.ByteBuf buf)
        Append a ByteBuf at the end of this ByteBufList.
      • prepend

        public void prepend​(io.netty.buffer.ByteBuf buf)
        Prepend a ByteBuf at the beginning of this ByteBufList.
      • readableBytes

        public int readableBytes()
        Returns:
        the total amount of readable bytes across all the ByteBuf included in the list
      • getBuffer

        public io.netty.buffer.ByteBuf getBuffer​(int index)
        Get access to a particular buffer in the list.
        Parameters:
        index - the index of the buffer
        Returns:
        the buffer
      • size

        public int size()
        Returns:
        the number of buffers included in the ByteBufList
      • getBytes

        public int getBytes​(byte[] dst)
        Write bytes from the current ByteBufList into a byte array.

        This won't modify the reader index of the internal buffers.

        Parameters:
        dst - the destination byte array
        Returns:
        the number of copied bytes
      • toArray

        public byte[] toArray()
        Creates a copy of the readable content of the internal buffers and returns the copy.
        Returns:
        an array containing all the internal buffers content
      • hasArray

        public boolean hasArray()
        Returns true if this buffer has a single backing byte array. If this method returns true, you can safely call array() and arrayOffset().
        Returns:
        true, if this ByteBufList is backed by a single array
      • array

        public byte[] array()
        Returns a reference to the array backing this ByteBufList. This method must only be called if hasArray() returns true.
        Returns:
        the array backing this ByteBufList
      • arrayOffset

        public int arrayOffset()
        Returns the offset of the first byte within the backing byte array of this buffer. This method must only be called if hasArray() returns true.
        Returns:
        the offset of the first byte within the backing byte array.
      • coalesce

        public static io.netty.buffer.ByteBuf coalesce​(ByteBufList list)
        Returns:
        a single buffer with the content of both individual buffers
      • retain

        public ByteBufList retain()
        Specified by:
        retain in interface io.netty.util.ReferenceCounted
        Overrides:
        retain in class io.netty.util.AbstractReferenceCounted
      • deallocate

        protected void deallocate()
        Specified by:
        deallocate in class io.netty.util.AbstractReferenceCounted
      • touch

        public io.netty.util.ReferenceCounted touch​(java.lang.Object hint)