Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/DigestInputStream.java
38829 views
/*1* Copyright (c) 1996, 2013, 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 orginal digest untouched.55*56* @see MessageDigest57*58* @see DigestOutputStream59*60* @author Benjamin Renaud61*/6263public class DigestInputStream extends FilterInputStream {6465/* NOTE: This should be made a generic UpdaterInputStream */6667/* Are we on or off? */68private boolean on = true;6970/**71* The message digest associated with this stream.72*/73protected MessageDigest digest;7475/**76* Creates a digest input stream, using the specified input stream77* and message digest.78*79* @param stream the input stream.80*81* @param digest the message digest to associate with this stream.82*/83public DigestInputStream(InputStream stream, MessageDigest digest) {84super(stream);85setMessageDigest(digest);86}8788/**89* Returns the message digest associated with this stream.90*91* @return the message digest associated with this stream.92* @see #setMessageDigest(java.security.MessageDigest)93*/94public MessageDigest getMessageDigest() {95return digest;96}9798/**99* Associates the specified message digest with this stream.100*101* @param digest the message digest to be associated with this stream.102* @see #getMessageDigest()103*/104public void setMessageDigest(MessageDigest digest) {105this.digest = digest;106}107108/**109* Reads a byte, and updates the message digest (if the digest110* function is on). That is, this method reads a byte from the111* input stream, blocking until the byte is actually read. If the112* digest function is on (see {@link #on(boolean) on}), this method113* will then call {@code update} on the message digest associated114* with this stream, passing it the byte read.115*116* @return the byte read.117*118* @exception IOException if an I/O error occurs.119*120* @see MessageDigest#update(byte)121*/122public int read() throws IOException {123int ch = in.read();124if (on && ch != -1) {125digest.update((byte)ch);126}127return ch;128}129130/**131* Reads into a byte array, and updates the message digest (if the132* digest function is on). That is, this method reads up to133* {@code len} bytes from the input stream into the array134* {@code b}, starting at offset {@code off}. This method135* blocks until the data is actually136* read. If the digest function is on (see137* {@link #on(boolean) on}), this method will then call {@code update}138* on the message digest associated with this stream, passing it139* the data.140*141* @param b the array into which the data is read.142*143* @param off the starting offset into {@code b} of where the144* data should be placed.145*146* @param len the maximum number of bytes to be read from the input147* stream into b, starting at offset {@code off}.148*149* @return the actual number of bytes read. This is less than150* {@code len} if the end of the stream is reached prior to151* reading {@code len} bytes. -1 is returned if no bytes were152* read because the end of the stream had already been reached when153* the call was made.154*155* @exception IOException if an I/O error occurs.156*157* @see MessageDigest#update(byte[], int, int)158*/159public int read(byte[] b, int off, int len) throws IOException {160int result = in.read(b, off, len);161if (on && result != -1) {162digest.update(b, off, result);163}164return result;165}166167/**168* Turns the digest function on or off. The default is on. When169* it is on, a call to one of the {@code read} methods results in an170* update on the message digest. But when it is off, the message171* digest is not updated.172*173* @param on true to turn the digest function on, false to turn174* it off.175*/176public void on(boolean on) {177this.on = on;178}179180/**181* Prints a string representation of this digest input stream and182* its associated message digest object.183*/184public String toString() {185return "[Digest Input Stream] " + digest.toString();186}187}188189190