src/java.base/share/classes/java/security/DigestInputStream.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.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FilterInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ByteArrayInputStream;
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: 5506
diff changeset
    40
 * {@code digest} methods on the associated message
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * digest after your calls to one of this digest input stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@link #read() read} 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: 5506
diff changeset
    46
 * {@code read} methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * results in an update on the message digest.  But when it is off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the message digest is not updated. The default is for the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * to be on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>Note that digest objects can compute only one digest (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@link MessageDigest}),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * so that in order to compute intermediate digests, a caller should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * retain a handle onto the digest object, and clone it for each
40789
43b42538af90 8165413: Typos in javadoc: extra period, wrong number, misspelled word
igerasim
parents: 25859
diff changeset
    55
 * digest to be computed, leaving the original digest untouched.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see DigestOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Benjamin Renaud
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 40789
diff changeset
    62
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class DigestInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* NOTE: This should be made a generic UpdaterInputStream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /* Are we on or off? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private boolean on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected MessageDigest 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
     * Creates a digest input stream, using the specified input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * and message digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param stream the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param digest the message digest to associate with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public DigestInputStream(InputStream stream, MessageDigest digest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        super(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        setMessageDigest(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return the message digest associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see #setMessageDigest(java.security.MessageDigest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public MessageDigest getMessageDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Associates the specified message digest with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param digest the message digest to be associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see #getMessageDigest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void setMessageDigest(MessageDigest digest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.digest = digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Reads a byte, and updates the message digest (if the digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * function is on).  That is, this method reads a byte from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * input stream, blocking until the byte is actually read. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * digest function is on (see {@link #on(boolean) on}), this method
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   115
     * will then call {@code update} on the message digest associated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * with this stream, passing it the byte read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return the byte read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   120
     * @throws    IOException if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see MessageDigest#update(byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (on && ch != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            digest.update((byte)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Reads into a byte array, and updates the message digest (if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * digest function is on).  That is, this method reads up to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   135
     * {@code len} bytes from the input stream into the array
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   136
     * {@code b}, starting at offset {@code off}. This method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * blocks until the data is actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * read. If the digest function is on (see
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   139
     * {@link #on(boolean) on}), this method will then call {@code update}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * on the message digest associated with this stream, passing it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param b the array into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   145
     * @param off the starting offset into {@code b} of where the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * data should be placed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param len the maximum number of bytes to be read from the input
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   149
     * stream into b, starting at offset {@code off}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return  the actual number of bytes read. This is less than
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   152
     * {@code len} if the end of the stream is reached prior to
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   153
     * reading {@code len} bytes. -1 is returned if no bytes were
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * read because the end of the stream had already been reached when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * the call was made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   157
     * @throws    IOException if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @see MessageDigest#update(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int result = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (on && result != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            digest.update(b, off, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * 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: 5506
diff changeset
   171
     * it is on, a call to one of the {@code read} methods results in an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * update on the message digest.  But when it is off, the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * digest is not updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param on true to turn the digest function on, false to turn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * it off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void on(boolean on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        this.on = on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Prints a string representation of this digest input stream and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * its associated message digest object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         return "[Digest Input Stream] " + digest.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}