public final class MessageDigestInputStream extends ObservableInputStream
ObservableInputStream
. It creates its own ObservableInputStream.Observer
,
which calculates a checksum using a MessageDigest
, for example, a SHA-512 sum.
To build an instance, see 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
.
Modifier and Type | Class and Description |
---|---|
static class |
MessageDigestInputStream.Builder
Builds new
MessageDigestInputStream instances. |
static class |
MessageDigestInputStream.MessageDigestMaintainingObserver
Maintains the message digest.
|
ObservableInputStream.Observer
Modifier and Type | Field and Description |
---|---|
private java.security.MessageDigest |
messageDigest |
Modifier | Constructor and Description |
---|---|
private |
MessageDigestInputStream(java.io.InputStream inputStream,
java.security.MessageDigest messageDigest)
Constructs a new instance, which calculates a signature on the given stream, using the given
MessageDigest . |
Modifier and Type | Method and Description |
---|---|
static MessageDigestInputStream.Builder |
builder()
Constructs a new
MessageDigestInputStream.Builder . |
java.security.MessageDigest |
getMessageDigest()
Gets the
MessageDigest , which is being used for generating the checksum. |
add, close, consume, getObservers, noteClosed, noteDataByte, noteDataBytes, noteError, noteFinished, read, read, read, remove, removeAllObservers
afterRead, available, beforeRead, handleIOException, mark, markSupported, reset, skip
private MessageDigestInputStream(java.io.InputStream inputStream, java.security.MessageDigest messageDigest)
MessageDigest
.
The MD5 cryptographic algorithm is weak and should not be used.
inputStream
- the stream to calculate the message digest formessageDigest
- the message digest to usejava.lang.NullPointerException
- if messageDigest is null.public static MessageDigestInputStream.Builder builder()
MessageDigestInputStream.Builder
.MessageDigestInputStream.Builder
.public java.security.MessageDigest getMessageDigest()
MessageDigest
, 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()
.