Package org.apache.tika.io
Class LookaheadInputStream
java.lang.Object
java.io.InputStream
org.apache.tika.io.LookaheadInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
Stream wrapper that make it easy to read up to n bytes ahead from
a stream that supports the mark feature. This class insulates the
underlying stream from things like possible mark(), reset() and close()
calls by external components that might otherwise invalidate the marked
state of a stream.
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.
- Since:
- Apache Tika 0.10
-
Constructor Summary
ConstructorsConstructorDescriptionLookaheadInputStream(InputStream stream, int n) Creates a lookahead wrapper for the given input stream. -
Method Summary
Methods inherited from class java.io.InputStream
nullInputStream, read, readAllBytes, readNBytes, readNBytes, transferTo
-
Constructor Details
-
LookaheadInputStream
Creates a lookahead wrapper for the given input stream. The given input stream should support the mark feature, as otherwise the state of that stream will be undefined after the lookahead wrapper has been closed. As a special case anullstream is treated as an empty stream.- Parameters:
stream- input stream, can benulln- maximum number of bytes to look ahead
-
-
Method Details
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
skip
- Overrides:
skipin classInputStream- Throws:
IOException
-
available
public int available()- Overrides:
availablein classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classInputStream
-
mark
public void mark(int readlimit) - Overrides:
markin classInputStream
-
reset
public void reset()- Overrides:
resetin classInputStream
-