org.apache.tika.io
Class LookaheadInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.apache.tika.io.LookaheadInputStream
All Implemented Interfaces:
java.io.Closeable

public class LookaheadInputStream
extends java.io.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:

     InputStream lookahead = new LookaheadInputStream(stream, n);
     try {
         processStream(lookahead);
     } finally {
         lookahead.close();
     }
 

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
LookaheadInputStream(java.io.InputStream stream, int n)
          Creates a lookahead wrapper for the given input stream.
 
Method Summary
 int available()
           
 void close()
           
 void mark(int readlimit)
           
 boolean markSupported()
           
 int read()
           
 int read(byte[] b, int off, int len)
           
 void reset()
           
 long skip(long n)
           
 
Methods inherited from class java.io.InputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LookaheadInputStream

public LookaheadInputStream(java.io.InputStream stream,
                            int n)
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 a null stream is treated as an empty stream.

Parameters:
stream - input stream, can be null
n - maximum number of bytes to look ahead
Method Detail

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

available

public int available()
Overrides:
available in class java.io.InputStream

markSupported

public boolean markSupported()
Overrides:
markSupported in class java.io.InputStream

mark

public void mark(int readlimit)
Overrides:
mark in class java.io.InputStream

reset

public void reset()
Overrides:
reset in class java.io.InputStream


Copyright © 2007-2011 The Apache Software Foundation. All Rights Reserved.