Class BufferedInputStream

java.lang.Object
java.io.InputStream
com.codename1.io.BufferedInputStream
All Implemented Interfaces:
Closeable, 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
    Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
    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.
    Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    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
    Closes this input stream and releases any system resources associated with the stream.
    If applicable this member represents the connection object for the stream
    static int
    The default size for a stream buffer
    Allows access to the underlying input stream if desired
    long
    Returns the time of the last activity
    Indicates the name of the stream for debugging purposes
    int
    Returns the total number of bytes read from this stream so far
    int
    Allows setting a yield duration for this stream which is useful for background operations to release CPU
    boolean
    Returns
    boolean
    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
    Tests if this input stream supports the mark and reset methods.
    int
    See the general contract of the read method of InputStream.
    int
    read(byte[] 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
    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
    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 reading from the stream, invoking this will cause the read() to return -1

    Methods inherited from class Object

    clone, equals, finalize, 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
      Specified by:
      close in interface Closeable
      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
      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