Class CompositeByteBuf

All Implemented Interfaces:
ByteBufConvertible, ReferenceCounted, Comparable<ByteBuf>, Iterable<ByteBuf>
Direct Known Subclasses:
WrappedCompositeByteBuf

public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements Iterable<ByteBuf>
A virtual buffer which shows multiple buffers as a single merged buffer. It is recommended to use ByteBufAllocator.compositeBuffer() or Unpooled.wrappedBuffer(ByteBuf...) instead of calling the constructor explicitly.
  • Field Details

  • Constructor Details

    • CompositeByteBuf

      private CompositeByteBuf(ByteBufAllocator alloc, boolean direct, int maxNumComponents, int initSize)
    • CompositeByteBuf

      public CompositeByteBuf(ByteBufAllocator alloc, boolean direct, int maxNumComponents)
    • CompositeByteBuf

      public CompositeByteBuf(ByteBufAllocator alloc, boolean direct, int maxNumComponents, ByteBuf... buffers)
    • CompositeByteBuf

      CompositeByteBuf(ByteBufAllocator alloc, boolean direct, int maxNumComponents, ByteBuf[] buffers, int offset)
    • CompositeByteBuf

      public CompositeByteBuf(ByteBufAllocator alloc, boolean direct, int maxNumComponents, Iterable<ByteBuf> buffers)
    • CompositeByteBuf

      CompositeByteBuf(ByteBufAllocator alloc, boolean direct, int maxNumComponents, CompositeByteBuf.ByteWrapper<T> wrapper, T[] buffers, int offset)
    • CompositeByteBuf

      CompositeByteBuf(ByteBufAllocator alloc)
  • Method Details

    • newCompArray

      private static CompositeByteBuf.Component[] newCompArray(int initComponents, int maxNumComponents)
    • addComponent

      public CompositeByteBuf addComponent(ByteBuf buffer)
      Add the given ByteBuf.

      Be aware that this method does not increase the writerIndex of the CompositeByteBuf. If you need to have it increased use addComponent(boolean, ByteBuf).

      ReferenceCounted.release() ownership of buffer is transferred to this CompositeByteBuf.

      Parameters:
      buffer - the ByteBuf to add. ReferenceCounted.release() ownership is transferred to this CompositeByteBuf.
    • addComponents

      public CompositeByteBuf addComponents(ByteBuf... buffers)
      Add the given ByteBufs.

      Be aware that this method does not increase the writerIndex of the CompositeByteBuf. If you need to have it increased use addComponents(boolean, ByteBuf[]).

      ReferenceCounted.release() ownership of all ByteBuf objects in buffers is transferred to this CompositeByteBuf.

      Parameters:
      buffers - the ByteBufs to add. ReferenceCounted.release() ownership of all ReferenceCounted.release() ownership of all ByteBuf objects is transferred to this CompositeByteBuf.
    • addComponents

      public CompositeByteBuf addComponents(Iterable<ByteBuf> buffers)
      Add the given ByteBufs.

      Be aware that this method does not increase the writerIndex of the CompositeByteBuf. If you need to have it increased use addComponents(boolean, Iterable).

      ReferenceCounted.release() ownership of all ByteBuf objects in buffers is transferred to this CompositeByteBuf.

      Parameters:
      buffers - the ByteBufs to add. ReferenceCounted.release() ownership of all ReferenceCounted.release() ownership of all ByteBuf objects is transferred to this CompositeByteBuf.
    • addComponent

      public CompositeByteBuf addComponent(int cIndex, ByteBuf buffer)
      Add the given ByteBuf on the specific index.

      Be aware that this method does not increase the writerIndex of the CompositeByteBuf. If you need to have it increased use addComponent(boolean, int, ByteBuf).

      ReferenceCounted.release() ownership of buffer is transferred to this CompositeByteBuf.

      Parameters:
      cIndex - the index on which the ByteBuf will be added.
      buffer - the ByteBuf to add. ReferenceCounted.release() ownership is transferred to this CompositeByteBuf.
    • addComponent

      public CompositeByteBuf addComponent(boolean increaseWriterIndex, ByteBuf buffer)
      Add the given ByteBuf and increase the writerIndex if increaseWriterIndex is true. ReferenceCounted.release() ownership of buffer is transferred to this CompositeByteBuf.
      Parameters:
      buffer - the ByteBuf to add. ReferenceCounted.release() ownership is transferred to this CompositeByteBuf.
    • addComponents

      public CompositeByteBuf addComponents(boolean increaseWriterIndex, ByteBuf... buffers)
      Add the given ByteBufs and increase the writerIndex if increaseWriterIndex is true. ReferenceCounted.release() ownership of all ByteBuf objects in buffers is transferred to this CompositeByteBuf.
      Parameters:
      buffers - the ByteBufs to add. ReferenceCounted.release() ownership of all ReferenceCounted.release() ownership of all ByteBuf objects is transferred to this CompositeByteBuf.
    • addComponents

      public CompositeByteBuf addComponents(boolean increaseWriterIndex, Iterable<ByteBuf> buffers)
      Add the given ByteBufs and increase the writerIndex if increaseWriterIndex is true. ReferenceCounted.release() ownership of all ByteBuf objects in buffers is transferred to this CompositeByteBuf.
      Parameters:
      buffers - the ByteBufs to add. ReferenceCounted.release() ownership of all ReferenceCounted.release() ownership of all ByteBuf objects is transferred to this CompositeByteBuf.
    • addComponent

      public CompositeByteBuf addComponent(boolean increaseWriterIndex, int cIndex, ByteBuf buffer)
      Add the given ByteBuf on the specific index and increase the writerIndex if increaseWriterIndex is true. ReferenceCounted.release() ownership of buffer is transferred to this CompositeByteBuf.
      Parameters:
      cIndex - the index on which the ByteBuf will be added.
      buffer - the ByteBuf to add. ReferenceCounted.release() ownership is transferred to this CompositeByteBuf.
    • checkForOverflow

      private static void checkForOverflow(int capacity, int readableBytes)
    • addComponent0

      private int addComponent0(boolean increaseWriterIndex, int cIndex, ByteBuf buffer)
      Precondition is that buffer != null.
    • ensureAccessible

      private static ByteBuf ensureAccessible(ByteBuf buf)
    • newComponent

      private CompositeByteBuf.Component newComponent(ByteBuf buf, int offset)
    • addComponents

      public CompositeByteBuf addComponents(int cIndex, ByteBuf... buffers)
      Add the given ByteBufs on the specific index

      Be aware that this method does not increase the writerIndex of the CompositeByteBuf. If you need to have it increased you need to handle it by your own.

      ReferenceCounted.release() ownership of all ByteBuf objects in buffers is transferred to this CompositeByteBuf.

      Parameters:
      cIndex - the index on which the ByteBuf will be added. ReferenceCounted.release() ownership of all ReferenceCounted.release() ownership of all ByteBuf objects is transferred to this CompositeByteBuf.
      buffers - the ByteBufs to add. ReferenceCounted.release() ownership of all ReferenceCounted.release() ownership of all ByteBuf objects is transferred to this CompositeByteBuf.
    • addComponents0

      private CompositeByteBuf addComponents0(boolean increaseWriterIndex, int cIndex, ByteBuf[] buffers, int arrOffset)
    • addComponents0

      private <T> int addComponents0(boolean increaseWriterIndex, int cIndex, CompositeByteBuf.ByteWrapper<T> wrapper, T[] buffers, int offset)
    • addComponents

      public CompositeByteBuf addComponents(int cIndex, Iterable<ByteBuf> buffers)
      Add the given ByteBufs on the specific index Be aware that this method does not increase the writerIndex of the CompositeByteBuf. If you need to have it increased you need to handle it by your own.

      ReferenceCounted.release() ownership of all ByteBuf objects in buffers is transferred to this CompositeByteBuf.

      Parameters:
      cIndex - the index on which the ByteBuf will be added.
      buffers - the ByteBufs to add. ReferenceCounted.release() ownership of all ReferenceCounted.release() ownership of all ByteBuf objects is transferred to this CompositeByteBuf.
    • addFlattenedComponents

      public CompositeByteBuf addFlattenedComponents(boolean increaseWriterIndex, ByteBuf buffer)
      Add the given ByteBuf and increase the writerIndex if increaseWriterIndex is true. If the provided buffer is a CompositeByteBuf itself, a "shallow copy" of its readable components will be performed. Thus the actual number of new components added may vary and in particular will be zero if the provided buffer is not readable.

      ReferenceCounted.release() ownership of buffer is transferred to this CompositeByteBuf.

      Parameters:
      buffer - the ByteBuf to add. ReferenceCounted.release() ownership is transferred to this CompositeByteBuf.
    • addComponents

      private CompositeByteBuf addComponents(boolean increaseIndex, int cIndex, Iterable<ByteBuf> buffers)
    • consolidateIfNeeded

      private void consolidateIfNeeded()
      This should only be called as last operation from a method as this may adjust the underlying array of components and so affect the index etc.
    • checkComponentIndex

      private void checkComponentIndex(int cIndex)
    • checkComponentIndex

      private void checkComponentIndex(int cIndex, int numComponents)
    • updateComponentOffsets

      private void updateComponentOffsets(int cIndex)
    • removeComponent

      public CompositeByteBuf removeComponent(int cIndex)
      Remove the ByteBuf from the given index.
      Parameters:
      cIndex - the index on from which the ByteBuf will be remove
    • removeComponents

      public CompositeByteBuf removeComponents(int cIndex, int numComponents)
      Remove the number of ByteBufs starting from the given index.
      Parameters:
      cIndex - the index on which the ByteBufs will be started to removed
      numComponents - the number of components to remove
    • iterator

      public Iterator<ByteBuf> iterator()
      Specified by:
      iterator in interface Iterable<ByteBuf>
    • forEachByteAsc0

      protected int forEachByteAsc0(int start, int end, ByteProcessor processor) throws Exception
      Overrides:
      forEachByteAsc0 in class AbstractByteBuf
      Throws:
      Exception
    • forEachByteDesc0

      protected int forEachByteDesc0(int rStart, int rEnd, ByteProcessor processor) throws Exception
      Overrides:
      forEachByteDesc0 in class AbstractByteBuf
      Throws:
      Exception
    • decompose

      public List<ByteBuf> decompose(int offset, int length)
      Same with AbstractByteBuf.slice(int, int) except that this method returns a list.
    • isDirect

      public boolean isDirect()
      Description copied from class: ByteBuf
      Returns true if and only if this buffer is backed by an NIO direct buffer.
      Specified by:
      isDirect in class ByteBuf
    • hasArray

      public boolean hasArray()
      Description copied from class: ByteBuf
      Returns true if and only if this buffer has a backing byte array. If this method returns true, you can safely call ByteBuf.array() and ByteBuf.arrayOffset().
      Specified by:
      hasArray in class ByteBuf
    • array

      public byte[] array()
      Description copied from class: ByteBuf
      Returns the backing byte array of this buffer.
      Specified by:
      array in class ByteBuf
    • arrayOffset

      public int arrayOffset()
      Description copied from class: ByteBuf
      Returns the offset of the first byte within the backing byte array of this buffer.
      Specified by:
      arrayOffset in class ByteBuf
    • hasMemoryAddress

      public boolean hasMemoryAddress()
      Description copied from class: ByteBuf
      Returns true if and only if this buffer has a reference to the low-level memory address that points to the backing data.
      Specified by:
      hasMemoryAddress in class ByteBuf
    • memoryAddress

      public long memoryAddress()
      Description copied from class: ByteBuf
      Returns the low-level memory address that point to the first byte of ths backing data.
      Specified by:
      memoryAddress in class ByteBuf
    • capacity

      public int capacity()
      Description copied from class: ByteBuf
      Returns the number of bytes (octets) this buffer can contain.
      Specified by:
      capacity in class ByteBuf
    • capacity

      public CompositeByteBuf capacity(int newCapacity)
      Description copied from class: ByteBuf
      Adjusts the capacity of this buffer. If the newCapacity is less than the current capacity, the content of this buffer is truncated. If the newCapacity is greater than the current capacity, the buffer is appended with unspecified data whose length is (newCapacity - currentCapacity).
      Specified by:
      capacity in class ByteBuf
    • alloc

      public ByteBufAllocator alloc()
      Description copied from class: ByteBuf
      Returns the ByteBufAllocator which created this buffer.
      Specified by:
      alloc in class ByteBuf
    • order

      public ByteOrder order()
      Description copied from class: ByteBuf
      Returns the endianness of this buffer.
      Specified by:
      order in class ByteBuf
    • numComponents

      public int numComponents()
      Return the current number of ByteBuf's that are composed in this instance
    • maxNumComponents

      public int maxNumComponents()
      Return the max number of ByteBuf's that are composed in this instance
    • toComponentIndex

      public int toComponentIndex(int offset)
      Return the index for the given offset
    • toComponentIndex0

      private int toComponentIndex0(int offset)
    • toByteIndex

      public int toByteIndex(int cIndex)
    • getByte

      public byte getByte(int index)
      Description copied from class: ByteBuf
      Gets a byte at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      getByte in class AbstractByteBuf
    • _getByte

      protected byte _getByte(int index)
      Specified by:
      _getByte in class AbstractByteBuf
    • _getShort

      protected short _getShort(int index)
      Specified by:
      _getShort in class AbstractByteBuf
    • _getShortLE

      protected short _getShortLE(int index)
      Specified by:
      _getShortLE in class AbstractByteBuf
    • _getUnsignedMedium

      protected int _getUnsignedMedium(int index)
      Specified by:
      _getUnsignedMedium in class AbstractByteBuf
    • _getUnsignedMediumLE

      protected int _getUnsignedMediumLE(int index)
      Specified by:
      _getUnsignedMediumLE in class AbstractByteBuf
    • _getInt

      protected int _getInt(int index)
      Specified by:
      _getInt in class AbstractByteBuf
    • _getIntLE

      protected int _getIntLE(int index)
      Specified by:
      _getIntLE in class AbstractByteBuf
    • _getLong

      protected long _getLong(int index)
      Specified by:
      _getLong in class AbstractByteBuf
    • _getLongLE

      protected long _getLongLE(int index)
      Specified by:
      _getLongLE in class AbstractByteBuf
    • getBytes

      public CompositeByteBuf getBytes(int index, byte[] dst, int dstIndex, int length)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer.
      Specified by:
      getBytes in class ByteBuf
      Parameters:
      dstIndex - the first index of the destination
      length - the number of bytes to transfer
    • getBytes

      public CompositeByteBuf getBytes(int index, ByteBuffer dst)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the specified absolute index until the destination's position reaches its limit. This method does not modify readerIndex or writerIndex of this buffer while the destination's position will be increased.
      Specified by:
      getBytes in class ByteBuf
    • getBytes

      public CompositeByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the specified absolute index. This method does not modify readerIndex or writerIndex of both the source (i.e. this) and the destination.
      Specified by:
      getBytes in class ByteBuf
      Parameters:
      dstIndex - the first index of the destination
      length - the number of bytes to transfer
    • getBytes

      public int getBytes(int index, GatheringByteChannel out, int length) throws IOException
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified channel starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer.
      Specified by:
      getBytes in class ByteBuf
      Parameters:
      length - the maximum number of bytes to transfer
      Returns:
      the actual number of bytes written out to the specified channel
      Throws:
      IOException - if the specified channel threw an exception during I/O
    • getBytes

      public int getBytes(int index, FileChannel out, long position, int length) throws IOException
      Description copied from class: ByteBuf
      Transfers this buffer's data starting at the specified absolute index to the specified channel starting at the given file position. This method does not modify readerIndex or writerIndex of this buffer. This method does not modify the channel's position.
      Specified by:
      getBytes in class ByteBuf
      Parameters:
      position - the file position at which the transfer is to begin
      length - the maximum number of bytes to transfer
      Returns:
      the actual number of bytes written out to the specified channel
      Throws:
      IOException - if the specified channel threw an exception during I/O
    • getBytes

      public CompositeByteBuf getBytes(int index, OutputStream out, int length) throws IOException
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified stream starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer.
      Specified by:
      getBytes in class ByteBuf
      Parameters:
      length - the number of bytes to transfer
      Throws:
      IOException - if the specified stream threw an exception during I/O
    • setByte

      public CompositeByteBuf setByte(int index, int value)
      Description copied from class: ByteBuf
      Sets the specified byte at the specified absolute index in this buffer. The 24 high-order bits of the specified value are ignored. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setByte in class AbstractByteBuf
    • _setByte

      protected void _setByte(int index, int value)
      Specified by:
      _setByte in class AbstractByteBuf
    • setShort

      public CompositeByteBuf setShort(int index, int value)
      Description copied from class: ByteBuf
      Sets the specified 16-bit short integer at the specified absolute index in this buffer. The 16 high-order bits of the specified value are ignored. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setShort in class AbstractByteBuf
    • _setShort

      protected void _setShort(int index, int value)
      Specified by:
      _setShort in class AbstractByteBuf
    • _setShortLE

      protected void _setShortLE(int index, int value)
      Specified by:
      _setShortLE in class AbstractByteBuf
    • setMedium

      public CompositeByteBuf setMedium(int index, int value)
      Description copied from class: ByteBuf
      Sets the specified 24-bit medium integer at the specified absolute index in this buffer. Please note that the most significant byte is ignored in the specified value. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setMedium in class AbstractByteBuf
    • _setMedium

      protected void _setMedium(int index, int value)
      Specified by:
      _setMedium in class AbstractByteBuf
    • _setMediumLE

      protected void _setMediumLE(int index, int value)
      Specified by:
      _setMediumLE in class AbstractByteBuf
    • setInt

      public CompositeByteBuf setInt(int index, int value)
      Description copied from class: ByteBuf
      Sets the specified 32-bit integer at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setInt in class AbstractByteBuf
    • _setInt

      protected void _setInt(int index, int value)
      Specified by:
      _setInt in class AbstractByteBuf
    • _setIntLE

      protected void _setIntLE(int index, int value)
      Specified by:
      _setIntLE in class AbstractByteBuf
    • setLong

      public CompositeByteBuf setLong(int index, long value)
      Description copied from class: ByteBuf
      Sets the specified 64-bit long integer at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setLong in class AbstractByteBuf
    • _setLong

      protected void _setLong(int index, long value)
      Specified by:
      _setLong in class AbstractByteBuf
    • _setLongLE

      protected void _setLongLE(int index, long value)
      Specified by:
      _setLongLE in class AbstractByteBuf
    • setBytes

      public CompositeByteBuf setBytes(int index, byte[] src, int srcIndex, int length)
      Description copied from class: ByteBuf
      Transfers the specified source array's data to this buffer starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer.
      Specified by:
      setBytes in class ByteBuf
    • setBytes

      public CompositeByteBuf setBytes(int index, ByteBuffer src)
      Description copied from class: ByteBuf
      Transfers the specified source buffer's data to this buffer starting at the specified absolute index until the source buffer's position reaches its limit. This method does not modify readerIndex or writerIndex of this buffer.
      Specified by:
      setBytes in class ByteBuf
    • setBytes

      public CompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length)
      Description copied from class: ByteBuf
      Transfers the specified source buffer's data to this buffer starting at the specified absolute index. This method does not modify readerIndex or writerIndex of both the source (i.e. this) and the destination.
      Specified by:
      setBytes in class ByteBuf
      Parameters:
      srcIndex - the first index of the source
      length - the number of bytes to transfer
    • setBytes

      public int setBytes(int index, InputStream in, int length) throws IOException
      Description copied from class: ByteBuf
      Transfers the content of the specified source stream to this buffer starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer.
      Specified by:
      setBytes in class ByteBuf
      Parameters:
      length - the number of bytes to transfer
      Returns:
      the actual number of bytes read in from the specified channel. -1 if the specified InputStream reached EOF.
      Throws:
      IOException - if the specified stream threw an exception during I/O
    • setBytes

      public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException
      Description copied from class: ByteBuf
      Transfers the content of the specified source channel to this buffer starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer.
      Specified by:
      setBytes in class ByteBuf
      Parameters:
      length - the maximum number of bytes to transfer
      Returns:
      the actual number of bytes read in from the specified channel. -1 if the specified channel is closed or it reached EOF.
      Throws:
      IOException - if the specified channel threw an exception during I/O
    • setBytes

      public int setBytes(int index, FileChannel in, long position, int length) throws IOException
      Description copied from class: ByteBuf
      Transfers the content of the specified source channel starting at the given file position to this buffer starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer. This method does not modify the channel's position.
      Specified by:
      setBytes in class ByteBuf
      Parameters:
      position - the file position at which the transfer is to begin
      length - the maximum number of bytes to transfer
      Returns:
      the actual number of bytes read in from the specified channel. -1 if the specified channel is closed or it reached EOF.
      Throws:
      IOException - if the specified channel threw an exception during I/O
    • copy

      public ByteBuf copy(int index, int length)
      Description copied from class: ByteBuf
      Returns a copy of this buffer's sub-region. Modifying the content of the returned buffer or this buffer does not affect each other at all. This method does not modify readerIndex or writerIndex of this buffer.
      Specified by:
      copy in class ByteBuf
    • copyTo

      private void copyTo(int index, int length, int componentId, ByteBuf dst)
    • component

      public ByteBuf component(int cIndex)
      Return the ByteBuf on the specified index
      Parameters:
      cIndex - the index for which the ByteBuf should be returned
      Returns:
      buf the ByteBuf on the specified index
    • componentAtOffset

      public ByteBuf componentAtOffset(int offset)
      Return the ByteBuf on the specified index
      Parameters:
      offset - the offset for which the ByteBuf should be returned
      Returns:
      the ByteBuf on the specified index
    • internalComponent

      public ByteBuf internalComponent(int cIndex)
      Return the internal ByteBuf on the specified index. Note that updating the indexes of the returned buffer will lead to an undefined behavior of this buffer.
      Parameters:
      cIndex - the index for which the ByteBuf should be returned
    • internalComponentAtOffset

      public ByteBuf internalComponentAtOffset(int offset)
      Return the internal ByteBuf on the specified offset. Note that updating the indexes of the returned buffer will lead to an undefined behavior of this buffer.
      Parameters:
      offset - the offset for which the ByteBuf should be returned
    • findComponent

      private CompositeByteBuf.Component findComponent(int offset)
    • findComponent0

      private CompositeByteBuf.Component findComponent0(int offset)
    • findIt

      private CompositeByteBuf.Component findIt(int offset)
    • nioBufferCount

      public int nioBufferCount()
      Description copied from class: ByteBuf
      Returns the maximum number of NIO ByteBuffers that consist this buffer. Note that ByteBuf.nioBuffers() or ByteBuf.nioBuffers(int, int) might return a less number of ByteBuffers.
      Specified by:
      nioBufferCount in class ByteBuf
      Returns:
      -1 if this buffer has no underlying ByteBuffer. the number of the underlying ByteBuffers if this buffer has at least one underlying ByteBuffer. Note that this method does not return 0 to avoid confusion.
      See Also:
    • internalNioBuffer

      public ByteBuffer internalNioBuffer(int index, int length)
      Description copied from class: ByteBuf
      Internal use only: Exposes the internal NIO buffer.
      Specified by:
      internalNioBuffer in class ByteBuf
    • nioBuffer

      public ByteBuffer nioBuffer(int index, int length)
      Description copied from class: ByteBuf
      Exposes this buffer's sub-region as an NIO ByteBuffer. The returned buffer either share or contains the copied content of this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method does not modify readerIndex or writerIndex of this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer and it adjusted its capacity.
      Specified by:
      nioBuffer in class ByteBuf
      See Also:
    • nioBuffers

      public ByteBuffer[] nioBuffers(int index, int length)
      Description copied from class: ByteBuf
      Exposes this buffer's bytes as an NIO ByteBuffer's for the specified index and length The returned buffer either share or contains the copied content of this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method does not modify readerIndex or writerIndex of this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer and it adjusted its capacity.
      Specified by:
      nioBuffers in class ByteBuf
      See Also:
    • consolidate

      public CompositeByteBuf consolidate()
      Consolidate the composed ByteBufs
    • consolidate

      public CompositeByteBuf consolidate(int cIndex, int numComponents)
      Consolidate the composed ByteBufs
      Parameters:
      cIndex - the index on which to start to compose
      numComponents - the number of components to compose
    • consolidate0

      private void consolidate0(int cIndex, int numComponents)
    • discardReadComponents

      public CompositeByteBuf discardReadComponents()
      Discard all ByteBufs which are read.
    • discardReadBytes

      public CompositeByteBuf discardReadBytes()
      Description copied from class: ByteBuf
      Discards the bytes between the 0th index and readerIndex. It moves the bytes between readerIndex and writerIndex to the 0th index, and sets readerIndex and writerIndex to 0 and oldWriterIndex - oldReaderIndex respectively.

      Please refer to the class documentation for more detailed explanation.

      Overrides:
      discardReadBytes in class AbstractByteBuf
    • allocBuffer

      private ByteBuf allocBuffer(int capacity)
    • toString

      public String toString()
      Description copied from class: ByteBuf
      Returns the string representation of this buffer. This method does not necessarily return the whole content of the buffer but returns the values of the key properties such as ByteBuf.readerIndex(), ByteBuf.writerIndex() and ByteBuf.capacity().
      Overrides:
      toString in class AbstractByteBuf
    • readerIndex

      public CompositeByteBuf readerIndex(int readerIndex)
      Description copied from class: ByteBuf
      Sets the readerIndex of this buffer.
      Overrides:
      readerIndex in class AbstractByteBuf
    • writerIndex

      public CompositeByteBuf writerIndex(int writerIndex)
      Description copied from class: ByteBuf
      Sets the writerIndex of this buffer.
      Overrides:
      writerIndex in class AbstractByteBuf
    • setIndex

      public CompositeByteBuf setIndex(int readerIndex, int writerIndex)
      Description copied from class: ByteBuf
      Sets the readerIndex and writerIndex of this buffer in one shot. This method is useful when you have to worry about the invocation order of ByteBuf.readerIndex(int) and ByteBuf.writerIndex(int) methods. For example, the following code will fail:
       // Create a buffer whose readerIndex, writerIndex and capacity are
       // 0, 0 and 8 respectively.
       ByteBuf buf = Unpooled.buffer(8);
      
       // IndexOutOfBoundsException is thrown because the specified
       // readerIndex (2) cannot be greater than the current writerIndex (0).
       buf.readerIndex(2);
       buf.writerIndex(4);
       
      The following code will also fail:
       // Create a buffer whose readerIndex, writerIndex and capacity are
       // 0, 8 and 8 respectively.
       ByteBuf buf = Unpooled.wrappedBuffer(new byte[8]);
      
       // readerIndex becomes 8.
       buf.readLong();
      
       // IndexOutOfBoundsException is thrown because the specified
       // writerIndex (4) cannot be less than the current readerIndex (8).
       buf.writerIndex(4);
       buf.readerIndex(2);
       
      By contrast, this method guarantees that it never throws an IndexOutOfBoundsException as long as the specified indexes meet basic constraints, regardless what the current index values of the buffer are:
       // No matter what the current state of the buffer is, the following
       // call always succeeds as long as the capacity of the buffer is not
       // less than 4.
       buf.setIndex(2, 4);
       
      Overrides:
      setIndex in class AbstractByteBuf
    • clear

      public CompositeByteBuf clear()
      Description copied from class: ByteBuf
      Sets the readerIndex and writerIndex of this buffer to 0. This method is identical to setIndex(0, 0).

      Please note that the behavior of this method is different from that of NIO buffer, which sets the limit to the capacity of the buffer.

      Overrides:
      clear in class AbstractByteBuf
    • markReaderIndex

      public CompositeByteBuf markReaderIndex()
      Description copied from class: ByteBuf
      Marks the current readerIndex in this buffer. You can reposition the current readerIndex to the marked readerIndex by calling ByteBuf.resetReaderIndex(). The initial value of the marked readerIndex is 0.
      Overrides:
      markReaderIndex in class AbstractByteBuf
    • resetReaderIndex

      public CompositeByteBuf resetReaderIndex()
      Description copied from class: ByteBuf
      Repositions the current readerIndex to the marked readerIndex in this buffer.
      Overrides:
      resetReaderIndex in class AbstractByteBuf
    • markWriterIndex

      public CompositeByteBuf markWriterIndex()
      Description copied from class: ByteBuf
      Marks the current writerIndex in this buffer. You can reposition the current writerIndex to the marked writerIndex by calling ByteBuf.resetWriterIndex(). The initial value of the marked writerIndex is 0.
      Overrides:
      markWriterIndex in class AbstractByteBuf
    • resetWriterIndex

      public CompositeByteBuf resetWriterIndex()
      Description copied from class: ByteBuf
      Repositions the current writerIndex to the marked writerIndex in this buffer.
      Overrides:
      resetWriterIndex in class AbstractByteBuf
    • ensureWritable

      public CompositeByteBuf ensureWritable(int minWritableBytes)
      Description copied from class: ByteBuf
      Expands the buffer ByteBuf.capacity() to make sure the number of writable bytes is equal to or greater than the specified value. If there are enough writable bytes in this buffer, this method returns with no side effect.
      Overrides:
      ensureWritable in class AbstractByteBuf
      Parameters:
      minWritableBytes - the expected minimum number of writable bytes
      See Also:
    • getBytes

      public CompositeByteBuf getBytes(int index, ByteBuf dst)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the specified absolute index until the destination becomes non-writable. This method is basically same with ByteBuf.getBytes(int, ByteBuf, int, int), except that this method increases the writerIndex of the destination by the number of the transferred bytes while ByteBuf.getBytes(int, ByteBuf, int, int) does not. This method does not modify readerIndex or writerIndex of the source buffer (i.e. this).
      Overrides:
      getBytes in class AbstractByteBuf
    • getBytes

      public CompositeByteBuf getBytes(int index, ByteBuf dst, int length)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the specified absolute index. This method is basically same with ByteBuf.getBytes(int, ByteBuf, int, int), except that this method increases the writerIndex of the destination by the number of the transferred bytes while ByteBuf.getBytes(int, ByteBuf, int, int) does not. This method does not modify readerIndex or writerIndex of the source buffer (i.e. this).
      Overrides:
      getBytes in class AbstractByteBuf
      Parameters:
      length - the number of bytes to transfer
    • getBytes

      public CompositeByteBuf getBytes(int index, byte[] dst)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer
      Overrides:
      getBytes in class AbstractByteBuf
    • setBoolean

      public CompositeByteBuf setBoolean(int index, boolean value)
      Description copied from class: ByteBuf
      Sets the specified boolean at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setBoolean in class AbstractByteBuf
    • setChar

      public CompositeByteBuf setChar(int index, int value)
      Description copied from class: ByteBuf
      Sets the specified 2-byte UTF-16 character at the specified absolute index in this buffer. The 16 high-order bits of the specified value are ignored. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setChar in class AbstractByteBuf
    • setFloat

      public CompositeByteBuf setFloat(int index, float value)
      Description copied from class: ByteBuf
      Sets the specified 32-bit floating-point number at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setFloat in class AbstractByteBuf
    • setDouble

      public CompositeByteBuf setDouble(int index, double value)
      Description copied from class: ByteBuf
      Sets the specified 64-bit floating-point number at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setDouble in class AbstractByteBuf
    • setBytes

      public CompositeByteBuf setBytes(int index, ByteBuf src)
      Description copied from class: ByteBuf
      Transfers the specified source buffer's data to this buffer starting at the specified absolute index until the source buffer becomes unreadable. This method is basically same with ByteBuf.setBytes(int, ByteBuf, int, int), except that this method increases the readerIndex of the source buffer by the number of the transferred bytes while ByteBuf.setBytes(int, ByteBuf, int, int) does not. This method does not modify readerIndex or writerIndex of this buffer (i.e. this).
      Overrides:
      setBytes in class AbstractByteBuf
    • setBytes

      public CompositeByteBuf setBytes(int index, ByteBuf src, int length)
      Description copied from class: ByteBuf
      Transfers the specified source buffer's data to this buffer starting at the specified absolute index. This method is basically same with ByteBuf.setBytes(int, ByteBuf, int, int), except that this method increases the readerIndex of the source buffer by the number of the transferred bytes while ByteBuf.setBytes(int, ByteBuf, int, int) does not. This method does not modify readerIndex or writerIndex of this buffer (i.e. this).
      Overrides:
      setBytes in class AbstractByteBuf
      Parameters:
      length - the number of bytes to transfer
    • setBytes

      public CompositeByteBuf setBytes(int index, byte[] src)
      Description copied from class: ByteBuf
      Transfers the specified source array's data to this buffer starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setBytes in class AbstractByteBuf
    • setZero

      public CompositeByteBuf setZero(int index, int length)
      Description copied from class: ByteBuf
      Fills this buffer with NUL (0x00) starting at the specified absolute index. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      setZero in class AbstractByteBuf
      Parameters:
      length - the number of NULs to write to the buffer
    • readBytes

      public CompositeByteBuf readBytes(ByteBuf dst)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination becomes non-writable, and increases the readerIndex by the number of the transferred bytes. This method is basically same with ByteBuf.readBytes(ByteBuf, int, int), except that this method increases the writerIndex of the destination by the number of the transferred bytes while ByteBuf.readBytes(ByteBuf, int, int) does not.
      Overrides:
      readBytes in class AbstractByteBuf
    • readBytes

      public CompositeByteBuf readBytes(ByteBuf dst, int length)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the current readerIndex and increases the readerIndex by the number of the transferred bytes (= length). This method is basically same with ByteBuf.readBytes(ByteBuf, int, int), except that this method increases the writerIndex of the destination by the number of the transferred bytes (= length) while ByteBuf.readBytes(ByteBuf, int, int) does not.
      Overrides:
      readBytes in class AbstractByteBuf
    • readBytes

      public CompositeByteBuf readBytes(ByteBuf dst, int dstIndex, int length)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the current readerIndex and increases the readerIndex by the number of the transferred bytes (= length).
      Overrides:
      readBytes in class AbstractByteBuf
      Parameters:
      dstIndex - the first index of the destination
      length - the number of bytes to transfer
    • readBytes

      public CompositeByteBuf readBytes(byte[] dst)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the current readerIndex and increases the readerIndex by the number of the transferred bytes (= dst.length).
      Overrides:
      readBytes in class AbstractByteBuf
    • readBytes

      public CompositeByteBuf readBytes(byte[] dst, int dstIndex, int length)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the current readerIndex and increases the readerIndex by the number of the transferred bytes (= length).
      Overrides:
      readBytes in class AbstractByteBuf
      Parameters:
      dstIndex - the first index of the destination
      length - the number of bytes to transfer
    • readBytes

      public CompositeByteBuf readBytes(ByteBuffer dst)
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.
      Overrides:
      readBytes in class AbstractByteBuf
    • readBytes

      public CompositeByteBuf readBytes(OutputStream out, int length) throws IOException
      Description copied from class: ByteBuf
      Transfers this buffer's data to the specified stream starting at the current readerIndex.
      Overrides:
      readBytes in class AbstractByteBuf
      Parameters:
      length - the number of bytes to transfer
      Throws:
      IOException - if the specified stream threw an exception during I/O
    • skipBytes

      public CompositeByteBuf skipBytes(int length)
      Description copied from class: ByteBuf
      Increases the current readerIndex by the specified length in this buffer.
      Overrides:
      skipBytes in class AbstractByteBuf
    • writeBoolean

      public CompositeByteBuf writeBoolean(boolean value)
      Description copied from class: ByteBuf
      Sets the specified boolean at the current writerIndex and increases the writerIndex by 1 in this buffer. If this.writableBytes is less than 1, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeBoolean in class AbstractByteBuf
    • writeByte

      public CompositeByteBuf writeByte(int value)
      Description copied from class: ByteBuf
      Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer. The 24 high-order bits of the specified value are ignored. If this.writableBytes is less than 1, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeByte in class AbstractByteBuf
    • writeShort

      public CompositeByteBuf writeShort(int value)
      Description copied from class: ByteBuf
      Sets the specified 16-bit short integer at the current writerIndex and increases the writerIndex by 2 in this buffer. The 16 high-order bits of the specified value are ignored. If this.writableBytes is less than 2, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeShort in class AbstractByteBuf
    • writeMedium

      public CompositeByteBuf writeMedium(int value)
      Description copied from class: ByteBuf
      Sets the specified 24-bit medium integer at the current writerIndex and increases the writerIndex by 3 in this buffer. If this.writableBytes is less than 3, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeMedium in class AbstractByteBuf
    • writeInt

      public CompositeByteBuf writeInt(int value)
      Description copied from class: ByteBuf
      Sets the specified 32-bit integer at the current writerIndex and increases the writerIndex by 4 in this buffer. If this.writableBytes is less than 4, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeInt in class AbstractByteBuf
    • writeLong

      public CompositeByteBuf writeLong(long value)
      Description copied from class: ByteBuf
      Sets the specified 64-bit long integer at the current writerIndex and increases the writerIndex by 8 in this buffer. If this.writableBytes is less than 8, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeLong in class AbstractByteBuf
    • writeChar

      public CompositeByteBuf writeChar(int value)
      Description copied from class: ByteBuf
      Sets the specified 2-byte UTF-16 character at the current writerIndex and increases the writerIndex by 2 in this buffer. The 16 high-order bits of the specified value are ignored. If this.writableBytes is less than 2, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeChar in class AbstractByteBuf
    • writeFloat

      public CompositeByteBuf writeFloat(float value)
      Description copied from class: ByteBuf
      Sets the specified 32-bit floating point number at the current writerIndex and increases the writerIndex by 4 in this buffer. If this.writableBytes is less than 4, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeFloat in class AbstractByteBuf
    • writeDouble

      public CompositeByteBuf writeDouble(double value)
      Description copied from class: ByteBuf
      Sets the specified 64-bit floating point number at the current writerIndex and increases the writerIndex by 8 in this buffer. If this.writableBytes is less than 8, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeDouble in class AbstractByteBuf
    • writeBytes

      public CompositeByteBuf writeBytes(ByteBuf src)
      Description copied from class: ByteBuf
      Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer becomes unreadable, and increases the writerIndex by the number of the transferred bytes. This method is basically same with ByteBuf.writeBytes(ByteBuf, int, int), except that this method increases the readerIndex of the source buffer by the number of the transferred bytes while ByteBuf.writeBytes(ByteBuf, int, int) does not. If this.writableBytes is less than src.readableBytes, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeBytes in class AbstractByteBuf
    • writeBytes

      public CompositeByteBuf writeBytes(ByteBuf src, int length)
      Description copied from class: ByteBuf
      Transfers the specified source buffer's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= length). This method is basically same with ByteBuf.writeBytes(ByteBuf, int, int), except that this method increases the readerIndex of the source buffer by the number of the transferred bytes (= length) while ByteBuf.writeBytes(ByteBuf, int, int) does not. If this.writableBytes is less than length, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeBytes in class AbstractByteBuf
      Parameters:
      length - the number of bytes to transfer
    • writeBytes

      public CompositeByteBuf writeBytes(ByteBuf src, int srcIndex, int length)
      Description copied from class: ByteBuf
      Transfers the specified source buffer's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= length). If this.writableBytes is less than length, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeBytes in class AbstractByteBuf
      Parameters:
      srcIndex - the first index of the source
      length - the number of bytes to transfer
    • writeBytes

      public CompositeByteBuf writeBytes(byte[] src)
      Description copied from class: ByteBuf
      Transfers the specified source array's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= src.length). If this.writableBytes is less than src.length, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeBytes in class AbstractByteBuf
    • writeBytes

      public CompositeByteBuf writeBytes(byte[] src, int srcIndex, int length)
      Description copied from class: ByteBuf
      Transfers the specified source array's data to this buffer starting at the current writerIndex and increases the writerIndex by the number of the transferred bytes (= length). If this.writableBytes is less than length, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeBytes in class AbstractByteBuf
      Parameters:
      srcIndex - the first index of the source
      length - the number of bytes to transfer
    • writeBytes

      public CompositeByteBuf writeBytes(ByteBuffer src)
      Description copied from class: ByteBuf
      Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer's position reaches its limit, and increases the writerIndex by the number of the transferred bytes. If this.writableBytes is less than src.remaining(), ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeBytes in class AbstractByteBuf
    • writeZero

      public CompositeByteBuf writeZero(int length)
      Description copied from class: ByteBuf
      Fills this buffer with NUL (0x00) starting at the current writerIndex and increases the writerIndex by the specified length. If this.writableBytes is less than length, ByteBuf.ensureWritable(int) will be called in an attempt to expand capacity to accommodate.
      Overrides:
      writeZero in class AbstractByteBuf
      Parameters:
      length - the number of NULs to write to the buffer
    • retain

      public CompositeByteBuf retain(int increment)
      Description copied from interface: ReferenceCounted
      Increases the reference count by the specified increment.
      Specified by:
      retain in interface ReferenceCounted
      Overrides:
      retain in class AbstractReferenceCountedByteBuf
    • retain

      public CompositeByteBuf retain()
      Description copied from interface: ReferenceCounted
      Increases the reference count by 1.
      Specified by:
      retain in interface ReferenceCounted
      Overrides:
      retain in class AbstractReferenceCountedByteBuf
    • touch

      public CompositeByteBuf touch()
      Description copied from interface: ReferenceCounted
      Records the current access location of this object for debugging purposes. If this object is determined to be leaked, the information recorded by this operation will be provided to you via ResourceLeakDetector. This method is a shortcut to touch(null).
      Specified by:
      touch in interface ReferenceCounted
      Overrides:
      touch in class AbstractReferenceCountedByteBuf
    • touch

      public CompositeByteBuf touch(Object hint)
      Description copied from interface: ReferenceCounted
      Records the current access location of this object with an additional arbitrary information for debugging purposes. If this object is determined to be leaked, the information recorded by this operation will be provided to you via ResourceLeakDetector.
      Specified by:
      touch in interface ReferenceCounted
      Overrides:
      touch in class AbstractReferenceCountedByteBuf
    • nioBuffers

      public ByteBuffer[] nioBuffers()
      Description copied from class: ByteBuf
      Exposes this buffer's readable bytes as an NIO ByteBuffer's. The returned buffer either share or contains the copied content of this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method does not modify readerIndex or writerIndex of this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer and it adjusted its capacity.
      Overrides:
      nioBuffers in class AbstractByteBuf
      See Also:
    • discardSomeReadBytes

      public CompositeByteBuf discardSomeReadBytes()
      Description copied from class: ByteBuf
      Similar to ByteBuf.discardReadBytes() except that this method might discard some, all, or none of read bytes depending on its internal implementation to reduce overall memory bandwidth consumption at the cost of potentially additional memory consumption.
      Overrides:
      discardSomeReadBytes in class AbstractByteBuf
    • deallocate

      protected void deallocate()
      Description copied from class: AbstractReferenceCountedByteBuf
      Specified by:
      deallocate in class AbstractReferenceCountedByteBuf
    • isAccessible

      boolean isAccessible()
      Description copied from class: ByteBuf
      Used internally by AbstractByteBuf.ensureAccessible() to try to guard against using the buffer after it was released (best-effort).
      Overrides:
      isAccessible in class AbstractReferenceCountedByteBuf
    • unwrap

      public ByteBuf unwrap()
      Description copied from class: ByteBuf
      Return the underlying buffer instance if this buffer is a wrapper of another buffer.
      Specified by:
      unwrap in class ByteBuf
      Returns:
      null if this buffer is not a wrapper
    • clearComps

      private void clearComps()
    • removeComp

      private void removeComp(int i)
    • removeCompRange

      private void removeCompRange(int from, int to)
    • addComp

      private void addComp(int i, CompositeByteBuf.Component c)
    • shiftComps

      private void shiftComps(int i, int count)