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:
java.io.Closeable,java.lang.AutoCloseable
public final class MessageDigestInputStream extends ObservableInputStream
This class is an example for using anObservableInputStream. It creates its ownObservableInputStream.Observer, which calculates a checksum using aMessageDigest, 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, norMessageDigest, are thread safe, so isMessageDigestInputStream.- Since:
- 2.15.0
- See Also:
MessageDigestInputStream.Builder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMessageDigestInputStream.BuilderBuilds newMessageDigestInputStream.static classMessageDigestInputStream.MessageDigestMaintainingObserverMaintains the message digest.-
Nested classes/interfaces inherited from class org.apache.commons.io.input.ObservableInputStream
ObservableInputStream.AbstractBuilder<T extends ObservableInputStream.AbstractBuilder<T>>, ObservableInputStream.Observer
-
-
Field Summary
Fields Modifier and Type Field Description private java.security.MessageDigestmessageDigestA non-null MessageDigest.
-
Constructor Summary
Constructors Modifier Constructor Description privateMessageDigestInputStream(MessageDigestInputStream.Builder builder)Constructs a new instance, which calculates a signature on the given stream, using the givenMessageDigest.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MessageDigestInputStream.Builderbuilder()Constructs a newMessageDigestInputStream.Builder.java.security.MessageDigestgetMessageDigest()Gets theMessageDigest, which is being used for generating the checksum, never null.-
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, checkOpen, handleIOException, isClosed, mark, markSupported, reset, setReference, skip, unwrap
-
-
-
-
Constructor Detail
-
MessageDigestInputStream
private MessageDigestInputStream(MessageDigestInputStream.Builder builder) throws java.io.IOException
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:
builder- A builder use to get the stream to calculate the message digest and the message digest to use- Throws:
java.lang.NullPointerException- if messageDigest is null.java.io.IOException
-
-
Method Detail
-
builder
public static MessageDigestInputStream.Builder builder()
Constructs a newMessageDigestInputStream.Builder.- Returns:
- a new
MessageDigestInputStream.Builder.
-
getMessageDigest
public java.security.MessageDigest getMessageDigest()
Gets theMessageDigest, which is being used for generating the checksum, never null.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, never null.
-
-