public class LookaheadInputStream
extends java.io.InputStream
The recommended usage pattern of this class is:
try (InputStream lookahead = new LookaheadInputStream(stream, n)) {
processStream(lookahead);
}
This usage pattern guarantees that only up to n bytes from the original stream can ever be read, and that the stream will have been marked and then reset to its original state once the above code block exits. No code in the fictional processStream() method can affect the the state of the original stream.
| Modifier and Type | Field and Description |
|---|---|
private byte[] |
buffer |
private int |
buffered |
private int |
mark |
private int |
position |
private java.io.InputStream |
stream |
| Constructor and Description |
|---|
LookaheadInputStream(java.io.InputStream stream,
int n)
Creates a lookahead wrapper for the given input stream.
|
| Modifier and Type | Method and Description |
|---|---|
int |
available() |
void |
close() |
private void |
fill() |
void |
mark(int readlimit) |
boolean |
markSupported() |
int |
read() |
int |
read(byte[] b,
int off,
int len) |
void |
reset() |
long |
skip(long n) |
private java.io.InputStream stream
private final byte[] buffer
private int buffered
private int position
private int mark
public LookaheadInputStream(java.io.InputStream stream,
int n)
null stream is treated as an empty stream.stream - input stream, can be nulln - maximum number of bytes to look aheadpublic void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class java.io.InputStreamjava.io.IOExceptionprivate void fill()
throws java.io.IOException
java.io.IOExceptionpublic int read()
throws java.io.IOException
read in class java.io.InputStreamjava.io.IOExceptionpublic int read(byte[] b,
int off,
int len)
throws java.io.IOException
read in class java.io.InputStreamjava.io.IOExceptionpublic long skip(long n)
throws java.io.IOException
skip in class java.io.InputStreamjava.io.IOExceptionpublic int available()
available in class java.io.InputStreampublic boolean markSupported()
markSupported in class java.io.InputStreampublic void mark(int readlimit)
mark in class java.io.InputStreampublic void reset()
reset in class java.io.InputStream