Path: blob/master/src/java.base/share/classes/java/security/DigestOutputStream.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.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* @since 1.254*/55public class DigestOutputStream extends FilterOutputStream {5657private boolean on = true;5859/**60* The message digest associated with this stream.61*/62protected MessageDigest digest;6364/**65* Creates a digest output stream, using the specified output stream66* and message digest.67*68* @param stream the output stream.69*70* @param digest the message digest to associate with this stream.71*/72public DigestOutputStream(OutputStream stream, MessageDigest digest) {73super(stream);74setMessageDigest(digest);75}7677/**78* Returns the message digest associated with this stream.79*80* @return the message digest associated with this stream.81* @see #setMessageDigest(java.security.MessageDigest)82*/83public MessageDigest getMessageDigest() {84return digest;85}8687/**88* Associates the specified message digest with this stream.89*90* @param digest the message digest to be associated with this stream.91* @see #getMessageDigest()92*/93public void setMessageDigest(MessageDigest digest) {94this.digest = digest;95}9697/**98* Updates the message digest (if the digest function is on) using99* the specified byte, and in any case writes the byte100* to the output stream. That is, if the digest function is on101* (see {@link #on(boolean) on}), this method calls102* {@code update} on the message digest associated with this103* stream, passing it the byte {@code b}. This method then104* writes the byte to the output stream, blocking until the byte105* is actually written.106*107* @param b the byte to be used for updating and writing to the108* output stream.109*110* @throws IOException if an I/O error occurs.111*112* @see MessageDigest#update(byte)113*/114public void write(int b) throws IOException {115out.write(b);116if (on) {117digest.update((byte)b);118}119}120121/**122* Updates the message digest (if the digest function is on) using123* the specified subarray, and in any case writes the subarray to124* the output stream. That is, if the digest function is on (see125* {@link #on(boolean) on}), this method calls {@code update}126* on the message digest associated with this stream, passing it127* the subarray specifications. This method then writes the subarray128* bytes to the output stream, blocking until the bytes are actually129* written.130*131* @param b the array containing the subarray to be used for updating132* and writing to the output stream.133*134* @param off the offset into {@code b} of the first byte to135* be updated and written.136*137* @param len the number of bytes of data to be updated and written138* from {@code b}, starting at offset {@code off}.139*140* @throws IOException if an I/O error occurs.141*142* @see MessageDigest#update(byte[], int, int)143*/144public void write(byte[] b, int off, int len) throws IOException {145out.write(b, off, len);146if (on) {147digest.update(b, off, len);148}149}150151/**152* Turns the digest function on or off. The default is on. When153* it is on, a call to one of the {@code write} methods results in an154* update on the message digest. But when it is off, the message155* digest is not updated.156*157* @param on true to turn the digest function on, false to turn it158* off.159*/160public void on(boolean on) {161this.on = on;162}163164/**165* Prints a string representation of this digest output stream and166* its associated message digest object.167*/168public String toString() {169return "[Digest Output Stream] " + digest.toString();170}171}172173174