Package io.ocfl.api.io
Class FixityCheckInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.security.DigestInputStream
-
- io.ocfl.api.io.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.
-
-
Field Summary
-
Fields inherited from class java.security.DigestInputStream
digest
-
Fields inherited from class java.io.FilterInputStream
in
-
-
Constructor Summary
Constructors Constructor Description FixityCheckInputStream(InputStream inputStream, DigestAlgorithm digestAlgorithm, String expectedDigestValue)
FixityCheckInputStream(InputStream inputStream, String digestAlgorithm, String expectedDigestValue)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
checkFixity()
Performs a fixity check and throws an exception if the check fails.FixityCheckInputStream
enableFixityCheck(boolean enabled)
By default fixity checking is enabled.Optional<String>
getActualDigestValue()
Returns the hex encoded digest value of the input stream.String
getExpectedDigestValue()
void
on(boolean on)
String
toString()
-
Methods inherited from class java.security.DigestInputStream
getMessageDigest, read, read, setMessageDigest
-
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip
-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Constructor Detail
-
FixityCheckInputStream
public FixityCheckInputStream(InputStream inputStream, DigestAlgorithm digestAlgorithm, String expectedDigestValue)
- Parameters:
inputStream
- the underlying streamdigestAlgorithm
- 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 streamdigestAlgorithm
- 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
-
on
public void on(boolean on)
- Overrides:
on
in classDigestInputStream
-
toString
public String toString()
- Overrides:
toString
in classDigestInputStream
-
-