Class 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 Detail

      • 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 Detail

      • 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
      • 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.