Path: blob/master/src/java.base/share/classes/java/security/DigestInputStream.java
41152 views
/*1* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627import java.io.IOException;28import java.io.EOFException;29import java.io.InputStream;30import java.io.FilterInputStream;31import java.io.PrintStream;32import java.io.ByteArrayInputStream;3334/**35* A transparent stream that updates the associated message digest using36* the bits going through the stream.37*38* <p>To complete the message digest computation, call one of the39* {@code digest} methods on the associated message40* digest after your calls to one of this digest input stream's41* {@link #read() read} methods.42*43* <p>It is possible to turn this stream on or off (see44* {@link #on(boolean) on}). When it is on, a call to one of the45* {@code read} methods46* results in an update on the message digest. But when it is off,47* the message digest is not updated. The default is for the stream48* to be on.49*50* <p>Note that digest objects can compute only one digest (see51* {@link MessageDigest}),52* so that in order to compute intermediate digests, a caller should53* retain a handle onto the digest object, and clone it for each54* digest to be computed, leaving the original digest untouched.55*56* @see MessageDigest57*58* @see DigestOutputStream59*60* @author Benjamin Renaud61* @since 1.262*/6364public class DigestInputStream extends FilterInputStream {6566/* NOTE: This should be made a generic UpdaterInputStream */6768/* Are we on or off? */69private boolean on = true;7071/**72* The message digest associated with this stream.73*/74protected MessageDigest digest;7576/**77* Creates a digest input stream, using the specified input stream78* and message digest.79*80* @param stream the input stream.81*82* @param digest the message digest to associate with this stream.83*/84public DigestInputStream(InputStream stream, MessageDigest digest) {85super(stream);86setMessageDigest(digest);87}8889/**90* Returns the message digest associated with this stream.91*92* @return the message digest associated with this stream.93* @see #setMessageDigest(java.security.MessageDigest)94*/95public MessageDigest getMessageDigest() {96return digest;97}9899/**100* Associates the specified message digest with this stream.101*102* @param digest the message digest to be associated with this stream.103* @see #getMessageDigest()104*/105public void setMessageDigest(MessageDigest digest) {106this.digest = digest;107}108109/**110* Reads a byte, and updates the message digest (if the digest111* function is on). That is, this method reads a byte from the112* input stream, blocking until the byte is actually read. If the113* digest function is on (see {@link #on(boolean) on}), this method114* will then call {@code update} on the message digest associated115* with this stream, passing it the byte read.116*117* @return the byte read.118*119* @throws IOException if an I/O error occurs.120*121* @see MessageDigest#update(byte)122*/123public int read() throws IOException {124int ch = in.read();125if (on && ch != -1) {126digest.update((byte)ch);127}128return ch;129}130131/**132* Reads into a byte array, and updates the message digest (if the133* digest function is on). That is, this method reads up to134* {@code len} bytes from the input stream into the array135* {@code b}, starting at offset {@code off}. This method136* blocks until the data is actually137* read. If the digest function is on (see138* {@link #on(boolean) on}), this method will then call {@code update}139* on the message digest associated with this stream, passing it140* the data.141*142* @param b the array into which the data is read.143*144* @param off the starting offset into {@code b} of where the145* data should be placed.146*147* @param len the maximum number of bytes to be read from the input148* stream into b, starting at offset {@code off}.149*150* @return the actual number of bytes read. This is less than151* {@code len} if the end of the stream is reached prior to152* reading {@code len} bytes. -1 is returned if no bytes were153* read because the end of the stream had already been reached when154* the call was made.155*156* @throws IOException if an I/O error occurs.157*158* @see MessageDigest#update(byte[], int, int)159*/160public int read(byte[] b, int off, int len) throws IOException {161int result = in.read(b, off, len);162if (on && result != -1) {163digest.update(b, off, result);164}165return result;166}167168/**169* Turns the digest function on or off. The default is on. When170* it is on, a call to one of the {@code read} methods results in an171* update on the message digest. But when it is off, the message172* digest is not updated.173*174* @param on true to turn the digest function on, false to turn175* it off.176*/177public void on(boolean on) {178this.on = on;179}180181/**182* Prints a string representation of this digest input stream and183* its associated message digest object.184*/185public String toString() {186return "[Digest Input Stream] " + digest.toString();187}188}189190191