Class PeekableInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.io.input.buffer.CircularBufferInputStream
-
- org.apache.commons.io.input.buffer.PeekableInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class PeekableInputStream extends CircularBufferInputStream
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.
-
-
Field Summary
-
Fields inherited from class org.apache.commons.io.input.buffer.CircularBufferInputStream
buffer, bufferSize, in
-
-
Constructor Summary
Constructors Constructor Description PeekableInputStream(java.io.InputStream inputStream)
Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size (8192).PeekableInputStream(java.io.InputStream inputStream, int bufferSize)
Creates a new instance, which filters the given input stream, and uses the given buffer size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
peek(byte[] sourceBuffer)
Returns whether the next bytes in the buffer are as given bysourceBuffer
.boolean
peek(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, read, read
-
-
-
-
Constructor Detail
-
PeekableInputStream
public PeekableInputStream(java.io.InputStream inputStream, int bufferSize)
Creates 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.
-
PeekableInputStream
public PeekableInputStream(java.io.InputStream inputStream)
Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size (8192).- Parameters:
inputStream
- The input stream, which is being buffered.
-
-
Method Detail
-
peek
public boolean peek(byte[] sourceBuffer) throws java.io.IOException
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:
java.io.IOException
- Refilling the buffer failed.
-
peek
public boolean peek(byte[] sourceBuffer, int offset, int length) throws java.io.IOException
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:
java.io.IOException
- if there is a problem calling fillBuffer()
-
-