0N/A/*
2362N/A * Copyright (c) 1996, 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.EOFException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.FilterInputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.ByteArrayInputStream;
0N/A
0N/A/**
0N/A * A transparent stream that updates the associated message digest using
0N/A * the bits going through the stream.
0N/A *
0N/A * <p>To complete the message digest computation, call one of the
0N/A * <code>digest</code> methods on the associated message
0N/A * digest after your calls to one of this digest input stream's
0N/A * {@link #read() read} methods.
0N/A *
0N/A * <p>It is possible to turn this stream on or off (see
0N/A * {@link #on(boolean) on}). When it is on, a call to one of the
0N/A * <code>read</code> methods
0N/A * results in an update on the message digest. But when it is off,
0N/A * the message digest is not updated. The default is for the stream
0N/A * to be on.
0N/A *
0N/A * <p>Note that digest objects can compute only one digest (see
0N/A * {@link MessageDigest}),
0N/A * so that in order to compute intermediate digests, a caller should
0N/A * retain a handle onto the digest object, and clone it for each
0N/A * digest to be computed, leaving the orginal digest untouched.
0N/A *
0N/A * @see MessageDigest
0N/A *
0N/A * @see DigestOutputStream
0N/A *
0N/A * @author Benjamin Renaud
0N/A */
0N/A
0N/Apublic class DigestInputStream extends FilterInputStream {
0N/A
0N/A /* NOTE: This should be made a generic UpdaterInputStream */
0N/A
0N/A /* Are we on or off? */
0N/A private boolean on = true;
0N/A
0N/A /**
0N/A * The message digest associated with this stream.
0N/A */
0N/A protected MessageDigest digest;
0N/A
0N/A /**
0N/A * Creates a digest input stream, using the specified input stream
0N/A * and message digest.
0N/A *
0N/A * @param stream the input stream.
0N/A *
0N/A * @param digest the message digest to associate with this stream.
0N/A */
0N/A public DigestInputStream(InputStream stream, MessageDigest digest) {
0N/A super(stream);
0N/A setMessageDigest(digest);
0N/A }
0N/A
0N/A /**
0N/A * Returns the message digest associated with this stream.
0N/A *
0N/A * @return the message digest associated with this stream.
0N/A * @see #setMessageDigest(java.security.MessageDigest)
0N/A */
0N/A public MessageDigest getMessageDigest() {
0N/A return digest;
0N/A }
0N/A
0N/A /**
0N/A * Associates the specified message digest with this stream.
0N/A *
0N/A * @param digest the message digest to be associated with this stream.
0N/A * @see #getMessageDigest()
0N/A */
0N/A public void setMessageDigest(MessageDigest digest) {
0N/A this.digest = digest;
0N/A }
0N/A
0N/A /**
0N/A * Reads a byte, and updates the message digest (if the digest
0N/A * function is on). That is, this method reads a byte from the
0N/A * input stream, blocking until the byte is actually read. If the
0N/A * digest function is on (see {@link #on(boolean) on}), this method
0N/A * will then call <code>update</code> on the message digest associated
0N/A * with this stream, passing it the byte read.
0N/A *
0N/A * @return the byte read.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A *
0N/A * @see MessageDigest#update(byte)
0N/A */
0N/A public int read() throws IOException {
0N/A int ch = in.read();
0N/A if (on && ch != -1) {
0N/A digest.update((byte)ch);
0N/A }
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * Reads into a byte array, and updates the message digest (if the
0N/A * digest function is on). That is, this method reads up to
0N/A * <code>len</code> bytes from the input stream into the array
0N/A * <code>b</code>, starting at offset <code>off</code>. This method
0N/A * blocks until the data is actually
0N/A * read. If the digest function is on (see
0N/A * {@link #on(boolean) on}), this method will then call <code>update</code>
0N/A * on the message digest associated with this stream, passing it
0N/A * the data.
0N/A *
0N/A * @param b the array into which the data is read.
0N/A *
0N/A * @param off the starting offset into <code>b</code> of where the
0N/A * data should be placed.
0N/A *
0N/A * @param len the maximum number of bytes to be read from the input
0N/A * stream into b, starting at offset <code>off</code>.
0N/A *
0N/A * @return the actual number of bytes read. This is less than
0N/A * <code>len</code> if the end of the stream is reached prior to
0N/A * reading <code>len</code> bytes. -1 is returned if no bytes were
0N/A * read because the end of the stream had already been reached when
0N/A * the call was made.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A *
0N/A * @see MessageDigest#update(byte[], int, int)
0N/A */
0N/A public int read(byte[] b, int off, int len) throws IOException {
0N/A int result = in.read(b, off, len);
0N/A if (on && result != -1) {
0N/A digest.update(b, off, result);
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Turns the digest function on or off. The default is on. When
0N/A * it is on, a call to one of the <code>read</code> methods results in an
0N/A * update on the message digest. But when it is off, the message
0N/A * digest is not updated.
0N/A *
0N/A * @param on true to turn the digest function on, false to turn
0N/A * it off.
0N/A */
0N/A public void on(boolean on) {
0N/A this.on = on;
0N/A }
0N/A
0N/A /**
0N/A * Prints a string representation of this digest input stream and
0N/A * its associated message digest object.
0N/A */
0N/A public String toString() {
0N/A return "[Digest Input Stream] " + digest.toString();
0N/A }
0N/A}