public class DigestInputStream extends FilterInputStream
DigestInputStream
is a FilterInputStream
which maintains an
associated message digest.Modifier and Type | Field and Description |
---|---|
protected MessageDigest |
digest
The message digest for this stream.
|
in
Constructor and Description |
---|
DigestInputStream(InputStream stream,
MessageDigest digest)
Constructs a new instance of this
DigestInputStream , using the
given stream and the digest . |
Modifier and Type | Method and Description |
---|---|
MessageDigest |
getMessageDigest()
Returns the message digest for this stream.
|
void |
on(boolean on)
Enables or disables the digest function (default is on).
|
int |
read()
Reads the next byte and returns it as an
int . |
int |
read(byte[] b,
int off,
int len)
Reads
len bytes into the specified byte[] , starting from
the specified offset. |
void |
setMessageDigest(MessageDigest digest)
Sets the message digest which this stream will use.
|
String |
toString()
Returns a string containing a concise, human-readable description of this
DigestInputStream including the digest. |
available, close, mark, markSupported, reset, skip
read
protected MessageDigest digest
public DigestInputStream(InputStream stream, MessageDigest digest)
DigestInputStream
, using the
given stream
and the digest
.
Warning: passing a null source creates an invalid
DigestInputStream
. All operations on such a stream will fail.
stream
- the input stream.digest
- the message digest.public MessageDigest getMessageDigest()
public void setMessageDigest(MessageDigest digest)
digest
- the message digest which this stream will use.public int read() throws IOException
int
. Updates the digest
for the byte if this function is on(boolean)
.
This operation is blocking.
read
in class FilterInputStream
IOException
- if reading the source stream causes an IOException
.public int read(byte[] b, int off, int len) throws IOException
len
bytes into the specified byte[]
, starting from
the specified offset. Updates the digest if this function is
on(boolean)
.
This operation is blocking.
read
in class FilterInputStream
b
- the byte array in which to store the bytesoff
- the initial position in b
to store the bytes read from
this streamlen
- the maximum number of bytes to store in b
IOException
- if reading the source stream causes an IOException
public void on(boolean on)
on
- true
if the digest should be computed, false
otherwise.MessageDigest