Class FixityCheckInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class FixityCheckInputStream
    extends DigestInputStream
    Wrapper around Java's DigestInputStream to compute a digest while streaming data, and then verify the fixity of the data. After all of the stream's bytes have been read, checkFixity() should be called.
    • Constructor Detail

      • FixityCheckInputStream

        public FixityCheckInputStream​(InputStream inputStream,
                                      DigestAlgorithm digestAlgorithm,
                                      String expectedDigestValue)
        Parameters:
        inputStream - the underlying stream
        digestAlgorithm - the algorithm to use to calculate the digest (eg. sha512)
        expectedDigestValue - the expected digest value
      • FixityCheckInputStream

        public FixityCheckInputStream​(InputStream inputStream,
                                      String digestAlgorithm,
                                      String expectedDigestValue)
        Parameters:
        inputStream - the underlying stream
        digestAlgorithm - the algorithm to use to calculate the digest (eg. sha512)
        expectedDigestValue - the expected digest value
    • Method Detail

      • checkFixity

        public void checkFixity()
        Performs a fixity check and throws an exception if the check fails. This method MUST NOT be called before the entire stream has been read, doing so will invalidate the digest.

        If the check is disabled, nothing happens

        Throws:
        FixityCheckException - when the actual digest value does not match the expected value
      • getExpectedDigestValue

        public String getExpectedDigestValue()
        Returns:
        the expected digest value
      • getActualDigestValue

        public Optional<String> getActualDigestValue()
        Returns the hex encoded digest value of the input stream. This method MUST NOT be called before the entire stream has been read, doing so will invalidate the digest. A digest will not be returned if the fixity check is disabled.
        Returns:
        the digest of the stream
      • enableFixityCheck

        public FixityCheckInputStream enableFixityCheck​(boolean enabled)
        By default fixity checking is enabled. Use this method to disable it, and prevent needless digest computation
        Parameters:
        enabled - if fixity should be checked
        Returns:
        this stream