Package org.apache.commons.io.input
Class MessageDigestInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.io.input.ProxyInputStream
org.apache.commons.io.input.ObservableInputStream
org.apache.commons.io.input.MessageDigestInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
This class is an example for using an
ObservableInputStream
. It creates its own ObservableInputStream.Observer
,
which calculates a checksum using a MessageDigest
, for example, a SHA-512 sum.
To build an instance, use MessageDigestInputStream.Builder
.
See the MessageDigest section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.
You must specify a message digest algorithm name or instance.
Note: Neither ObservableInputStream
, nor MessageDigest
, are thread safe, so is MessageDigestInputStream
.
- Since:
- 2.15.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builds newMessageDigestInputStream
.static class
Maintains the message digest.Nested classes/interfaces inherited from class org.apache.commons.io.input.ObservableInputStream
ObservableInputStream.Observer
-
Field Summary
FieldsFields inherited from class java.io.FilterInputStream
in
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
MessageDigestInputStream
(InputStream inputStream, MessageDigest messageDigest) Constructs a new instance, which calculates a signature on the given stream, using the givenMessageDigest
. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Constructs a newMessageDigestInputStream.Builder
.Gets theMessageDigest
, which is being used for generating the checksum.Methods inherited from class org.apache.commons.io.input.ObservableInputStream
add, close, consume, getObservers, noteClosed, noteDataByte, noteDataBytes, noteError, noteFinished, read, read, read, remove, removeAllObservers
Methods inherited from class org.apache.commons.io.input.ProxyInputStream
afterRead, available, beforeRead, handleIOException, mark, markSupported, reset, skip, unwrap
-
Field Details
-
messageDigest
-
-
Constructor Details
-
MessageDigestInputStream
Constructs a new instance, which calculates a signature on the given stream, using the givenMessageDigest
.The MD5 cryptographic algorithm is weak and should not be used.
- Parameters:
inputStream
- the stream to calculate the message digest formessageDigest
- the message digest to use- Throws:
NullPointerException
- if messageDigest is null.
-
-
Method Details
-
builder
Constructs a newMessageDigestInputStream.Builder
.- Returns:
- a new
MessageDigestInputStream.Builder
.
-
getMessageDigest
Gets theMessageDigest
, which is being used for generating the checksum.Note: The checksum will only reflect the data, which has been read so far. This is probably not, what you expect. Make sure, that the complete data has been read, if that is what you want. The easiest way to do so is by invoking
ObservableInputStream.consume()
.- Returns:
- the message digest used
-