jdk/src/share/classes/java/security/DigestOutputStream.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 17917 9bd1b39cdbcf
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.EOFException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FilterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A transparent stream that updates the associated message digest using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the bits going through the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>To complete the message digest computation, call one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <code>digest</code> methods on the associated message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * digest after your calls to one of this digest ouput stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@link #write(int) write} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>It is possible to turn this stream on or off (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * {@link #on(boolean) on}). When it is on, a call to one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>write</code> methods results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * an update on the message digest.  But when it is off, the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * digest is not updated. The default is for the stream to be on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see DigestInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class DigestOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected MessageDigest digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Creates a digest output stream, using the specified output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * and message digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param stream the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param digest the message digest to associate with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public DigestOutputStream(OutputStream stream, MessageDigest digest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        super(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        setMessageDigest(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns the message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return the message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @see #setMessageDigest(java.security.MessageDigest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public MessageDigest getMessageDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Associates the specified message digest with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param digest the message digest to be associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @see #getMessageDigest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void setMessageDigest(MessageDigest digest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.digest = digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Updates the message digest (if the digest function is on) using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the specified byte, and in any case writes the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * to the output stream. That is, if the digest function is on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * (see {@link #on(boolean) on}), this method calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <code>update</code> on the message digest associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * stream, passing it the byte <code>b</code>. This method then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * writes the byte to the output stream, blocking until the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * is actually written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param b the byte to be used for updating and writing to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see MessageDigest#update(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            digest.update((byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Updates the message digest (if the digest function is on) using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * the specified subarray, and in any case writes the subarray to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the output stream. That is, if the digest function is on (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * {@link #on(boolean) on}), this method calls <code>update</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * on the message digest associated with this stream, passing it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * the subarray specifications. This method then writes the subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * bytes to the output stream, blocking until the bytes are actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param b the array containing the subarray to be used for updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * and writing to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param off the offset into <code>b</code> of the first byte to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * be updated and written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param len the number of bytes of data to be updated and written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * from <code>b</code>, starting at offset <code>off</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @see MessageDigest#update(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void write(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            digest.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Turns the digest function on or off. The default is on.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * it is on, a call to one of the <code>write</code> methods results in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * update on the message digest.  But when it is off, the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * digest is not updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param on true to turn the digest function on, false to turn it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void on(boolean on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.on = on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Prints a string representation of this digest output stream and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * its associated message digest object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         return "[Digest Output Stream] " + digest.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}