All Implemented Interfaces:
ReadCounter, Closeable, AutoCloseable

public class EOFOnMatchInputStreamextends PushbackInputStream implements ReadCounter
InputStream that returns EOF when input matches specified bytes. The reader never sees bytes from a full match. Extends PushbackInputStream for convenience but uses its buffer as a FIFO, not a stack. Do not call unread() methods externally.
  • Constructor Details

    • EOFOnMatchInputStream

      public EOFOnMatchInputStream(InputStream in, byte[] match)
      Non-counter mode. getRead() will return 0.
      Parameters:
      in - the underlying stream to read until the match is seen
      match - will be copied
    • EOFOnMatchInputStream

      public EOFOnMatchInputStream(InputStream in, ReadCounter ctr, byte[] match)
      Counter mode. getRead() will the ReadCounter's value, not including the match bytes.
      Parameters:
      in - the underlying stream to read until the match is seen
      ctr - counts the bytes read from the underlying stream, or null for non-counter mode
      match - will be copied
  • Method Details

    • getRead

      public long getRead()
      If constructed with a counter, returns the count (not necessarily starting at 0) minus the buffered/matched count. Otherwise returns 0.
      Specified by:
      getRead in interface ReadCounter
      Returns:
      the number of bytes read from the underlying stream that were not buffered here or matched against the search pattern, or 0 if this stream was not wrapped around a counting stream
    • wasFound

      public boolean wasFound()
      Whether the stream stopped because the search pattern matched.
      Returns:
      true if we returned EOF because we hit the match
    • read

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

      public int read(byte[] buf, int off, int len) throws IOException
      Read bytes into a buffer.
      Overrides:
      read in class PushbackInputStream
      Parameters:
      buf - the buffer to read into
      off - the start offset in the buffer
      len - the maximum number of bytes to read
      Returns:
      the number of bytes read, or -1 on end of stream
      Throws:
      IOException - on I/O error
    • skip

      public long skip(long n) throws IOException
      Skip bytes in the stream.
      Overrides:
      skip in class PushbackInputStream
      Parameters:
      n - the number of bytes to skip
      Returns:
      the actual number of bytes skipped
      Throws:
      IOException - on I/O error