Package org.bzdev.io

Class CounterInputStream

java.lang.Object
java.io.InputStream
org.bzdev.io.CounterInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class CounterInputStream extends InputStream
An input stream with a byte count. This class provides an implementation of InputStream with a byte counter giving the current position in the input stream. One use for this class is for software instrumentation during development (e.g., to empirically determine buffer sizes).
  • Constructor Details

    • CounterInputStream

      public CounterInputStream(InputStream is)
      Constructor.
      Parameters:
      is - the input stream to count.
  • Method Details

    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class InputStream
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

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

      public void reset() throws IOException
      Repositions this stream to the position at the time the mark method was last called on this input stream.

      The general contract of reset is:

      • If the method markSupported returns true, then:
        • If the method mark has not been called since the stream was created, or the number of bytes read from the stream since mark was last called is larger than the argument to mark at that last call, then an IOException will be thrown.
        • If such an IOException is not thrown, then the stream is reset to a state such that all the bytes read since the most recent call to mark (or since the start of the file, if mark has not been called) will be resupplied to subsequent callers of the read method, followed by any bytes that otherwise would have been the next input data as of the time of the call to reset.
      • If the method markSupported returns false, then:
        • The call to reset will throw an IOException.
        • If an IOException is not thrown, then the stream is reset to a fixed state that depends on the particular type of the input stream and how it was created. The bytes that will be supplied to subsequent callers of the read method depend on the particular type of the input stream.
      Overrides:
      reset in class InputStream
      Throws:
      IOException - the read limit has been exceeded or if the mark is not supported or set.
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • getCount

      public long getCount()
      Get the current byte count. This returns the number of bytes that have been read and skipped.
      Returns:
      the number of bytes read so far.