All Implemented Interfaces:
Closeable, AutoCloseable

public class LookaheadInputStreamextends FilterInputStream
Simple lookahead buffer to keep the last K bytes in reserve, configured to easily be reused. Currently only used by the ResettableGZIPInputStream.
  • Field Details

    • _eofReached

      private boolean _eofReached
      True once the footer has been seen.
    • _footerLookahead

      private final byte[] _footerLookahead
      The footer bytes to watch for.
    • size

      private final int size
      Length of the footer to look ahead for.
    • index

      private int index
      Next footer byte to read.
    • _fakeInputStream

      private static final InputStream _fakeInputStream
      Empty stream returned once the footer has been reached.
  • Constructor Details

    • LookaheadInputStream

      public LookaheadInputStream(int lookaheadSize)
      Configure a stream that hides a number of bytes from the reader. The last n bytes will never be available from read(), they can only be obtained from getFooter().

      initialize(InputStream) MUST be called before doing any read() calls.

      Parameters:
      lookaheadSize - how many bytes to hide
  • Method Details

    • getEOFReached

      public boolean getEOFReached()
      Whether the end of the stream has been reached.
      Returns:
      whether EOF has been reached
    • initialize

      public void initialize(InputStream src) throws IOException
      Start the LookaheadInputStream with the given input stream. Resets everything if the LookaheadInputStream was previously used. WARNING - blocking until lookaheadSize bytes are read!
      Parameters:
      src - the input stream
      Throws:
      IOException - if less than lookaheadSize bytes could be read.
    • read

      public int read() throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • read

      public int read(byte[] buf, int off, int len) throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • getFooter

      public byte[] getFooter()
      Grab the lookahead footer. This will be of size lookaheadsize given in constructor. The last byte received will be in the last byte of the array.
      Returns:
      the lookahead footer bytes
    • skip

      public long skip(long n) throws IOException
      Skip forward, respecting the lookahead buffer.
      Overrides:
      skip in class FilterInputStream
      Throws:
      IOException