Class UnpooledDirectByteBuf

All Implemented Interfaces:
ByteBufConvertible, ReferenceCounted, Comparable<ByteBuf>
Direct Known Subclasses:
ByteBufUtil.ThreadLocalDirectByteBuf, UnpooledByteBufAllocator.InstrumentedUnpooledDirectByteBuf, UnpooledUnsafeDirectByteBuf

public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf
A NIO ByteBuffer based buffer. It is recommended to use AbstractByteBufAllocator.directBuffer(int, int), Unpooled.directBuffer(int) and Unpooled.wrappedBuffer(ByteBuffer) instead of calling the constructor explicitly.
  • Field Details

    • alloc

      private final ByteBufAllocator alloc
    • buffer

      ByteBuffer buffer
    • tmpNioBuf

      private ByteBuffer tmpNioBuf
    • capacity

      private int capacity
    • doNotFree

      private boolean doNotFree
  • Constructor Details

    • UnpooledDirectByteBuf

      public UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity)
      Creates a new direct buffer.
      Parameters:
      initialCapacity - the initial capacity of the underlying direct buffer
      maxCapacity - the maximum capacity of the underlying direct buffer
    • UnpooledDirectByteBuf

      protected UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity)
      Creates a new direct buffer by wrapping the specified initial buffer.
      Parameters:
      maxCapacity - the maximum capacity of the underlying direct buffer
    • UnpooledDirectByteBuf

      UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity, boolean doFree, boolean slice)
  • Method Details

    • allocateDirect

      protected ByteBuffer allocateDirect(int initialCapacity)
      Allocate a new direct ByteBuffer with the given initialCapacity.
    • freeDirect

      protected void freeDirect(ByteBuffer buffer)
      Free a direct ByteBuffer
    • setByteBuffer

      void setByteBuffer(ByteBuffer buffer, boolean tryFree)
    • 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
    • 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 ByteBuf 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
    • 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
    • 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

      public short getShort(int index)
      Description copied from class: ByteBuf
      Gets a 16-bit short integer at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      getShort 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

      public int getUnsignedMedium(int index)
      Description copied from class: ByteBuf
      Gets an unsigned 24-bit medium integer at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      getUnsignedMedium 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

      public int getInt(int index)
      Description copied from class: ByteBuf
      Gets a 32-bit integer at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      getInt 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

      public long getLong(int index)
      Description copied from class: ByteBuf
      Gets a 64-bit long integer at the specified absolute index in this buffer. This method does not modify readerIndex or writerIndex of this buffer.
      Overrides:
      getLong 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 ByteBuf 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 ByteBuf 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

      void getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal)
    • readBytes

      public ByteBuf 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
    • getBytes

      public ByteBuf 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

      void getBytes(int index, ByteBuffer dst, boolean internal)
    • readBytes

      public ByteBuf 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
    • setByte

      public ByteBuf 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 ByteBuf 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 ByteBuf 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 ByteBuf 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 ByteBuf 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 ByteBuf 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 ByteBuf 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 ByteBuf 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
    • getBytes

      public ByteBuf 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
    • getBytes

      void getBytes(int index, OutputStream out, int length, boolean internal) throws IOException
      Throws:
      IOException
    • readBytes

      public ByteBuf 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
    • 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

      private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException
      Throws:
      IOException
    • 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

      private int getBytes(int index, FileChannel out, long position, int length, boolean internal) throws IOException
      Throws:
      IOException
    • readBytes

      public int readBytes(GatheringByteChannel 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 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
    • readBytes

      public int readBytes(FileChannel out, long position, int length) throws IOException
      Description copied from class: ByteBuf
      Transfers this buffer's data starting at the current readerIndex to the specified channel starting at the given file position. This method does not modify the channel's position.
      Overrides:
      readBytes in class AbstractByteBuf
      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
    • 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
    • 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:
    • 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:
    • isContiguous

      public final boolean isContiguous()
      Description copied from class: ByteBuf
      Returns true if this ByteBuf implementation is backed by a single memory region. Composite buffer implementations must return false even if they currently hold ≤ 1 components. For buffers that return true, it's guaranteed that a successful call to ByteBuf.discardReadBytes() will increase the value of ByteBuf.maxFastWritableBytes() by the current readerIndex.

      This method will return false by default, and a false return value does not necessarily mean that the implementation is composite or that it is not backed by a single memory region.

      Overrides:
      isContiguous in class ByteBuf
    • 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
    • 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
    • internalNioBuffer

      private ByteBuffer internalNioBuffer()
    • 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:
    • deallocate

      protected void deallocate()
      Description copied from class: AbstractReferenceCountedByteBuf
      Specified by:
      deallocate 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