Class ChecksumInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public final class ChecksumInputStream
    extends CountingInputStream
    Automatically verifies a Checksum value once the stream is exhausted or the count threshold is reached.

    If the Checksum does not meet the expected value when exhausted, then the input stream throws an IOException.

    If you do not need the verification or threshold feature, then use a plain CheckedInputStream.

    To build an instance, use ChecksumInputStream.Builder.

    Since:
    2.16.0
    See Also:
    ChecksumInputStream.Builder
    • Field Detail

      • expectedChecksumValue

        private final long expectedChecksumValue
        The expected checksum.
      • countThreshold

        private final long countThreshold
        The count threshold to limit how much input is consumed to update the Checksum before the input stream validates its value.

        By default, all input updates the Checksum.

    • Constructor Detail

      • ChecksumInputStream

        private ChecksumInputStream​(ChecksumInputStream.Builder builder)
                             throws java.io.IOException
        Constructs a new instance.
        Parameters:
        builder - build parameters.
        Throws:
        java.io.IOException
    • Method Detail

      • afterRead

        protected void afterRead​(int n)
                          throws java.io.IOException
        Description copied from class: CountingInputStream
        Adds the number of read bytes to the count.
        Overrides:
        afterRead in class CountingInputStream
        Parameters:
        n - number of bytes read, or -1 if no more bytes are available
        Throws:
        java.io.IOException - Not thrown here but subclasses may throw.
      • getChecksum

        private java.util.zip.Checksum getChecksum()
        Gets the current checksum value.
        Returns:
        the current checksum value.
      • getRemaining

        public long getRemaining()
        Gets the byte count remaining to read.
        Returns:
        bytes remaining to read, a negative number means the threshold is unbound.