# Class BufferedInputStream

java.lang.Object
java.io.InputStream
com.codename1.io.BufferedInputStream

All Implemented Interfaces:
  `AutoCloseable`

Direct Known Subclasses:
  `TarInputStream`

public class BufferedInputStream extends InputStream
Based on the buffered input stream from the JDK with some minor tweaks to allow external classes to monitor stream status and progress.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `BufferedInputStream(InputStream in)`
  Creates a `BufferedInputStream` and saves its argument, the input stream `in`, for later use.
  `BufferedInputStream(InputStream in, int size)`
  Creates a `BufferedInputStream` with the specified buffer size, and saves its argument, the input stream `in`, for later use.
  `BufferedInputStream(InputStream in, int size, String name)`
  Creates a `BufferedInputStream` with the specified buffer size, and saves its argument, the input stream `in`, for later use.
  `BufferedInputStream(InputStream in, String name)`
  Creates a `BufferedInputStream` and saves its argument, the input stream `in`, for later use.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `available()`
  Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
  `void`
  `close()`
  Closes this input stream and releases any system resources associated with the stream.
  `Object`
  `getConnection()`
  If applicable this member represents the connection object for the stream
  `static int`
  `getDefaultBufferSize()`
  The default size for a stream buffer
  `InputStream`
  `getInternal()`
  Allows access to the underlying input stream if desired
  `long`
  `getLastActivityTime()`
  Returns the time of the last activity
  `String`
  `getName()`
  Indicates the name of the stream for debugging purposes
  `int`
  `getTotalBytesRead()`
  Returns the total number of bytes read from this stream so far
  `int`
  `getYield()`
  Allows setting a yield duration for this stream which is useful for background operations to release CPU
  `boolean`
  `isDisableBuffering()`

  ###### Returns

  `boolean`
  `isPrintInput()`
  Prints out all the data that passes through this stream to the console.
  `void`
  `mark(int readlimit)`
  See the general contract of the `mark` method of `InputStream`.
  `boolean`
  `markSupported()`
  Tests if this input stream supports the `mark` and `reset` methods.
  `int`
  `read()`
  See the general contract of the `read` method of `InputStream`.
  `int`
  `read(byte[] b)`
  Reads some number of bytes from the input stream and stores them into the buffer array b.
  `int`
  `read(byte[] b, int off, int len)`
  Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
  `void`
  `reset()`
  See the general contract of the `reset` method of `InputStream`.
  `void`
  `setConnection(Object connection)`
  If applicable this member represents the connection object for the stream
  `static void`
  `setDefaultBufferSize(int aDefaultBufferSize)`
  The default size for a stream buffer
  `void`
  `setDisableBuffering(boolean disableBuffering)`

  ###### Parameters

  `void`
  `setPrintInput(boolean printInput)`
  Prints out all the data that passes through this stream to the console.
  `void`
  `setProgressListener(IOProgressListener progressListener)`
  Sets the callback for IO updates from a buffered stream
  `void`
  `setYield(int yield)`
  Allows setting a yield duration for this stream which is useful for background operations to release CPU
  `long`
  `skip(long n)`
  See the general contract of the `skip` method of `InputStream`.
  `void`
  `stop()`
  Stop reading from the stream, invoking this will cause the read() to return -1

  ### Methods inherited from class Object

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

- ## Constructor Details

  - ### BufferedInputStream

    public BufferedInputStream(InputStream in)

    Creates a `BufferedInputStream` and saves its argument, the input stream `in`, for later use. An internal buffer array is created and stored in `buf`.

    ###### Parameters

    - `in`: the underlying input stream.

  - ### BufferedInputStream

    public BufferedInputStream(InputStream in, String name)

    Creates a `BufferedInputStream` and saves its argument, the input stream `in`, for later use. An internal buffer array is created and stored in `buf`.

    ###### Parameters

    - `in`: the underlying input stream.

    - `name`: the name of the stream

  - ### BufferedInputStream

    public BufferedInputStream(InputStream in, int size)

    Creates a `BufferedInputStream` with the specified buffer size, and saves its argument, the input stream `in`, for later use. An internal buffer array of length `size` is created and stored in `buf`.

    ###### Parameters

    - `in`: the underlying input stream.

    - `size`: the buffer size.

    ###### Throws

    - `IllegalArgumentException`: if size <= 0.

  - ### BufferedInputStream

    public BufferedInputStream(InputStream in, int size, String name)

    Creates a `BufferedInputStream` with the specified buffer size, and saves its argument, the input stream `in`, for later use. An internal buffer array of length `size` is created and stored in `buf`.

    ###### Parameters

    - `in`: the underlying input stream.

    - `size`: the buffer size.

    - `name`: the name of the stream

    ###### Throws

    - `IllegalArgumentException`: if size <= 0.

- ## Method Details

  - ### getDefaultBufferSize

    public static int getDefaultBufferSize()

    The default size for a stream buffer

    ###### Returns

    the defaultBufferSize

  - ### setDefaultBufferSize

    public static void setDefaultBufferSize(int aDefaultBufferSize)

    The default size for a stream buffer

    ###### Parameters

    - `aDefaultBufferSize`: the defaultBufferSize to set

  - ### getName

    public String getName()

    Indicates the name of the stream for debugging purposes

    ###### Returns

    the name of the stream

  - ### getInternal

    public InputStream getInternal()

    Allows access to the underlying input stream if desired

    ###### Returns

    the internal input stream

  - ### read

    public int read() throws IOException

    See the general contract of the `read` method of `InputStream`.

    ###### Returns

    Specified by:
      `read` in class `InputStream`
    Returns:

      the next byte of data, or `-1` if the end of the stream is reached.

      ###### Throws

      - `IOException`: @throws IOException if this input stream has been closed by invoking its `#close()` method, or an I/O error occurs.
    Throws:
      `IOException`

  - ### read

    public int read(byte[] b, int off, int len) throws IOException

    Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

    This method implements the general contract of the corresponding `int, int) read` method of the `InputStream` class. As an additional convenience, it attempts to read as many bytes as possible by repeatedly invoking the `read` method of the underlying stream. This iterated `read` continues until one of the following conditions becomes true:

    - The specified number of bytes have been read,

    - The `read` method of the underlying stream returns `-1`, indicating end-of-file, or

    - The `available` method of the underlying stream returns zero, indicating that further input requests would block.

    If the first `read` on the underlying stream returns `-1` to indicate end-of-file then this method returns `-1`. Otherwise this method returns the number of bytes actually read.

    Subclasses of this class are encouraged, but not required, to attempt to read as many bytes as possible in the same fashion.

    ###### Parameters

    - `b`: destination buffer.

    - `off`: offset at which to start storing bytes.

    - `len`: maximum number of bytes to read.

    ###### Returns

    Overrides:
      `read` in class `InputStream`
    Returns:

      the number of bytes read, or `-1` if the end of the stream has been reached.

      ###### Throws

      - `IOException`: @throws IOException if this input stream has been closed by invoking its `#close()` method, or an I/O error occurs.
    Throws:
      `IOException`

  - ### skip

    public long skip(long n) throws IOException

    See the general contract of the `skip` method of `InputStream`.

    ###### Throws

    - `IOException`: @throws IOException if the stream does not support seek, or if this input stream has been closed by invoking its `#close()` method, or an I/O error occurs.
    Overrides:
      `skip` in class `InputStream`
    Throws:
      `IOException`

  - ### available

    public int available() throws IOException

    Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

    This method returns the sum of the number of bytes remaining to be read in the buffer (`count - pos`) and the result of calling the `in`.available().

    ###### Returns

    Overrides:
      `available` in class `InputStream`
    Returns:

      an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking.

      ###### Throws

      - `IOException`: @throws IOException if this input stream has been closed by invoking its close() method, or an I/O error occurs.
    Throws:
      `IOException`

  - ### mark

    public void mark(int readlimit)

    See the general contract of the `mark` method of `InputStream`.

    ###### Parameters

    - `readlimit`: @param readlimit the maximum limit of bytes that can be read before the mark position becomes invalid.
    Overrides:
      `mark` in class `InputStream`

  - ### reset

    public void reset() throws IOException

    See the general contract of the `reset` method of `InputStream`.

    If `markpos` is `-1` (no mark has been set or the mark has been invalidated), an `IOException` is thrown. Otherwise, `pos` is set equal to `markpos`.

    ###### Throws

    - `IOException`: @throws IOException if this stream has not been marked or, if the mark has been invalidated, or the stream has been closed by invoking its `#close()` method, or an I/O error occurs.
    Overrides:
      `reset` in class `InputStream`
    Throws:
      `IOException`

  - ### markSupported

    public boolean markSupported()

    Tests if this input stream supports the `mark` and `reset` methods. The `markSupported` method of `BufferedInputStream` returns `true`.

    ###### Returns

    Overrides:
      `markSupported` in class `InputStream`
    Returns:

      a `boolean` indicating if this stream type supports the `mark` and `reset` methods.

      ###### See also

      - java.io.InputStream#mark(int)

      - java.io.InputStream#reset()

  - ### close

    public void close() throws IOException

    Closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

    ###### Throws

    - `IOException`: if an I/O error occurs.
    Specified by:
      `close` in interface `AutoCloseable`
    Overrides:
      `close` in class `InputStream`
    Throws:
      `IOException`

  - ### getLastActivityTime

    public long getLastActivityTime()

    Returns the time of the last activity

    ###### Returns

    time of the last activity on this stream

  - ### getTotalBytesRead

    public int getTotalBytesRead()

    Returns the total number of bytes read from this stream so far

    ###### Returns

    the total number of bytes read from this stream so far

  - ### setProgressListener

    public void setProgressListener(IOProgressListener progressListener)

    Sets the callback for IO updates from a buffered stream

    ###### Parameters

    - `progressListener`: the progressListener to set

  - ### read

    public int read(byte[] b) throws IOException
    Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown. If b is null, a NullPointerException is thrown. If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b. The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected. If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed. The read(b) method for class InputStream has the same effect as: read(b, 0, b.length)
    Overrides:
      `read` in class `InputStream`
    Throws:
      `IOException`

  - ### getConnection

    public Object getConnection()

    If applicable this member represents the connection object for the stream

    ###### Returns

    the connection

  - ### setConnection

    public void setConnection(Object connection)

    If applicable this member represents the connection object for the stream

    ###### Parameters

    - `connection`: the connection to set

  - ### isDisableBuffering

    public boolean isDisableBuffering()

    ###### Returns

    the disableBuffering

  - ### setDisableBuffering

    public void setDisableBuffering(boolean disableBuffering)

    ###### Parameters

    - `disableBuffering`: the disableBuffering to set

  - ### isPrintInput

    public boolean isPrintInput()

    Prints out all the data that passes through this stream to the console. This is a very useful debugging tool.

    ###### Returns

    the printInput

  - ### setPrintInput

    public void setPrintInput(boolean printInput)

    Prints out all the data that passes through this stream to the console. This is a very useful debugging tool.

    ###### Parameters

    - `printInput`: the printInput to set

  - ### getYield

    public int getYield()

    Allows setting a yield duration for this stream which is useful for background operations to release CPU

    ###### Returns

    the yield

  - ### setYield

    public void setYield(int yield)

    Allows setting a yield duration for this stream which is useful for background operations to release CPU

    ###### Parameters

    - `yield`: the yield to set

  - ### stop

    public void stop()
    Stop reading from the stream, invoking this will cause the read() to return -1