All Implemented Interfaces:
ReadCounter, Closeable, AutoCloseable
Direct Known Subclasses:
LimitInputStream

public class CountingInputStreamextends FilterInputStream implements ReadCounter
InputStream that counts bytes read and skipped.
  • Field Details

    • count

      protected long count
      Total bytes read or skipped.
  • Constructor Details

    • CountingInputStream

      public CountingInputStream(InputStream in)
      Create a CountingInputStream wrapping the given input stream.
      Parameters:
      in - the underlying input stream
  • Method Details

    • skip

      public long skip(long n) throws IOException
      Skip bytes and count them.
      Overrides:
      skip in class FilterInputStream
      Parameters:
      n - number of bytes to skip
      Returns:
      actual number of bytes skipped
      Throws:
      IOException - on I/O error
    • getRead

      public long getRead()
      Get the total number of bytes read or skipped so far.
      Specified by:
      getRead in interface ReadCounter
      Returns:
      total bytes read/skipped
    • read

      public int read() throws IOException
      Read a single byte and count it.
      Overrides:
      read in class FilterInputStream
      Returns:
      the byte read, or -1 on end of stream
      Throws:
      IOException - on I/O error
    • read

      public int read(byte[] buf, int off, int len) throws IOException
      Read bytes into a buffer and count them.
      Overrides:
      read in class FilterInputStream
      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