Class ChecksumInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
edu.harvard.hul.ois.jhove.ChecksumInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class ChecksumInputStream extends FilterInputStream
A ChecksumInputStream is a FilterInputStream with the added functionality of calculating checksums as it goes. The idea of replacing this with java.util.zip.CheckedInputStream looks very tempting, but we need the byte count, which CheckedInputStream doesn't provide.
Author:
Gary McGath
  • Constructor Details

    • ChecksumInputStream

      public ChecksumInputStream(InputStream stream, Checksummer cksummer)
      Constructor.
      Parameters:
      stream - Stream to be filtered
      cksummer - Object to calculate checksum on the bytes as they are read
  • Method Details

    • read

      public int read() throws IOException
      Reads a byte from the subsumed stream, updating the byte count and the checksums.
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. All bytes read are fed through the checksummer.
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer. All bytes read are fed through the checksummer.
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Skips n bytes. Reads them and feeds them through the checksummer.
      Overrides:
      skip in class FilterInputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Closes the subsumed stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterInputStream
      Throws:
      IOException
    • getNBytes

      public long getNBytes()
      Returns the byte count to date on the stream. This returns the number of bytes read. Because of buffering, this is not a reliable indicator of how many bytes have actually been processed.
    • getChecksummer

      public Checksummer getChecksummer()
      Returns the Checksummer object.