org.apache.commons.io.input.buffer

## Class CircularBufferInputStream

- java.lang.Object
- - java.io.InputStream
  - - java.io.FilterInputStream
    - - org.apache.commons.io.input.buffer.CircularBufferInputStream

- All Implemented Interfaces:
    Closeable, AutoCloseable
  Direct Known Subclasses:
    PeekableInputStream

      public class CircularBufferInputStream
      extends FilterInputStream

  Implements a buffered input stream, which is internally based on a `CircularByteBuffer`. Unlike the `BufferedInputStream`, this one doesn't need to reallocate byte arrays internally.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected CircularByteBuffer` | `buffer` Internal buffer.
    `protected int` | `bufferSize` Internal buffer size.

    - ### Fields inherited from class java.io.FilterInputStream

      `in`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CircularBufferInputStream(InputStream inputStream)` Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size (`IOUtils.DEFAULT_BUFFER_SIZE`).
    `CircularBufferInputStream(InputStream inputStream, int bufferSize)` Creates a new instance, which filters the given input stream, and uses the given buffer size.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `protected void` | `fillBuffer()` Fills the buffer with the contents of the input stream.
    `protected boolean` | `haveBytes(int count)` Fills the buffer from the input stream until the given number of bytes have been added to the buffer.
    `int` | `read()`
    `int` | `read(byte[] targetBuffer, int offset, int length)`

    - ### Methods inherited from class java.io.FilterInputStream

      `available, mark, markSupported, read, reset, skip`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### buffer

          protected final CircularByteBuffer buffer

      Internal buffer.

    - #### bufferSize

          protected final int bufferSize

      Internal buffer size.

  - ### Constructor Detail

    - #### CircularBufferInputStream

          public CircularBufferInputStream(InputStream inputStream)

      Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size (`IOUtils.DEFAULT_BUFFER_SIZE`).
      Parameters:
        `inputStream` - The input stream, which is being buffered.

    - #### CircularBufferInputStream

          public CircularBufferInputStream(InputStream inputStream,
                                           int bufferSize)

      Creates a new instance, which filters the given input stream, and uses the given buffer size.
      Parameters:
        `inputStream` - The input stream, which is being buffered.
        `bufferSize` - The size of the `CircularByteBuffer`, which is used internally.

  - ### Method Detail

    - #### close

          public void close()
                     throws IOException

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Overrides:
        `close` in class `FilterInputStream`
      Throws:
        `IOException`

    - #### fillBuffer

          protected void fillBuffer()
                             throws IOException

      Fills the buffer with the contents of the input stream.
      Throws:
        `IOException` - in case of an error while reading from the input stream.

    - #### haveBytes

          protected boolean haveBytes(int count)
                               throws IOException

      Fills the buffer from the input stream until the given number of bytes have been added to the buffer.
      Parameters:
        `count` - number of byte to fill into the buffer
      Returns:
        true if the buffer has bytes
      Throws:
        `IOException` - in case of an error while reading from the input stream.

    - #### read

          public int read()
                   throws IOException

      Overrides:
        `read` in class `FilterInputStream`
      Throws:
        `IOException`

    - #### read

          public int read(byte[] targetBuffer,
                          int offset,
                          int length)
                   throws IOException

      Overrides:
        `read` in class `FilterInputStream`
      Throws:
        `IOException`

Copyright © 2002–2023 The Apache Software Foundation. All rights reserved.