public class RecyclableBufferedInputStream 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"));
in
Constructor and Description |
---|
RecyclableBufferedInputStream(InputStream in,
ArrayPool byteArrayPool) |
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 |
fixMarkLimit()
Reduces the mark limit to match the current buffer length to prevent the buffer from continuing
to increase in size.
|
void |
mark(int readlimit)
Sets a mark position in this stream.
|
boolean |
markSupported()
|
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 offset,
int byteCount)
Reads at most
byteCount bytes from this stream and stores them in byte array buffer starting at offset offset . |
void |
release() |
void |
reset()
Resets this stream to the last marked location.
|
long |
skip(long byteCount)
Skips
byteCount bytes in this stream. |
read
public RecyclableBufferedInputStream(InputStream in, ArrayPool byteArrayPool)
public int available() throws IOException
InputStream.available()
for important caveats.available
in class FilterInputStream
IOException
- if this stream is closed or an error occurspublic void fixMarkLimit()
Subsequent calls to mark(int)
will be obeyed and may cause the buffer size to
increase.
public void release()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class FilterInputStream
IOException
- 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 FilterInputStream
readlimit
- the number of bytes that can be read before the mark is invalidated.reset()
public boolean markSupported()
markSupported
in class FilterInputStream
true
for BufferedInputStreams.mark(int)
,
reset()
public int read() throws IOException
read
in class FilterInputStream
IOException
- if this stream is closed or another IOException occurs.public int read(@NonNull byte[] buffer, int offset, int byteCount) throws IOException
byteCount
bytes from this stream and stores them in byte array buffer
starting at offset offset
. Returns the number of bytes actually read or -1 if
no bytes were read and the end of the stream was encountered. If all the buffered bytes have
been used, a mark has not been put and the requested number of bytes is larger than the
receiver's buffer size, this implementation bypasses the buffer and simply places the results
directly into buffer
.read
in class FilterInputStream
buffer
- the byte array in which to store the bytes read.IndexOutOfBoundsException
- if offset < 0
or byteCount < 0
, or if offset + byteCount
is greater than the size of buffer
.IOException
- if the stream is already closed or another IOException
occurs.public void reset() throws IOException
reset
in class FilterInputStream
IOException
- if this stream is closed, no mark has been put 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 FilterInputStream
byteCount
- the number of bytes to skip. This method does nothing and returns 0 if
byteCount
is less than zero.IOException
- if this stream is closed or another IOException occurs.