Class LookaheadInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class LookaheadInputStream
    extends InputStream
    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