BufferedInputStream

A <code>BufferedInputStream</code> adds functionality to another input stream-namely, the ability to buffer the input and to support the <code>mark</code> and <code>reset</code> methods. When the <code>BufferedInputStream</code> is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The <code>mark</code> operation remembers a point in the input stream and the <code>reset</code> operation causes all the bytes read since the most recent <code>mark</code> operation to be reread before new bytes are taken from the contained input stream.

@author Arthur van Hoff

Constructors

this
this(InputStream inputStream)

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

this
this(InputStream inputStream, int size)

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

Members

Functions

available
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. 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. <p> This method returns the sum of the number of bytes remaining to be read in the buffer (<code>count&nbsp;- pos</code>) and the result of calling the {@link java.io.FilterInputStream#inputStream inputStream}.available().

close
void close()

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.

mark
void mark(int readlimit)

See the general contract of the <code>mark</code> method of <code>InputStream</code>.

markSupported
bool markSupported()

Tests if this input stream supports the <code>mark</code> and <code>reset</code> methods. The <code>markSupported</code> method of <code>BufferedInputStream</code> returns <code>true</code>.

position
void position(int index)
Undocumented in source. Be warned that the author may not have intended to support it.
read
int read()

See the general contract of the <code>read</code> method of <code>InputStream</code>.

read
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.

reset
void reset()

See the general contract of the <code>reset</code> method of <code>InputStream</code>. <p> If <code>markpos</code> is <code>-1</code> (no mark has been set or the mark has been invalidated), an <code>IOException</code> is thrown. Otherwise, <code>pos</code> is set equal to <code>markpos</code>.

skip
long skip(long n)

See the general contract of the <code>skip</code> method of <code>InputStream</code>.

Variables

buf
byte[] buf;

The internal buffer array where the data is stored. When necessary, it may be replaced by another array of a different size.

count
int count;

The index one greater than the index of the last valid byte in the buffer. This value is always in the range <code>0</code> through <code>buf.length</code>; elements <code>buf[0]</code> through <code>buf[count-1] </code>contain buffered input data obtained from the underlying input stream.

marklimit
int marklimit;

The maximum read ahead allowed after a call to the <code>mark</code> method before subsequent calls to the <code>reset</code> method fail. Whenever the difference between <code>pos</code> and <code>markpos</code> exceeds <code>marklimit</code>, then the mark may be dropped by setting <code>markpos</code> to <code>-1</code>.

markpos
int markpos;

The value of the <code>pos</code> field at the time the last <code>mark</code> method was called. <p> This value is always in the range <code>-1</code> through <code>pos</code>. If there is no marked position in the input stream, this field is <code>-1</code>. If there is a marked position in the input stream, then <code>bufmarkpos</code> is the first byte to be supplied as input after a <code>reset</code> operation. If <code>markpos</code> is not <code>-1</code>, then all bytes from positions <code>bufmarkpos</code> through <code>buf[pos-1]</code> must remain in the buffer array (though they may be moved to another place in the buffer array, with suitable adjustments to the values of <code>count</code>, <code>pos</code>, and <code>markpos</code>); they may not be discarded unless and until the difference between <code>pos</code> and <code>markpos</code> exceeds <code>marklimit</code>.

pos
int pos;

The current position in the buffer. This is the index of the next character to be read from the <code>buf</code> array. <p> This value is always in the range <code>0</code> through <code>count</code>. If it is less than <code>count</code>, then <code>bufpos</code> is the next byte to be supplied as input; if it is equal to <code>count</code>, then the next <code>read</code> or <code>skip</code> operation will require more bytes to be read from the contained input stream.

Inherited Members

From FilterInputStream

inputStream
InputStream inputStream;

The input stream to be filtered.

read
int read()

Reads the next byte of data from this input stream. The value byte is returned as an <code>int</code> in the range <code>0</code> to <code>255</code>. If no byte is available because the end of the stream has been reached, the value <code>-1</code> is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. <p> This method simply performs <code>inputStream.read()</code> and returns the result.

read
int read(byte[] b)

Reads up to <code>b.length</code> bytes of data from this input stream into an array of bytes. This method blocks until some input is available. <p> This method simply performs the call <code>read(b, 0, b.length)</code> and returns the result. It is important that it does <i>not</i> do <code>inputStream.read(b)</code> instead; certain subclasses of <code>FilterInputStream</code> depend on the implementation strategy actually used.

read
int read(byte[] b, int off, int len)

Reads up to <code>len</code> bytes of data from this input stream into an array of bytes. If <code>len</code> is not zero, the method blocks until some input is available; otherwise, no bytes are read and <code>0</code> is returned. <p> This method simply performs <code>inputStream.read(b, off, len)</code> and returns the result.

skip
long skip(long n)

Skips over and discards <code>n</code> bytes of data from the input stream. The <code>skip</code> method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly <code>0</code>. The actual number of bytes skipped is returned. <p> This method simply performs <code>inputStream.skip(n)</code>.

available
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 caller of a method for this input stream. The next caller 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. <p> This method returns the result of {@link #inputStream inputStream}.available().

close
void close()

Closes this input stream and releases any system resources associated with the stream. This method simply performs <code>inputStream.close()</code>.

mark
void mark(int readlimit)

Marks the current position in this input stream. A subsequent call to the <code>reset</code> method repositions this stream at the last marked position so that subsequent reads re-read the same bytes. <p> The <code>readlimit</code> argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated. <p> This method simply performs <code>inputStream.mark(readlimit)</code>.

reset
void reset()

Repositions this stream to the position at the time the <code>mark</code> method was last called on this input stream. <p> This method simply performs <code>inputStream.reset()</code>. <p> Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails. If this happens within readlimit bytes, it allows the outer code to reset the stream and try another parser.

markSupported
bool markSupported()

Tests if this input stream supports the <code>mark</code> and <code>reset</code> methods. This method simply performs <code>inputStream.markSupported()</code>.

Meta