public class BufferedInputStream extends FilterInputStream
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.
A typical application pattern for the class looks like this:
BufferedInputStream buf = new BufferedInputStream(new FileInputStream("file.java"));
BufferedOutputStream| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
buf
The buffer containing the current bytes read from the target InputStream.
|
protected int |
count
The total number of bytes inside the byte array
buf. |
static int |
DEFAULT_BUFFER_SIZE
The default buffer size if it is not specified.
|
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
buf. |
in| Constructor and Description |
|---|
BufferedInputStream(InputStream in)
Constructs a new
BufferedInputStream, providing in with a buffer
of 8192 bytes. |
BufferedInputStream(InputStream in,
int size)
Constructs a new
BufferedInputStream, providing in with size bytes
of buffer. |
| Modifier and Type | Method and Description |
|---|---|
int |
available()
Returns an estimated number of bytes that can be read or skipped without blocking for more
input.
|
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[] buffer,
int byteOffset,
int byteCount)
Reads up to
byteCount bytes from this stream and stores them in
the byte array buffer starting at byteOffset. |
void |
reset()
Resets this stream to the last marked location.
|
long |
skip(long byteCount)
Skips
byteCount bytes in this stream. |
nullInputStream, read, readAllBytes, readNBytes, readNBytes, transferTopublic static final int DEFAULT_BUFFER_SIZE
protected volatile byte[] buf
protected int count
buf.protected int marklimit
protected int markpos
protected int pos
buf.public BufferedInputStream(InputStream in)
BufferedInputStream, providing in with a buffer
of 8192 bytes.
Warning: passing a null source creates a closed
BufferedInputStream. All read operations on such a stream will
fail with an IOException.
in - the InputStream the buffer reads from.public BufferedInputStream(InputStream in, int size)
BufferedInputStream, providing in with size bytes
of buffer.
Warning: passing a null source creates a closed
BufferedInputStream. All read operations on such a stream will
fail with an IOException.
in - the InputStream the buffer reads from.size - the size of buffer in bytes.IllegalArgumentException - if size <= 0.public int available()
throws IOException
InputStream.available() for
important caveats.available in class FilterInputStreamIOException - if this stream is closed or an error occurspublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class FilterInputStreamIOException - if an error occurs while closing this stream.public void mark(int readlimit)
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.mark in class FilterInputStreamreadlimit - the number of bytes that can be read before the mark is
invalidated.reset()public boolean markSupported()
BufferedInputStream supports the mark()
and reset() methods.markSupported in class FilterInputStreamtrue for BufferedInputStreams.mark(int),
reset()public int read()
throws IOException
read in class FilterInputStreamIOException - if this stream is closed or another IOException occurs.public int read(byte[] buffer,
int byteOffset,
int byteCount)
throws IOException
InputStreambyteCount bytes from this stream and stores them in
the byte array buffer starting at byteOffset.
Returns the number of bytes actually read or -1 if the end of the stream
has been reached.read in class FilterInputStreamIOException - if the stream is closed or another IOException occurs.public void reset()
throws IOException
reset in class FilterInputStreamIOException - 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.mark(int)public long skip(long byteCount)
throws IOException
byteCount bytes in this stream. Subsequent calls to
read will not return these bytes unless reset is
used.skip in class FilterInputStreambyteCount - the number of bytes to skip. skip does nothing and
returns 0 if byteCount is less than zero.IOException - if this stream is closed or another IOException occurs.FilterInputStream.mark(int),
FilterInputStream.reset()