Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/DigestOutputStream.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.OutputStream;30import java.io.FilterOutputStream;31import java.io.PrintStream;32import java.io.ByteArrayOutputStream;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 output stream's41* {@link #write(int) write} 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 write} methods results in46* an update on the message digest. But when it is off, the message47* digest is not updated. The default is for the stream to be on.48*49* @see MessageDigest50* @see DigestInputStream51*52* @author Benjamin Renaud53*/54public class DigestOutputStream extends FilterOutputStream {5556private boolean on = true;5758/**59* The message digest associated with this stream.60*/61protected MessageDigest digest;6263/**64* Creates a digest output stream, using the specified output stream65* and message digest.66*67* @param stream the output stream.68*69* @param digest the message digest to associate with this stream.70*/71public DigestOutputStream(OutputStream stream, MessageDigest digest) {72super(stream);73setMessageDigest(digest);74}7576/**77* Returns the message digest associated with this stream.78*79* @return the message digest associated with this stream.80* @see #setMessageDigest(java.security.MessageDigest)81*/82public MessageDigest getMessageDigest() {83return digest;84}8586/**87* Associates the specified message digest with this stream.88*89* @param digest the message digest to be associated with this stream.90* @see #getMessageDigest()91*/92public void setMessageDigest(MessageDigest digest) {93this.digest = digest;94}9596/**97* Updates the message digest (if the digest function is on) using98* the specified byte, and in any case writes the byte99* to the output stream. That is, if the digest function is on100* (see {@link #on(boolean) on}), this method calls101* {@code update} on the message digest associated with this102* stream, passing it the byte {@code b}. This method then103* writes the byte to the output stream, blocking until the byte104* is actually written.105*106* @param b the byte to be used for updating and writing to the107* output stream.108*109* @exception IOException if an I/O error occurs.110*111* @see MessageDigest#update(byte)112*/113public void write(int b) throws IOException {114out.write(b);115if (on) {116digest.update((byte)b);117}118}119120/**121* Updates the message digest (if the digest function is on) using122* the specified subarray, and in any case writes the subarray to123* the output stream. That is, if the digest function is on (see124* {@link #on(boolean) on}), this method calls {@code update}125* on the message digest associated with this stream, passing it126* the subarray specifications. This method then writes the subarray127* bytes to the output stream, blocking until the bytes are actually128* written.129*130* @param b the array containing the subarray to be used for updating131* and writing to the output stream.132*133* @param off the offset into {@code b} of the first byte to134* be updated and written.135*136* @param len the number of bytes of data to be updated and written137* from {@code b}, starting at offset {@code off}.138*139* @exception IOException if an I/O error occurs.140*141* @see MessageDigest#update(byte[], int, int)142*/143public void write(byte[] b, int off, int len) throws IOException {144out.write(b, off, len);145if (on) {146digest.update(b, off, len);147}148}149150/**151* Turns the digest function on or off. The default is on. When152* it is on, a call to one of the {@code write} methods results in an153* update on the message digest. But when it is off, the message154* digest is not updated.155*156* @param on true to turn the digest function on, false to turn it157* off.158*/159public void on(boolean on) {160this.on = on;161}162163/**164* Prints a string representation of this digest output stream and165* its associated message digest object.166*/167public String toString() {168return "[Digest Output Stream] " + digest.toString();169}170}171172173