2N/A/*
2N/A * Copyright (c) 1996, 1999, Oracle and/or its affiliates. All rights reserved.
2N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2N/A *
2N/A * This code is free software; you can redistribute it and/or modify it
2N/A * under the terms of the GNU General Public License version 2 only, as
2N/A * published by the Free Software Foundation. Oracle designates this
2N/A * particular file as subject to the "Classpath" exception as provided
2N/A * by Oracle in the LICENSE file that accompanied this code.
2N/A *
2N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2N/A * version 2 for more details (a copy is included in the LICENSE file that
2N/A * accompanied this code).
2N/A *
2N/A * You should have received a copy of the GNU General Public License version
2N/A * 2 along with this work; if not, write to the Free Software Foundation,
2N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2N/A *
2N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2N/A * or visit www.oracle.com if you need additional information or have any
2N/A * questions.
2N/A */
2N/A
2N/Apackage java.security;
2N/A
2N/Aimport java.io.IOException;
2N/Aimport java.io.EOFException;
2N/Aimport java.io.OutputStream;
2N/Aimport java.io.FilterOutputStream;
2N/Aimport java.io.PrintStream;
2N/Aimport java.io.ByteArrayOutputStream;
2N/A
2N/A/**
2N/A * A transparent stream that updates the associated message digest using
2N/A * the bits going through the stream.
2N/A *
2N/A * <p>To complete the message digest computation, call one of the
2N/A * <code>digest</code> methods on the associated message
2N/A * digest after your calls to one of this digest ouput stream's
2N/A * {@link #write(int) write} methods.
2N/A *
2N/A * <p>It is possible to turn this stream on or off (see
2N/A * {@link #on(boolean) on}). When it is on, a call to one of the
2N/A * <code>write</code> methods results in
2N/A * an update on the message digest. But when it is off, the message
2N/A * digest is not updated. The default is for the stream to be on.
2N/A *
2N/A * @see MessageDigest
2N/A * @see DigestInputStream
2N/A *
2N/A * @author Benjamin Renaud
2N/A */
2N/Apublic class DigestOutputStream extends FilterOutputStream {
2N/A
2N/A private boolean on = true;
2N/A
2N/A /**
2N/A * The message digest associated with this stream.
2N/A */
2N/A protected MessageDigest digest;
2N/A
2N/A /**
2N/A * Creates a digest output stream, using the specified output stream
2N/A * and message digest.
2N/A *
2N/A * @param stream the output stream.
2N/A *
2N/A * @param digest the message digest to associate with this stream.
2N/A */
2N/A public DigestOutputStream(OutputStream stream, MessageDigest digest) {
2N/A super(stream);
2N/A setMessageDigest(digest);
2N/A }
2N/A
2N/A /**
2N/A * Returns the message digest associated with this stream.
2N/A *
2N/A * @return the message digest associated with this stream.
2N/A * @see #setMessageDigest(java.security.MessageDigest)
2N/A */
2N/A public MessageDigest getMessageDigest() {
2N/A return digest;
2N/A }
2N/A
2N/A /**
2N/A * Associates the specified message digest with this stream.
2N/A *
2N/A * @param digest the message digest to be associated with this stream.
2N/A * @see #getMessageDigest()
2N/A */
2N/A public void setMessageDigest(MessageDigest digest) {
2N/A this.digest = digest;
2N/A }
2N/A
2N/A /**
2N/A * Updates the message digest (if the digest function is on) using
2N/A * the specified byte, and in any case writes the byte
2N/A * to the output stream. That is, if the digest function is on
2N/A * (see {@link #on(boolean) on}), this method calls
2N/A * <code>update</code> on the message digest associated with this
2N/A * stream, passing it the byte <code>b</code>. This method then
2N/A * writes the byte to the output stream, blocking until the byte
2N/A * is actually written.
2N/A *
2N/A * @param b the byte to be used for updating and writing to the
2N/A * output stream.
2N/A *
2N/A * @exception IOException if an I/O error occurs.
2N/A *
2N/A * @see MessageDigest#update(byte)
2N/A */
2N/A public void write(int b) throws IOException {
2N/A if (on) {
2N/A digest.update((byte)b);
2N/A }
2N/A out.write(b);
2N/A }
2N/A
2N/A /**
2N/A * Updates the message digest (if the digest function is on) using
2N/A * the specified subarray, and in any case writes the subarray to
2N/A * the output stream. That is, if the digest function is on (see
2N/A * {@link #on(boolean) on}), this method calls <code>update</code>
2N/A * on the message digest associated with this stream, passing it
2N/A * the subarray specifications. This method then writes the subarray
2N/A * bytes to the output stream, blocking until the bytes are actually
2N/A * written.
2N/A *
2N/A * @param b the array containing the subarray to be used for updating
2N/A * and writing to the output stream.
2N/A *
2N/A * @param off the offset into <code>b</code> of the first byte to
2N/A * be updated and written.
2N/A *
2N/A * @param len the number of bytes of data to be updated and written
2N/A * from <code>b</code>, starting at offset <code>off</code>.
2N/A *
2N/A * @exception IOException if an I/O error occurs.
2N/A *
2N/A * @see MessageDigest#update(byte[], int, int)
2N/A */
2N/A public void write(byte[] b, int off, int len) throws IOException {
2N/A if (on) {
2N/A digest.update(b, off, len);
2N/A }
2N/A out.write(b, off, len);
2N/A }
2N/A
2N/A /**
2N/A * Turns the digest function on or off. The default is on. When
2N/A * it is on, a call to one of the <code>write</code> methods results in an
2N/A * update on the message digest. But when it is off, the message
2N/A * digest is not updated.
2N/A *
2N/A * @param on true to turn the digest function on, false to turn it
2N/A * off.
2N/A */
2N/A public void on(boolean on) {
2N/A this.on = on;
2N/A }
2N/A
2N/A /**
2N/A * Prints a string representation of this digest output stream and
2N/A * its associated message digest object.
2N/A */
2N/A public String toString() {
2N/A return "[Digest Output Stream] " + digest.toString();
2N/A }
2N/A}
2N/A