src/java.base/share/classes/java/security/DigestOutputStream.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 47216 71c04702a3d5
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2019, 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
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 17917
diff changeset
    40
 * {@code digest} methods on the associated message
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18579
diff changeset
    41
 * digest after your calls to one of this digest output stream's
2
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
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 17917
diff changeset
    46
 * {@code write} methods results in
2
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
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 25859
diff changeset
    54
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class DigestOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected MessageDigest digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates a digest output stream, using the specified output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * and message digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param stream the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param digest the message digest to associate with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public DigestOutputStream(OutputStream stream, MessageDigest digest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        super(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        setMessageDigest(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Returns the message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return the message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @see #setMessageDigest(java.security.MessageDigest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public MessageDigest getMessageDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Associates the specified message digest with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param digest the message digest to be associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @see #getMessageDigest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void setMessageDigest(MessageDigest digest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.digest = digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Updates the message digest (if the digest function is on) using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * the specified byte, and in any case writes the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * to the output stream. That is, if the digest function is on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * (see {@link #on(boolean) on}), this method calls
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 17917
diff changeset
   103
     * {@code update} on the message digest associated with this
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 17917
diff changeset
   104
     * stream, passing it the byte {@code b}. This method then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * writes the byte to the output stream, blocking until the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * is actually written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param b the byte to be used for updating and writing to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   111
     * @throws    IOException if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see MessageDigest#update(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public void write(int b) throws IOException {
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   116
        out.write(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            digest.update((byte)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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Updates the message digest (if the digest function is on) using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the specified subarray, and in any case writes the subarray to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the output stream. That is, if the digest function is on (see
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 17917
diff changeset
   126
     * {@link #on(boolean) on}), this method calls {@code update}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * on the message digest associated with this stream, passing it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * the subarray specifications. This method then writes the subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * bytes to the output stream, blocking until the bytes are actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param b the array containing the subarray to be used for updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * and writing to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 17917
diff changeset
   135
     * @param off the offset into {@code b} of the first byte to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * be updated and written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param len the number of bytes of data to be updated and written
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 17917
diff changeset
   139
     * from {@code b}, starting at offset {@code off}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   141
     * @throws    IOException if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see MessageDigest#update(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public void write(byte[] b, int off, int len) throws IOException {
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   146
        out.write(b, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            digest.update(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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Turns the digest function on or off. The default is on.  When
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 17917
diff changeset
   154
     * it is on, a call to one of the {@code write} methods results in an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * update on the message digest.  But when it is off, the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * digest is not updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param on true to turn the digest function on, false to turn it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void on(boolean on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.on = on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Prints a string representation of this digest output stream and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * its associated message digest object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         return "[Digest Output Stream] " + digest.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}