Class PeekableInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.io.input.buffer.CircularBufferInputStream
org.apache.commons.io.input.buffer.PeekableInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
Implements a buffered input stream, which allows to peek into the buffers first bytes. This comes in handy when
manually implementing scanners, lexers, parsers, and the like.
- Since:
- 2.7
-
Field Summary
Fields inherited from class org.apache.commons.io.input.buffer.CircularBufferInputStream
buffer, bufferSizeFields inherited from class java.io.FilterInputStream
in -
Constructor Summary
ConstructorsConstructorDescriptionPeekableInputStream(InputStream inputStream) Constructs a new instance, which filters the given input stream, and uses a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE).PeekableInputStream(InputStream inputStream, int bufferSize) Constructs a new instance, which filters the given input stream, and uses the given buffer size. -
Method Summary
Modifier and TypeMethodDescriptionbooleanpeek(byte[] sourceBuffer) Returns whether the next bytes in the buffer are as given bysourceBuffer.booleanpeek(byte[] sourceBuffer, int offset, int length) Returns whether the next bytes in the buffer are as given bysourceBuffer, {code offset}, andlength.Methods inherited from class org.apache.commons.io.input.buffer.CircularBufferInputStream
close, fillBuffer, haveBytes, read, readMethods inherited from class java.io.FilterInputStream
available, mark, markSupported, read, reset, skip
-
Constructor Details
-
PeekableInputStream
Constructs a new instance, which filters the given input stream, and uses a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE).- Parameters:
inputStream- The input stream, which is being buffered.
-
PeekableInputStream
Constructs a new instance, which filters the given input stream, and uses the given buffer size.- Parameters:
inputStream- The input stream, which is being buffered.bufferSize- The size of theCircularByteBuffer, which is used internally.
-
-
Method Details
-
peek
Returns whether the next bytes in the buffer are as given bysourceBuffer. This is equivalent topeek(byte[], int, int)withoffset== 0, andlength==sourceBuffer.length- Parameters:
sourceBuffer- the buffer to compare against- Returns:
- true if the next bytes are as given
- Throws:
IOException- Refilling the buffer failed.
-
peek
Returns whether the next bytes in the buffer are as given bysourceBuffer, {code offset}, andlength.- Parameters:
sourceBuffer- the buffer to compare againstoffset- the start offsetlength- the length to compare- Returns:
- true if the next bytes in the buffer are as given
- Throws:
IOException- if there is a problem calling fillBuffer()
-