Package org.saidone.misc
Class AnvDigestInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.saidone.misc.AnvDigestInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
AnvDigestInputStream
wraps another InputStream
and computes a
message digest while the data is read.
The digest algorithm is provided at construction time and each read operation
updates the underlying MessageDigest
. When the stream has been
consumed the final hash value can be obtained via getHash()
.
This utility is typically used when a checksum or fingerprint of the streamed
content is required without buffering the entire input in memory.
-
Field Summary
Fields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorsConstructorDescriptionAnvDigestInputStream
(InputStream inputStream) Creates a digesting stream that performs no hashing.AnvDigestInputStream
(InputStream inputStream, String algorithm) Creates a new digesting stream using the supplied algorithm. -
Method Summary
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, reset, skip
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
AnvDigestInputStream
public AnvDigestInputStream(InputStream inputStream, String algorithm) throws NoSuchAlgorithmException Creates a new digesting stream using the supplied algorithm.- Parameters:
inputStream
- the underlying stream to readalgorithm
- name of theMessageDigest
algorithm- Throws:
NoSuchAlgorithmException
- if the algorithm is not available
-
AnvDigestInputStream
Creates a digesting stream that performs no hashing. This constructor is mainly useful when the same type is required but hashing is disabled.- Parameters:
inputStream
- the underlying stream- Throws:
NoSuchAlgorithmException
- if the algorithm is not available
-
-
Method Details
-
read
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classFilterInputStream
- Throws:
IOException
-
getHash
Returns the computed hash as a lowercase hexadecimal string. This method should be called once the stream has been fully consumed. If hashing was disabled viaAnvDigestInputStream(InputStream)
, this method returnsnull
.- Returns:
- the digest of the read bytes or
null
if hashing is disabled
-