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 null
n
- maximum number of bytes to look aheadpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
private void fill() throws java.io.IOException
java.io.IOException
public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public long skip(long n) throws java.io.IOException
skip
in class java.io.InputStream
java.io.IOException
public int available()
available
in class java.io.InputStream
public boolean markSupported()
markSupported
in class java.io.InputStream
public void mark(int readlimit)
mark
in class java.io.InputStream
public void reset()
reset
in class java.io.InputStream