Package org.apache.commons.io.input
Class MessageDigestCalculatingInputStream
- 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.MessageDigestCalculatingInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class MessageDigestCalculatingInputStream extends ObservableInputStream
This class is an example for using anObservableInputStream
. It creates its ownObservableInputStream.Observer
, which calculates a checksum using a MessageDigest, for example an MD5 sum. Note: NeitherObservableInputStream
, norMessageDigest
, are thread safe. So isMessageDigestCalculatingInputStream
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MessageDigestCalculatingInputStream.MessageDigestMaintainingObserver
Maintains the message digest.-
Nested classes/interfaces inherited from class org.apache.commons.io.input.ObservableInputStream
ObservableInputStream.Observer
-
-
Constructor Summary
Constructors Constructor Description MessageDigestCalculatingInputStream(java.io.InputStream inputStream)
Creates a new instance, which calculates a signature on the given stream, using aMessageDigest
with the "MD5" algorithm.MessageDigestCalculatingInputStream(java.io.InputStream inputStream, java.lang.String algorithm)
Creates a new instance, which calculates a signature on the given stream, using aMessageDigest
with the given algorithm.MessageDigestCalculatingInputStream(java.io.InputStream inputStream, java.security.MessageDigest messageDigest)
Creates a new instance, which calculates a signature on the given stream, using the givenMessageDigest
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.security.MessageDigest
getMessageDigest()
Returns theMessageDigest
, which is being used for generating the checksum.-
Methods inherited from class org.apache.commons.io.input.ObservableInputStream
add, close, consume, getObservers, read, read, read, remove, removeAllObservers
-
Methods inherited from class org.apache.commons.io.input.ProxyInputStream
available, mark, markSupported, reset, skip
-
-
-
-
Constructor Detail
-
MessageDigestCalculatingInputStream
public MessageDigestCalculatingInputStream(java.io.InputStream inputStream, java.security.MessageDigest messageDigest)
Creates a new instance, which calculates a signature on the given stream, using the givenMessageDigest
.- Parameters:
inputStream
- the stream to calculate the message digest formessageDigest
- the message digest to use
-
MessageDigestCalculatingInputStream
public MessageDigestCalculatingInputStream(java.io.InputStream inputStream, java.lang.String algorithm) throws java.security.NoSuchAlgorithmException
Creates a new instance, which calculates a signature on the given stream, using aMessageDigest
with the given algorithm.- Parameters:
inputStream
- the stream to calculate the message digest foralgorithm
- the name of the algorithm to use- Throws:
java.security.NoSuchAlgorithmException
- if no Provider supports a MessageDigestSpi implementation for the specified algorithm.
-
MessageDigestCalculatingInputStream
public MessageDigestCalculatingInputStream(java.io.InputStream inputStream) throws java.security.NoSuchAlgorithmException
Creates a new instance, which calculates a signature on the given stream, using aMessageDigest
with the "MD5" algorithm.- Parameters:
inputStream
- the stream to calculate the message digest for- Throws:
java.security.NoSuchAlgorithmException
- if no Provider supports a MessageDigestSpi implementation for the specified algorithm.
-
-
Method Detail
-
getMessageDigest
public java.security.MessageDigest getMessageDigest()
Returns 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 invokingObservableInputStream.consume()
.- Returns:
- the message digest used
-
-