类 DefaultDataBuffer

java.lang.Object
cn.taketoday.core.io.buffer.DefaultDataBuffer
所有已实现的接口:
DataBuffer
直接已知子类:
DefaultDataBuffer.SlicedDefaultDataBuffer

public class DefaultDataBuffer extends Object implements DataBuffer
Default implementation of the DataBuffer interface that uses a ByteBuffer internally. with separate read and write positions. Constructed using the DefaultDataBufferFactory.

Inspired by Netty's ByteBuf. Introduced so that non-Netty runtimes (i.e. Servlet) do not require Netty on the classpath.

从以下版本开始:
4.0
作者:
Arjen Poutsma, Juergen Hoeller, Brian Clozel, Harry Yang
另请参阅:
  • 字段详细资料

    • MAX_CAPACITY

      private static final int MAX_CAPACITY
      另请参阅:
    • CAPACITY_THRESHOLD

      private static final int CAPACITY_THRESHOLD
      另请参阅:
    • dataBufferFactory

      private final DefaultDataBufferFactory dataBufferFactory
    • byteBuffer

      private ByteBuffer byteBuffer
    • capacity

      private int capacity
    • readPosition

      private int readPosition
    • writePosition

      private int writePosition
  • 构造器详细资料

  • 方法详细资料

    • fromFilledByteBuffer

      static DefaultDataBuffer fromFilledByteBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer)
    • fromEmptyByteBuffer

      static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer)
    • getNativeBuffer

      public ByteBuffer getNativeBuffer()
      Directly exposes the native ByteBuffer that this buffer is based on also updating the ByteBuffer's position and limit to match the current readPosition() and readableByteCount().
      返回:
      the wrapped byte buffer
    • setNativeBuffer

      private void setNativeBuffer(ByteBuffer byteBuffer)
    • factory

      public DefaultDataBufferFactory factory()
      从接口复制的说明: DataBuffer
      Return the DataBufferFactory that created this buffer.
      指定者:
      factory 在接口中 DataBuffer
      返回:
      the creating buffer factory
    • indexOf

      public int indexOf(IntPredicate predicate, int fromIndex)
      从接口复制的说明: DataBuffer
      Return the index of the first byte in this buffer that matches the given predicate.
      指定者:
      indexOf 在接口中 DataBuffer
      参数:
      predicate - the predicate to match
      fromIndex - the index to start the search from
      返回:
      the index of the first byte that matches predicate; or -1 if none match
    • lastIndexOf

      public int lastIndexOf(IntPredicate predicate, int fromIndex)
      从接口复制的说明: DataBuffer
      Return the index of the last byte in this buffer that matches the given predicate.
      指定者:
      lastIndexOf 在接口中 DataBuffer
      参数:
      predicate - the predicate to match
      fromIndex - the index to start the search from
      返回:
      the index of the last byte that matches predicate; or -1 if none match
    • readableByteCount

      public int readableByteCount()
      从接口复制的说明: DataBuffer
      Return the number of bytes that can be read from this data buffer.
      指定者:
      readableByteCount 在接口中 DataBuffer
      返回:
      the readable byte count
    • writableByteCount

      public int writableByteCount()
      从接口复制的说明: DataBuffer
      Return the number of bytes that can be written to this data buffer.
      指定者:
      writableByteCount 在接口中 DataBuffer
      返回:
      the writable byte count
    • readPosition

      public int readPosition()
      从接口复制的说明: DataBuffer
      Return the position from which this buffer will read.
      指定者:
      readPosition 在接口中 DataBuffer
      返回:
      the read position
    • readPosition

      public DefaultDataBuffer readPosition(int readPosition)
      从接口复制的说明: DataBuffer
      Set the position from which this buffer will read.
      指定者:
      readPosition 在接口中 DataBuffer
      参数:
      readPosition - the new read position
      返回:
      this buffer
    • writePosition

      public int writePosition()
      从接口复制的说明: DataBuffer
      Return the position to which this buffer will write.
      指定者:
      writePosition 在接口中 DataBuffer
      返回:
      the write position
    • writePosition

      public DefaultDataBuffer writePosition(int writePosition)
      从接口复制的说明: DataBuffer
      Set the position to which this buffer will write.
      指定者:
      writePosition 在接口中 DataBuffer
      参数:
      writePosition - the new write position
      返回:
      this buffer
    • capacity

      public int capacity()
      从接口复制的说明: DataBuffer
      Return the number of bytes that this buffer can contain.
      指定者:
      capacity 在接口中 DataBuffer
      返回:
      the capacity
    • capacity

      public DataBuffer capacity(int capacity)
      从接口复制的说明: DataBuffer
      Set the number of bytes that this buffer can contain.

      If the new capacity is lower than the current capacity, the contents of this buffer will be truncated. If the new capacity is higher than the current capacity, it will be expanded.

      指定者:
      capacity 在接口中 DataBuffer
      参数:
      capacity - the new capacity
      返回:
      this buffer
      另请参阅:
    • setCapacity

      private void setCapacity(int newCapacity)
    • ensureWritable

      public DataBuffer ensureWritable(int length)
      从接口复制的说明: DataBuffer
      Ensure that the current buffer has enough DataBuffer.writableByteCount() to write the amount of data given as an argument. If not, the missing capacity will be added to the buffer.
      指定者:
      ensureWritable 在接口中 DataBuffer
      参数:
      length - the writable capacity to check for
      返回:
      this buffer
    • allocate

      private static ByteBuffer allocate(int capacity, boolean direct)
    • getByte

      public byte getByte(int index)
      从接口复制的说明: DataBuffer
      Read a single byte at the given index from this data buffer.
      指定者:
      getByte 在接口中 DataBuffer
      参数:
      index - the index at which the byte will be read
      返回:
      the byte at the given index
    • read

      public byte read()
      从接口复制的说明: DataBuffer
      Read a single byte from the current reading position from this data buffer.
      指定者:
      read 在接口中 DataBuffer
      返回:
      the byte at this buffer's current reading position
    • read

      public DefaultDataBuffer read(byte[] destination)
      从接口复制的说明: DataBuffer
      Read this buffer's data into the specified destination, starting at the current reading position of this buffer.
      指定者:
      read 在接口中 DataBuffer
      参数:
      destination - the array into which the bytes are to be written
      返回:
      this buffer
    • read

      public DefaultDataBuffer read(byte[] destination, int offset, int length)
      从接口复制的说明: DataBuffer
      Read at most length bytes of this buffer into the specified destination, starting at the current reading position of this buffer.
      指定者:
      read 在接口中 DataBuffer
      参数:
      destination - the array into which the bytes are to be written
      offset - the index within destination of the first byte to be written
      length - the maximum number of bytes to be written in destination
      返回:
      this buffer
    • write

      public DefaultDataBuffer write(byte b)
      从接口复制的说明: DataBuffer
      Write a single byte into this buffer at the current writing position.
      指定者:
      write 在接口中 DataBuffer
      参数:
      b - the byte to be written
      返回:
      this buffer
    • write

      public DefaultDataBuffer write(byte[] source)
      从接口复制的说明: DataBuffer
      Write the given source into this buffer, starting at the current writing position of this buffer.
      指定者:
      write 在接口中 DataBuffer
      参数:
      source - the bytes to be written into this buffer
      返回:
      this buffer
    • write

      public DefaultDataBuffer write(byte[] source, int offset, int length)
      从接口复制的说明: DataBuffer
      Write at most length bytes of the given source into this buffer, starting at the current writing position of this buffer.
      指定者:
      write 在接口中 DataBuffer
      参数:
      source - the bytes to be written into this buffer
      offset - the index within source to start writing from
      length - the maximum number of bytes to be written from source
      返回:
      this buffer
    • write

      public DefaultDataBuffer write(DataBuffer... dataBuffers)
      从接口复制的说明: DataBuffer
      Write one or more DataBuffers to this buffer, starting at the current writing position. It is the responsibility of the caller to release the given data buffers.
      指定者:
      write 在接口中 DataBuffer
      参数:
      dataBuffers - the byte buffers to write into this buffer
      返回:
      this buffer
    • write

      public DefaultDataBuffer write(ByteBuffer... buffers)
      从接口复制的说明: DataBuffer
      Write one or more ByteBuffer to this buffer, starting at the current writing position.
      指定者:
      write 在接口中 DataBuffer
      参数:
      buffers - the byte buffers to write into this buffer
      返回:
      this buffer
    • write

      private void write(ByteBuffer source)
    • slice

      public DefaultDataBuffer slice(int index, int length)
      从接口复制的说明: DataBuffer
      Create a new DataBuffer whose contents is a shared subsequence of this data buffer's content. Data between this data buffer and the returned buffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.

      Note that this method will not call DataBufferUtils.retain(DataBuffer) on the resulting slice: the reference count will not be increased.

      指定者:
      slice 在接口中 DataBuffer
      参数:
      index - the index at which to start the slice
      length - the length of the slice
      返回:
      the specified slice of this data buffer
      另请参阅:
    • split

      public DataBuffer split(int index)
      从接口复制的说明: DataBuffer
      Splits this data buffer into two at the given index.

      Data that precedes the index will be returned in a new buffer, while this buffer will contain data that follows after index. Memory between the two buffers is shared, but independent and cannot overlap (unlike slice).

      The read and write position of the returned buffer are truncated to fit within the buffers capacity if necessary. The positions of this buffer are set to 0 if they are smaller than index.

      指定者:
      split 在接口中 DataBuffer
      参数:
      index - the index at which it should be split
      返回:
      a new data buffer, containing the bytes from index 0 to index
    • asByteBuffer

      public ByteBuffer asByteBuffer()
      从接口复制的说明: DataBuffer
      Expose this buffer's bytes as a ByteBuffer. Data between this DataBuffer and the returned ByteBuffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.

      DataBuffer.toByteBuffer(), which does not share data and returns a copy.

      指定者:
      asByteBuffer 在接口中 DataBuffer
      返回:
      this data buffer as a byte buffer
    • asByteBuffer

      public ByteBuffer asByteBuffer(int index, int length)
      从接口复制的说明: DataBuffer
      Expose a subsequence of this buffer's bytes as a ByteBuffer. Data between this DataBuffer and the returned ByteBuffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.

      in favor of DataBuffer.toByteBuffer(int, int), which does not share data and returns a copy.

      指定者:
      asByteBuffer 在接口中 DataBuffer
      参数:
      index - the index at which to start the byte buffer
      length - the length of the returned byte buffer
      返回:
      this data buffer as a byte buffer
    • toByteBuffer

      public ByteBuffer toByteBuffer(int index, int length)
      从接口复制的说明: DataBuffer
      Returns a ByteBuffer representation of a subsequence of this buffer's bytes. Data between this DataBuffer and the returned ByteBuffer is not shared.
      指定者:
      toByteBuffer 在接口中 DataBuffer
      返回:
      this data buffer as a byte buffer
    • toByteBuffer

      public void toByteBuffer(int srcPos, ByteBuffer dest, int destPos, int length)
      从接口复制的说明: DataBuffer
      Copies the given length from this data buffer into the given destination ByteBuffer, beginning at the given source position, and the given destination position in the destination byte buffer.
      指定者:
      toByteBuffer 在接口中 DataBuffer
      参数:
      srcPos - the position of this data buffer from where copying should start
      dest - the destination byte buffer
      destPos - the position in dest to where copying should start
      length - the amount of data to copy
    • readableByteBuffers

      public DataBuffer.ByteBufferIterator readableByteBuffers()
      从接口复制的说明: DataBuffer
      Returns a closeable iterator over each ByteBuffer in this data buffer that can be read. Calling this method is more efficient than DataBuffer.toByteBuffer(), as no data is copied. However, the byte buffers provided can only be used during the iteration.

      Note that the returned iterator must be used in a try-with-resources clause or explicitly closed.

      指定者:
      readableByteBuffers 在接口中 DataBuffer
      返回:
      a closeable iterator over the readable byte buffers contained in this data buffer
    • writableByteBuffers

      public DataBuffer.ByteBufferIterator writableByteBuffers()
      从接口复制的说明: DataBuffer
      Returns a closeable iterator over each ByteBuffer in this data buffer that can be written to. The byte buffers provided can only be used during the iteration.

      Note that the returned iterator must be used in a try-with-resources clause or explicitly closed.

      指定者:
      writableByteBuffers 在接口中 DataBuffer
      返回:
      a closeable iterator over the writable byte buffers contained in this data buffer
    • toString

      public String toString(int index, int length, Charset charset)
      从接口复制的说明: DataBuffer
      Return a part of this buffer's data as a String using the specified charset.
      指定者:
      toString 在接口中 DataBuffer
      参数:
      index - the index at which to start the string
      length - the number of bytes to use for the string
      charset - the charset to use
      返回:
      a string representation of a part of this buffers data
    • calculateCapacity

      private int calculateCapacity(int neededCapacity)
      Calculate the capacity of the buffer.
      另请参阅:
      • AbstractByteBufAllocator.calculateNewCapacity(int, int)
    • equals

      public boolean equals(@Nullable Object other)
      覆盖:
      equals 在类中 Object
    • hashCode

      public int hashCode()
      覆盖:
      hashCode 在类中 Object
    • toString

      public String toString()
      覆盖:
      toString 在类中 Object
    • checkIndex

      private void checkIndex(int index, int length)
    • checkIndex

      private void checkIndex(int index)
    • checkLength

      private void checkLength(int length)
    • assertIndex

      private void assertIndex(boolean expression, String format, Object... args)