java.security
Class DigestInputStream

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.InputStream sample code for java.io.InputStream definition code for java.io.InputStream 
      extended by java.io.FilterInputStream sample code for java.io.FilterInputStream definition code for java.io.FilterInputStream 
          extended by java.security.DigestInputStream
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable

public class DigestInputStream
extends FilterInputStream sample code for java.io.FilterInputStream definition code for java.io.FilterInputStream

A transparent stream that updates the associated message digest using the bits going through the stream.

To complete the message digest computation, call one of the digest methods on the associated message digest after your calls to one of this digest input stream's read sample code for java.security.DigestInputStream.read() definition code for java.security.DigestInputStream.read() methods.

It is possible to turn this stream on or off (see on sample code for java.security.DigestInputStream.on(boolean) definition code for java.security.DigestInputStream.on(boolean) ). When it is on, a call to one of the read methods results in an update on the message digest. But when it is off, the message digest is not updated. The default is for the stream to be on.

Note that digest objects can compute only one digest (see MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest ), so that in order to compute intermediate digests, a caller should retain a handle onto the digest object, and clone it for each digest to be computed, leaving the orginal digest untouched.

See Also:
MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest , DigestOutputStream sample code for java.security.DigestOutputStream definition code for java.security.DigestOutputStream

Field Summary
protected  MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest digest sample code for java.security.DigestInputStream.digest definition code for java.security.DigestInputStream.digest
          The message digest associated with this stream.
 
Fields inherited from class java.io.FilterInputStream sample code for java.io.FilterInputStream definition code for java.io.FilterInputStream
in sample code for java.io.FilterInputStream.in definition code for java.io.FilterInputStream.in
 
Constructor Summary
DigestInputStream sample code for java.security.DigestInputStream.DigestInputStream(java.io.InputStream, java.security.MessageDigest) definition code for java.security.DigestInputStream.DigestInputStream(java.io.InputStream, java.security.MessageDigest) (InputStream sample code for java.io.InputStream definition code for java.io.InputStream  stream, MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest  digest)
          Creates a digest input stream, using the specified input stream and message digest.
 
Method Summary
 MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest getMessageDigest sample code for java.security.DigestInputStream.getMessageDigest() definition code for java.security.DigestInputStream.getMessageDigest() ()
          Returns the message digest associated with this stream.
 void on sample code for java.security.DigestInputStream.on(boolean) definition code for java.security.DigestInputStream.on(boolean) (boolean on)
          Turns the digest function on or off.
 int read sample code for java.security.DigestInputStream.read() definition code for java.security.DigestInputStream.read() ()
          Reads a byte, and updates the message digest (if the digest function is on).
 int read sample code for java.security.DigestInputStream.read(byte[], int, int) definition code for java.security.DigestInputStream.read(byte[], int, int) (byte[] b, int off, int len)
          Reads into a byte array, and updates the message digest (if the digest function is on).
 void setMessageDigest sample code for java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest) definition code for java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest) (MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest  digest)
          Associates the specified message digest with this stream.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.security.DigestInputStream.toString() definition code for java.security.DigestInputStream.toString() ()
          Prints a string representation of this digest input stream and its associated message digest object.
 
Methods inherited from class java.io.FilterInputStream sample code for java.io.FilterInputStream definition code for java.io.FilterInputStream
available sample code for java.io.FilterInputStream.available() definition code for java.io.FilterInputStream.available() , close sample code for java.io.FilterInputStream.close() definition code for java.io.FilterInputStream.close() , mark sample code for java.io.FilterInputStream.mark(int) definition code for java.io.FilterInputStream.mark(int) , markSupported sample code for java.io.FilterInputStream.markSupported() definition code for java.io.FilterInputStream.markSupported() , read sample code for java.io.FilterInputStream.read(byte[]) definition code for java.io.FilterInputStream.read(byte[]) , reset sample code for java.io.FilterInputStream.reset() definition code for java.io.FilterInputStream.reset() , skip sample code for java.io.FilterInputStream.skip(long) definition code for java.io.FilterInputStream.skip(long)
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

digest sample code for java.security.DigestInputStream.digest

protected MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest  digest
The message digest associated with this stream.

Constructor Detail

DigestInputStream sample code for java.security.DigestInputStream(java.io.InputStream, java.security.MessageDigest) definition code for java.security.DigestInputStream(java.io.InputStream, java.security.MessageDigest)

public DigestInputStream(InputStream sample code for java.io.InputStream definition code for java.io.InputStream  stream,
                         MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest  digest)
Creates a digest input stream, using the specified input stream and message digest.

Parameters:
stream - the input stream.
digest - the message digest to associate with this stream.
Method Detail

getMessageDigest sample code for java.security.DigestInputStream.getMessageDigest() definition code for java.security.DigestInputStream.getMessageDigest()

public MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest  getMessageDigest()
Returns the message digest associated with this stream.

Returns:
the message digest associated with this stream.
See Also:
setMessageDigest(java.security.MessageDigest) sample code for java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest) definition code for java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)

setMessageDigest sample code for java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest) definition code for java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)

public void setMessageDigest(MessageDigest sample code for java.security.MessageDigest definition code for java.security.MessageDigest  digest)
Associates the specified message digest with this stream.

Parameters:
digest - the message digest to be associated with this stream.
See Also:
getMessageDigest() sample code for java.security.DigestInputStream.getMessageDigest() definition code for java.security.DigestInputStream.getMessageDigest()

read sample code for java.security.DigestInputStream.read() definition code for java.security.DigestInputStream.read()

public int read()
         throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Reads a byte, and updates the message digest (if the digest function is on). That is, this method reads a byte from the input stream, blocking until the byte is actually read. If the digest function is on (see on sample code for java.security.DigestInputStream.on(boolean) definition code for java.security.DigestInputStream.on(boolean) ), this method will then call update on the message digest associated with this stream, passing it the byte read.

Overrides:
read sample code for java.io.FilterInputStream.read() definition code for java.io.FilterInputStream.read() in class FilterInputStream sample code for java.io.FilterInputStream definition code for java.io.FilterInputStream
Returns:
the byte read.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
MessageDigest.update(byte) sample code for java.security.MessageDigest.update(byte) definition code for java.security.MessageDigest.update(byte)

read sample code for java.security.DigestInputStream.read(byte[], int, int) definition code for java.security.DigestInputStream.read(byte[], int, int)

public int read(byte[] b,
                int off,
                int len)
         throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Reads into a byte array, and updates the message digest (if the digest function is on). That is, this method reads up to len bytes from the input stream into the array b, starting at offset off. This method blocks until the data is actually read. If the digest function is on (see on sample code for java.security.DigestInputStream.on(boolean) definition code for java.security.DigestInputStream.on(boolean) ), this method will then call update on the message digest associated with this stream, passing it the data.

Overrides:
read sample code for java.io.FilterInputStream.read(byte[], int, int) definition code for java.io.FilterInputStream.read(byte[], int, int) in class FilterInputStream sample code for java.io.FilterInputStream definition code for java.io.FilterInputStream
Parameters:
b - the array into which the data is read.
off - the starting offset into b of where the data should be placed.
len - the maximum number of bytes to be read from the input stream into b, starting at offset off.
Returns:
the actual number of bytes read. This is less than len if the end of the stream is reached prior to reading len bytes. -1 is returned if no bytes were read because the end of the stream had already been reached when the call was made.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
MessageDigest.update(byte[], int, int) sample code for java.security.MessageDigest.update(byte[], int, int) definition code for java.security.MessageDigest.update(byte[], int, int)

on sample code for java.security.DigestInputStream.on(boolean) definition code for java.security.DigestInputStream.on(boolean)

public void on(boolean on)
Turns the digest function on or off. The default is on. When it is on, a call to one of the read methods results in an update on the message digest. But when it is off, the message digest is not updated.

Parameters:
on - true to turn the digest function on, false to turn it off.

toString sample code for java.security.DigestInputStream.toString() definition code for java.security.DigestInputStream.toString()

public String sample code for java.lang.String definition code for java.lang.String  toString()
Prints a string representation of this digest input stream and its associated message digest object.

Overrides:
toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a string representation of the object.