public class BufferedInputStream

  1. Object
  2. InputStream
  3. BufferedInputStream

ImplementsAutoCloseable

Known subtypesTarInputStream

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

Constructors

public BufferedInputStream(InputStream in)Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
public BufferedInputStream(InputStream in, String name)Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
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.
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.

Methods

public static int getDefaultBufferSize()The default size for a stream buffer
public static void setDefaultBufferSize(int aDefaultBufferSize)The default size for a stream buffer
public String getName()Indicates the name of the stream for debugging purposes
public InputStream getInternal()Allows access to the underlying input stream if desired
public synchronized int read() throws IOExceptionSee the general contract of the read method of InputStream.
public synchronized int read(byte[] b, int off, int len) throws IOExceptionReads bytes from this byte-input stream into the specified byte array, starting at the given offset.
public synchronized long skip(long n) throws IOExceptionSee the general contract of the skip method of InputStream.
public synchronized int available() throws IOExceptionReturns 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.
public synchronized void mark(int readlimit)See the general contract of the mark method of InputStream.
public synchronized void reset() throws IOExceptionSee the general contract of the reset method of InputStream.
public boolean markSupported()Tests if this input stream supports the mark and reset methods.
public void close() throws IOExceptionCloses this input stream and releases any system resources associated with the stream.
public synchronized long getLastActivityTime()Returns the time of the last activity
public synchronized int getTotalBytesRead()Returns the total number of bytes read from this stream so far
public void setProgressListener(IOProgressListener progressListener)Sets the callback for IO updates from a buffered stream
public int read(byte[] b) throws IOExceptionReads 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.
public Object getConnection()If applicable this member represents the connection object for the stream
public void setConnection(Object connection)If applicable this member represents the connection object for the stream
public synchronized boolean isDisableBuffering()
public synchronized void setDisableBuffering(boolean disableBuffering)
public synchronized boolean isPrintInput()Prints out all the data that passes through this stream to the console.
public synchronized void setPrintInput(boolean printInput)Prints out all the data that passes through this stream to the console.
public synchronized int getYield()Allows setting a yield duration for this stream which is useful for background operations to release CPU
public synchronized void setYield(int yield)Allows setting a yield duration for this stream which is useful for background operations to release CPU
public synchronized void stop()Stop reading from the stream, invoking this will cause the read() to return -1

Inherited methods

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 InputStream
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 InputStream
the underlying input stream.
name String
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 InputStream
the underlying input stream.
size int
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 InputStream
the underlying input stream.
size int
the buffer size.
name String
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 int
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 synchronized int read() throws IOException
See the general contract of the read method of InputStream.

Returns

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

Throws

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

read

public synchronized 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 byte[]
destination buffer.
off int
offset at which to start storing bytes.
len int
maximum number of bytes to read.

Returns

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

Throws

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

skip

public synchronized long skip(long n) throws IOException
See the general contract of the skip method of InputStream.

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.

available

public synchronized 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

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

Throws

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

mark

public synchronized void mark(int readlimit)
See the general contract of the mark method of InputStream.

Parameters

readlimit int
the maximum limit of bytes that can be read before the mark position becomes invalid.

reset

public synchronized 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
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.

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods. The markSupported method of BufferedInputStream returns true.

Returns

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

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.

getLastActivityTime

public synchronized long getLastActivityTime()
Returns the time of the last activity

Returns

time of the last activity on this stream

getTotalBytesRead

public synchronized 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 IOProgressListener
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 bk 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)

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 Object
the connection to set

isDisableBuffering

public synchronized boolean isDisableBuffering()

Returns

the disableBuffering

setDisableBuffering

public synchronized void setDisableBuffering(boolean disableBuffering)

Parameters

disableBuffering boolean
the disableBuffering to set

isPrintInput

public synchronized 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 synchronized 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 boolean
the printInput to set

getYield

public synchronized int getYield()
Allows setting a yield duration for this stream which is useful for background operations to release CPU

Returns

the yield

setYield

public synchronized void setYield(int yield)
Allows setting a yield duration for this stream which is useful for background operations to release CPU

Parameters

yield int
the yield to set

stop

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