Package org.nuiton.util
Class MD5InputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
java.security.DigestInputStream
org.nuiton.util.MD5InputStream
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class MD5InputStream
extends java.security.DigestInputStream
MD5InputStream, a subclass of FilterInputStream implementing MD5
functionality on a stream.
- Author:
- Tony Chemit - chemit@codelutin.com
-
Field Summary
Fields Modifier and Type Field Description protected longstreamLengthlength of readed stream -
Constructor Summary
Constructors Constructor Description MD5InputStream(java.io.InputStream in)Creates a MD5InputStream -
Method Summary
Modifier and Type Method Description protected static java.security.MessageDigestgetMD5Digest()longgetStreamLength()byte[]hash()Returns array of bytes representing hash of the stream as finalized for the current state.static byte[]hash(java.io.File input)Compute the MD5 for the giveninputfile.static byte[]hash(java.io.InputStream input)Compute the MD5 for the giveninput.static byte[]hash(java.lang.String input)Compute the MD5 for the giveninputsring.intread()intread(byte[] bytes, int offset, int length)Methods inherited from class java.security.DigestInputStream
getMessageDigest, on, setMessageDigest, toStringMethods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip
-
Field Details
-
streamLength
protected long streamLengthlength of readed stream
-
-
Constructor Details
-
MD5InputStream
public MD5InputStream(java.io.InputStream in)Creates a MD5InputStream- Parameters:
in- The input stream
-
-
Method Details
-
getMD5Digest
protected static java.security.MessageDigest getMD5Digest() throws java.lang.IllegalStateException- Throws:
java.lang.IllegalStateException
-
hash
public static byte[] hash(java.io.InputStream input) throws java.io.IOExceptionCompute the MD5 for the giveninput. Note: The the stream will be closed after calling the method even if something was wrong.- Parameters:
input- the stream to parse- Returns:
- the MD5 hash for the given input stream
- Throws:
java.io.IOException- if any pb while reading in stream or digest
-
hash
public static byte[] hash(java.io.File input) throws java.io.IOExceptionCompute the MD5 for the giveninputfile.- Parameters:
input- the File stream to parse- Returns:
- the MD5 hash for the given input File
- Throws:
java.io.IOException- if any pb while reading in stream or digest
-
hash
public static byte[] hash(java.lang.String input) throws java.io.IOExceptionCompute the MD5 for the giveninputsring.- Parameters:
input- the stream to parse- Returns:
- the MD5 hash for the given input String
- Throws:
java.io.IOException- if any pb while reading in stream or digest
-
read
public int read() throws java.io.IOException- Overrides:
readin classjava.security.DigestInputStream- Throws:
java.io.IOException
-
read
public int read(byte[] bytes, int offset, int length) throws java.io.IOException- Overrides:
readin classjava.security.DigestInputStream- Throws:
java.io.IOException
-
hash
public byte[] hash()Returns array of bytes representing hash of the stream as finalized for the current state.- Returns:
- hash
- See Also:
MessageDigest.digest()
-
getStreamLength
public long getStreamLength()
-