java.security

Class DigestInputStream

public class DigestInputStream extends FilterInputStream

DigestInputStream is a class that ties an InputStream with a MessageDigest. The Message Digest is used by the class to update it self as bytes are read from the InputStream. The updating to the digest depends on the on flag which is set to true by default to tell the class to update the data in the message digest.
Field Summary
protected MessageDigestdigest
The message digest for the DigestInputStream
Constructor Summary
DigestInputStream(InputStream stream, MessageDigest digest)
Constructs a new DigestInputStream.
Method Summary
MessageDigestgetMessageDigest()
Returns the MessageDigest associated with this DigestInputStream
voidon(boolean on)
Sets the flag specifing if this DigestInputStream updates the digest in the write() methods.
intread()
Reads a byte from the input stream and updates the digest.
intread(byte[] b, int off, int len)
Reads bytes from the input stream and updates the digest.
voidsetMessageDigest(MessageDigest digest)
Sets the current MessageDigest to current parameter
StringtoString()
Converts the input stream and underlying message digest to a string.

Field Detail

digest

protected MessageDigest digest
The message digest for the DigestInputStream

Constructor Detail

DigestInputStream

public DigestInputStream(InputStream stream, MessageDigest digest)
Constructs a new DigestInputStream. It associates a MessageDigest with the stream to compute the stream as data is written.

Parameters: stream An InputStream to associate this stream with digest A MessageDigest to hash the stream with

Method Detail

getMessageDigest

public MessageDigest getMessageDigest()
Returns the MessageDigest associated with this DigestInputStream

Returns: The MessageDigest used to hash this stream

on

public void on(boolean on)
Sets the flag specifing if this DigestInputStream updates the digest in the write() methods. The default is on;

Parameters: on True means it digests stream, false means it does not

read

public int read()
Reads a byte from the input stream and updates the digest. This method reads the underlying input stream and if the on flag is true then updates the message digest.

Returns: Returns a byte from the input stream, -1 is returned to indicate that the end of stream was reached before this read call

Throws: IOException if an IO error occurs in the underlying input stream, this error is thrown

read

public int read(byte[] b, int off, int len)
Reads bytes from the input stream and updates the digest. This method reads the underlying input stream and if the on flag is true then updates the message digest.

Parameters: b a byte array to store the data from the input stream off an offset to start at in the array len length of data to read

Returns: Returns count of bytes read, -1 is returned to indicate that the end of stream was reached before this read call

Throws: IOException if an IO error occurs in the underlying input stream, this error is thrown

setMessageDigest

public void setMessageDigest(MessageDigest digest)
Sets the current MessageDigest to current parameter

Parameters: digest A MessageDigest to associate with this stream

toString

public String toString()
Converts the input stream and underlying message digest to a string.

Returns: A string representing the input stream and message digest.