-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public class RecyclableBufferedInputStream extends FilterInputStream
Wraps an existing 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"));
-
-
Constructor Summary
Constructors Constructor Description RecyclableBufferedInputStream(InputStream in, ArrayPool byteArrayPool)
-
Method Summary
Modifier and Type Method Description synchronized intavailable()Returns an estimated number of bytes that can be read or skipped without blocking for moreinput. synchronized voidfixMarkLimit()Reduces the mark limit to match the current buffer length to prevent the buffer from continuingto increase in size. synchronized voidrelease()voidclose()Closes this stream. synchronized voidmark(int readlimit)Sets a mark position in this stream. booleanmarkSupported()Indicates whether {@code BufferedInputStream}supports the mark and methods.synchronized intread()Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.Returns -1 if the end of the source string has been reached. synchronized intread(@NonNull() Array<byte> buffer, int offset, int byteCount)Reads at most {@code byteCount}bytes from this stream and stores them in byte array{@code * buffer}starting at offset{@code offset}.synchronized voidreset()Resets this stream to the last marked location. synchronized longskip(long byteCount)Skips {@code byteCount}bytes in this stream.-
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip -
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
RecyclableBufferedInputStream
RecyclableBufferedInputStream(InputStream in, ArrayPool byteArrayPool)
-
-
Method Detail
-
available
synchronized int available()
Returns an estimated number of bytes that can be read or skipped without blocking for moreinput. This method returns the number of bytes available in the buffer plus those available inthe source stream, but see available for important caveats.
-
fixMarkLimit
synchronized void fixMarkLimit()
Reduces the mark limit to match the current buffer length to prevent the buffer from continuingto increase in size.
Subsequent calls to mark will be obeyed and may cause the buffer size toincrease.
-
release
synchronized void release()
-
close
void close()
Closes this stream. The source stream is closed and any resources associated with it arereleased.
-
mark
synchronized void mark(int readlimit)
Sets a mark position in this stream. The parameter
{@code readlimit}indicates how many bytescan be read before a mark is invalidated. Calling reset will reposition the streamback to the marked position if{@code readlimit}has not been surpassed. The underlying buffermay be increased in size to allow{@code readlimit}number of bytes to be supported.- Parameters:
readlimit- the number of bytes that can be read before the mark is invalidated.
-
markSupported
boolean markSupported()
Indicates whether
{@code BufferedInputStream}supports the mark and methods.
-
read
synchronized int read()
Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.Returns -1 if the end of the source string has been reached. If the internal buffer does notcontain any available bytes then it is filled from the source stream and the first byte isreturned.
-
read
synchronized int read(@NonNull() Array<byte> buffer, int offset, int byteCount)
Reads at most
{@code byteCount}bytes from this stream and stores them in byte array{@code * buffer}starting at offset{@code offset}. Returns the number of bytes actually read or -1 ifno bytes were read and the end of the stream was encountered. If all the buffered bytes havebeen used, a mark has not been put and the requested number of bytes is larger than thereceiver's buffer size, this implementation bypasses the buffer and simply places the resultsdirectly into{@code buffer}.- Parameters:
buffer- the byte array in which to store the bytes read.
-
reset
synchronized void reset()
Resets this stream to the last marked location.
-
skip
synchronized long skip(long byteCount)
Skips
{@code byteCount}bytes in this stream. Subsequent calls to read will not returnthese bytes unless reset is used.- Parameters:
byteCount- the number of bytes to skip.
-
-
-
-