FileDocCategorySizeDatePackage
DigestInputStream.javaAPI DocAndroid 1.5 API5150Wed May 06 22:41:06 BST 2009java.security

DigestInputStream

public class DigestInputStream extends FilterInputStream
{@code DigestInputStream} is a {@code FilterInputStream} which maintains an associated message digest.
since
Android 1.0

Fields Summary
protected MessageDigest
digest
The message digest for this stream.
private boolean
isOn
Constructors Summary
public DigestInputStream(InputStream stream, MessageDigest digest)
Constructs a new instance of this {@code DigestInputStream}, using the given {@code stream} and the {@code digest}.

param
stream the input stream.
param
digest the message digest.
since
Android 1.0


                                                              
         
        super(stream);
        this.digest = digest;
    
Methods Summary
public java.security.MessageDigestgetMessageDigest()
Returns the message digest for this stream.

return
the message digest for this stream.
since
Android 1.0

        return digest;
    
public voidon(boolean on)
Enables or disables the digest function (default is on).

param
on {@code true} if the digest should be computed, {@code false} otherwise.
since
Android 1.0

        isOn = on;
    
public intread()
Reads the next byte and returns it as an {@code int}. Updates the digest for the byte if this function is {@link #on(boolean)}.

This operation is blocking.

return
the byte which was read or -1 at end of stream.
throws
IOException if reading the source stream causes an {@code IOException}.
since
Android 1.0

        // read the next byte
        int byteRead = in.read();
        // update digest only if
        // - digest functionality is on
        // - eos has not been reached
        if (isOn && (byteRead != -1)) {
            digest.update((byte)byteRead);
        }
        // return byte read
        return byteRead;
    
public intread(byte[] b, int off, int len)
Reads {@code len} bytes into the specified {@code byte[]}, starting from the specified offset. Updates the digest if this function is {@link #on(boolean)}.

This operation is blocking.

param
b the byte array in which to store the bytes
param
off the initial position in {@code b} to store the bytes read from this stream
param
len the maximum number of bytes to store in {@code b}
return
the number of bytes actually read or -1 if the end of the filtered stream has been reached while reading
throws
IOException if reading the source stream causes an {@code IOException}
since
Android 1.0

        // read next up to len bytes
        int bytesRead = in.read(b, off, len);
        // update digest only if
        // - digest functionality is on
        // - eos has not been reached
        if (isOn && (bytesRead != -1)) {
            digest.update(b, off, bytesRead);
        }
        // return number of bytes read
        return bytesRead;
    
public voidsetMessageDigest(java.security.MessageDigest digest)
Sets the message digest which this stream will use.

param
digest the message digest which this stream will use.
since
Android 1.0

        this.digest = digest;
    
public java.lang.StringtoString()
Returns a string containing a concise, human-readable description of this {@code DigestInputStream} including the digest.

return
a printable representation for this {@code DigestInputStream}.
since
Android 1.0

        return super.toString() + ", " + digest.toString() + //$NON-NLS-1$
            (isOn ? ", is on" : ", is off"); //$NON-NLS-1$ //$NON-NLS-2$