org.springframework.core.io.buffer

## 接口 DataBuffer

- 所有已知子接口:
    PooledDataBuffer
  所有已知实现类:
    DefaultDataBuffer, NettyDataBuffer

      public interface DataBuffer

  Basic abstraction over byte buffers.

  `DataBuffer`s has a separate read and write position, as opposed to `ByteBuffer`'s single position. As such, the `DataBuffer` does not require a flip to read after writing. In general, the following invariant holds for the read and write positions, and the capacity:

  > `0` `<=` readPosition `<=` writePosition `<=` capacity

  The capacity of a `DataBuffer` is expanded on demand, similar to `StringBuilder`.

  The main purpose of the `DataBuffer` abstraction is to provide a convenient wrapper around `ByteBuffer` which is similar to Netty's `ByteBuf` but can also be used on non-Netty platforms (i.e. Servlet containers).

  从以下版本开始:
    5.0
  作者:
    Arjen Poutsma
  另请参阅:
    `DataBufferFactory`

- - ### 方法概要

    所有方法 实例方法 抽象方法
    限定符和类型 | 方法和说明
    --- | ---
    `ByteBuffer` | `asByteBuffer()` Expose this buffer's bytes as a `ByteBuffer`.
    `ByteBuffer` | `asByteBuffer(int index, int length)` Expose a subsequence of this buffer's bytes as a `ByteBuffer`.
    `InputStream` | `asInputStream()` Expose this buffer's data as an `InputStream`.
    `InputStream` | `asInputStream(boolean releaseOnClose)` Expose this buffer's data as an `InputStream`.
    `OutputStream` | `asOutputStream()` Expose this buffer's data as an `OutputStream`.
    `int` | `capacity()` Return the number of bytes that this buffer can contain.
    `DataBuffer` | `capacity(int capacity)` Set the number of bytes that this buffer can contain.
    `DataBufferFactory` | `factory()` Return the `DataBufferFactory` that created this buffer.
    `byte` | `getByte(int index)` Read a single byte at the given index from this data buffer.
    `int` | `indexOf(IntPredicate predicate, int fromIndex)` Return the index of the first byte in this buffer that matches the given predicate.
    `int` | `lastIndexOf(IntPredicate predicate, int fromIndex)` Return the index of the last byte in this buffer that matches the given predicate.
    `byte` | `read()` Read a single byte from the current reading position from this data buffer.
    `DataBuffer` | `read(byte[] destination)` Read this buffer's data into the specified destination, starting at the current reading position of this buffer.
    `DataBuffer` | `read(byte[] destination, int offset, int length)` Read at most `length` bytes of this buffer into the specified destination, starting at the current reading position of this buffer.
    `int` | `readableByteCount()` Return the number of bytes that can be read from this data buffer.
    `int` | `readPosition()` Return the position from which this buffer will read.
    `DataBuffer` | `readPosition(int readPosition)` Set the position from which this buffer will read.
    `DataBuffer` | `slice(int index, int length)` Create a new `DataBuffer` whose contents is a shared subsequence of this data buffer's content.
    `int` | `writableByteCount()` Return the number of bytes that can be written to this data buffer.
    `DataBuffer` | `write(byte b)` Write a single byte into this buffer at the current writing position.
    `DataBuffer` | `write(byte[] source)` Write the given source into this buffer, startin at the current writing position of this buffer.
    `DataBuffer` | `write(byte[] source, int offset, int length)` Write at most `length` bytes of the given source into this buffer, starting at the current writing position of this buffer.
    `DataBuffer` | `write(ByteBuffer... buffers)` Write one or more `ByteBuffer` to this buffer, starting at the current writing position.
    `DataBuffer` | `write(DataBuffer... buffers)` Write one or more `DataBuffer`s to this buffer, starting at the current writing position.
    `int` | `writePosition()` Return the position to which this buffer will write.
    `DataBuffer` | `writePosition(int writePosition)` Set the position to which this buffer will write.

- - ### 方法详细资料

    - #### factory

          DataBufferFactory factory()

      Return the `DataBufferFactory` that created this buffer.
      返回:
        the creating buffer factory

    - #### indexOf

          int indexOf(IntPredicate predicate,
                      int fromIndex)

      Return the index of the first byte in this buffer that matches the given predicate.
      参数:
        `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

          int lastIndexOf(IntPredicate predicate,
                          int fromIndex)

      Return the index of the last byte in this buffer that matches the given predicate.
      参数:
        `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

          int readableByteCount()

      Return the number of bytes that can be read from this data buffer.
      返回:
        the readable byte count

    - #### writableByteCount

          int writableByteCount()

      Return the number of bytes that can be written to this data buffer.
      返回:
        the writable byte count
      从以下版本开始:
        5.0.1

    - #### capacity

          int capacity()

      Return the number of bytes that this buffer can contain.
      返回:
        the capacity
      从以下版本开始:
        5.0.1

    - #### capacity

          DataBuffer capacity(int capacity)

      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` - the new capacity
      返回:
        this buffer

    - #### readPosition

          int readPosition()

      Return the position from which this buffer will read.
      返回:
        the read position
      从以下版本开始:
        5.0.1

    - #### readPosition

          DataBuffer readPosition(int readPosition)

      Set the position from which this buffer will read.
      参数:
        `readPosition` - the new read position
      返回:
        this buffer
      抛出:
        `IndexOutOfBoundsException` - if `readPosition` is smaller than 0 or greater than `writePosition()`
      从以下版本开始:
        5.0.1

    - #### writePosition

          int writePosition()

      Return the position to which this buffer will write.
      返回:
        the write position
      从以下版本开始:
        5.0.1

    - #### writePosition

          DataBuffer writePosition(int writePosition)

      Set the position to which this buffer will write.
      参数:
        `writePosition` - the new write position
      返回:
        this buffer
      抛出:
        `IndexOutOfBoundsException` - if `writePosition` is smaller than `readPosition()` or greater than `capacity()`
      从以下版本开始:
        5.0.1

    - #### getByte

          byte getByte(int index)

      Read a single byte at the given index from this data buffer.
      参数:
        `index` - the index at which the byte will be read
      返回:
        the byte at the given index
      抛出:
        `IndexOutOfBoundsException` - when `index` is out of bounds
      从以下版本开始:
        5.0.4

    - #### read

          byte read()

      Read a single byte from the current reading position from this data buffer.
      返回:
        the byte at this buffer's current reading position

    - #### read

          DataBuffer read(byte[] destination)

      Read this buffer's data into the specified destination, starting at the current reading position of this buffer.
      参数:
        `destination` - the array into which the bytes are to be written
      返回:
        this buffer

    - #### read

          DataBuffer read(byte[] destination,
                          int offset,
                          int length)

      Read at most `length` bytes of this buffer into the specified destination, starting at the current reading position of this buffer.
      参数:
        `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

          DataBuffer write(byte b)

      Write a single byte into this buffer at the current writing position.
      参数:
        `b` - the byte to be written
      返回:
        this buffer

    - #### write

          DataBuffer write(byte[] source)

      Write the given source into this buffer, startin at the current writing position of this buffer.
      参数:
        `source` - the bytes to be written into this buffer
      返回:
        this buffer

    - #### write

          DataBuffer write(byte[] source,
                           int offset,
                           int length)

      Write at most `length` bytes of the given source into this buffer, starting at the current writing position of this buffer.
      参数:
        `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

          DataBuffer write(DataBuffer... buffers)

      Write one or more `DataBuffer`s to this buffer, starting at the current writing position. It is the responsibility of the caller to release the given data buffers.
      参数:
        `buffers` - the byte buffers to write into this buffer
      返回:
        this buffer

    - #### write

          DataBuffer write(ByteBuffer... buffers)

      Write one or more `ByteBuffer` to this buffer, starting at the current writing position.
      参数:
        `buffers` - the byte buffers to write into this buffer
      返回:
        this buffer

    - #### slice

          DataBuffer slice(int index,
                           int length)

      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.
      参数:
        `index` - the index at which to start the slice
        `length` - the length of the slice
      返回:
        the specified slice of this data buffer

    - #### asByteBuffer

          ByteBuffer asByteBuffer()

      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.
      返回:
        this data buffer as a byte buffer

    - #### asByteBuffer

          ByteBuffer asByteBuffer(int index,
                                  int length)

      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.
      参数:
        `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
      从以下版本开始:
        5.0.1

    - #### asInputStream

          InputStream asInputStream()

      Expose this buffer's data as an `InputStream`. Both data and read position are shared between the returned stream and this data buffer. The underlying buffer will not be released when the input stream is closed.
      返回:
        this data buffer as an input stream
      另请参阅:
        `asInputStream(boolean)`

    - #### asInputStream

          InputStream asInputStream(boolean releaseOnClose)

      Expose this buffer's data as an `InputStream`. Both data and read position are shared between the returned stream and this data buffer.
      参数:
        `releaseOnClose` - whether the underlying buffer will be released when the input stream is closed.
      返回:
        this data buffer as an input stream
      从以下版本开始:
        5.0.4

    - #### asOutputStream

          OutputStream asOutputStream()

      Expose this buffer's data as an `OutputStream`. Both data and write position are shared between the returned stream and this data buffer.
      返回:
        this data buffer as an output stream