Package org.apache.commons.io.input

# Class UnsynchronizedBufferedInputStream

java.lang.Object
java.io.InputStream
org.apache.commons.io.input.UnsynchronizedFilterInputStream
org.apache.commons.io.input.UnsynchronizedBufferedInputStream

All Implemented Interfaces:
  `Closeable`, `AutoCloseable`

public final class UnsynchronizedBufferedInputStream extends UnsynchronizedFilterInputStream
An unsynchronized version of `BufferedInputStream`, not thread-safe.

Wraps an existing `InputStream` and buffers the input. Expensive interaction with the underlying input stream is minimized, since most (smaller) requests can be satisfied by accessing the buffer alone. The drawback is that some extra space is required to hold the buffer and that copying takes place when filling that buffer, but this is usually outweighed by the performance benefits.

To build an instance, see `UnsynchronizedBufferedInputStream.Builder`.

A typical application pattern for the class looks like this:

     UnsynchronizedBufferedInputStream s = new UnsynchronizedBufferedInputStream.Builder().
       .setInputStream(new FileInputStream("file.java"))
       .setBufferSize(8192)
       .get();

Provenance: Apache Harmony and modified.

Since:
  2.12.0
See Also:
  - `BufferedInputStream`

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `UnsynchronizedBufferedInputStream.Builder`
  Builds a new `UnsynchronizedBufferedInputStream` instance.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected byte[]`
  `buffer`
  The buffer containing the current bytes read from the target InputStream.
  `protected int`
  `count`
  The total number of bytes inside the byte array `buffer`.
  `protected int`
  `markLimit`
  The current limit, which when passed, invalidates the current mark.
  `protected int`
  `markPos`
  The currently marked position.
  `protected int`
  `pos`
  The current position within the byte array `buffer`.

  ### Fields inherited from class org.apache.commons.io.input.UnsynchronizedFilterInputStream

  `inputStream`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `available()`
  Returns the number of bytes that are available before this stream will block.
  `void`
  `close()`
  Closes this stream.
  `void`
  `mark(int readLimit)`
  Sets a mark position in this stream.
  `boolean`
  `markSupported()`
  Indicates whether `BufferedInputStream` supports the `mark()` and `reset()` methods.
  `int`
  `read()`
  Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.
  `int`
  `read(byte[] dest, int offset, int length)`
  Reads at most `length` bytes from this stream and stores them in byte array `buffer` starting at offset `offset`.
  `void`
  `reset()`
  Resets this stream to the last marked location.
  `long`
  `skip(long amount)`
  Skips `amount` number of bytes in this stream.

  ### Methods inherited from class org.apache.commons.io.input.UnsynchronizedFilterInputStream

  `builder, read`

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

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

- ## Field Details

  - ### buffer

    protected volatile byte[] buffer
    The buffer containing the current bytes read from the target InputStream.

  - ### count

    protected int count
    The total number of bytes inside the byte array `buffer`.

  - ### markLimit

    protected int markLimit
    The current limit, which when passed, invalidates the current mark.

  - ### markPos

    protected int markPos
    The currently marked position. -1 indicates no mark has been set or the mark has been invalidated.

  - ### pos

    protected int pos
    The current position within the byte array `buffer`.

- ## Method Details

  - ### available

    public int available() throws IOException
    Returns the number of bytes that are available before this stream will block. This method returns the number of bytes available in the buffer plus those available in the source stream.
    Overrides:
      `available` in class `UnsynchronizedFilterInputStream`
    Returns:
      the number of bytes available before blocking.
    Throws:
      `IOException` - if this stream is closed.

  - ### close

    public void close() throws IOException
    Closes this stream. The source stream is closed and any resources associated with it are released.
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`
    Overrides:
      `close` in class `UnsynchronizedFilterInputStream`
    Throws:
      `IOException` - if an error occurs while closing this stream.

  - ### mark

    public void mark(int readLimit)
    Sets a mark position in this stream. The parameter `readLimit` indicates how many bytes can be read before a mark is invalidated. Calling `reset()` will reposition the stream back to the marked position if `readLimit` has not been surpassed. The underlying buffer may be increased in size to allow `readLimit` number of bytes to be supported.
    Overrides:
      `mark` in class `UnsynchronizedFilterInputStream`
    Parameters:
      `readLimit` - the number of bytes that can be read before the mark is invalidated.
    See Also:
      - `reset()`

  - ### markSupported

    public boolean markSupported()
    Indicates whether `BufferedInputStream` supports the `mark()` and `reset()` methods.
    Overrides:
      `markSupported` in class `UnsynchronizedFilterInputStream`
    Returns:
      `true` for BufferedInputStreams.
    See Also:
      - `mark(int)`
      - `reset()`

  - ### read

    public int read() throws IOException
    Reads a single byte from this stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the source string has been reached. If the internal buffer does not contain any available bytes then it is filled from the source stream and the first byte is returned.
    Overrides:
      `read` in class `UnsynchronizedFilterInputStream`
    Returns:
      the byte read or -1 if the end of the source stream has been reached.
    Throws:
      `IOException` - if this stream is closed or another IOException occurs.

  - ### read

    public int read(byte[] dest, int offset, int length) throws IOException
    Reads at most `length` bytes from this stream and stores them in byte array `buffer` starting at offset `offset`. Returns the number of bytes actually read or -1 if no bytes were read and the end of the stream was encountered. If all the buffered bytes have been used, a mark has not been set and the requested number of bytes is larger than the receiver's buffer size, this implementation bypasses the buffer and simply places the results directly into `buffer`.
    Overrides:
      `read` in class `UnsynchronizedFilterInputStream`
    Parameters:
      `dest` - the byte array in which to store the bytes read.
      `offset` - the initial position in `buffer` to store the bytes read from this stream.
      `length` - the maximum number of bytes to store in `buffer`.
    Returns:
      the number of bytes actually read or -1 if end of stream.
    Throws:
      `IndexOutOfBoundsException` - if `offset < 0` or `length < 0`, or if `offset + length` is greater than the size of `buffer`.
      `IOException` - if the stream is already closed or another IOException occurs.

  - ### reset

    public void reset() throws IOException
    Resets this stream to the last marked location.
    Overrides:
      `reset` in class `UnsynchronizedFilterInputStream`
    Throws:
      `IOException` - if this stream is closed, no mark has been set or the mark is no longer valid because more than `readLimit` bytes have been read since setting the mark.
    See Also:
      - `mark(int)`

  - ### skip

    public long skip(long amount) throws IOException
    Skips `amount` number of bytes in this stream. Subsequent `read()`'s will not return these bytes unless `reset()` is used.
    Overrides:
      `skip` in class `UnsynchronizedFilterInputStream`
    Parameters:
      `amount` - the number of bytes to skip. `skip` does nothing and returns 0 if `amount` is less than zero.
    Returns:
      the number of bytes actually skipped.
    Throws:
      `IOException` - if this stream is closed or another IOException occurs.
    See Also:
      - `UnsynchronizedFilterInputStream.mark(int)`
      - `UnsynchronizedFilterInputStream.reset()`