1 /*
2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.security;
27 
28 import java.io.IOException;
29 import java.io.EOFException;
30 import java.io.OutputStream;
31 import java.io.FilterOutputStream;
32 import java.io.PrintStream;
33 import java.io.ByteArrayOutputStream;
34 
35 /**
36  * A transparent stream that updates the associated message digest using
37  * the bits going through the stream.
38  *
39  * <p>To complete the message digest computation, call one of the
40  * {@code digest} methods on the associated message
41  * digest after your calls to one of this digest output stream's
42  * {@link #write(int) write} methods.
43  *
44  * <p>It is possible to turn this stream on or off (see
45  * {@link #on(boolean) on}). When it is on, a call to one of the
46  * {@code write} methods results in
47  * an update on the message digest.  But when it is off, the message
48  * digest is not updated. The default is for the stream to be on.
49  *
50  * @see MessageDigest
51  * @see DigestInputStream
52  *
53  * @author Benjamin Renaud
54  */
55 public class DigestOutputStream extends FilterOutputStream {
56 
57     private boolean on = true;
58 
59     /**
60      * The message digest associated with this stream.
61      */
62     protected MessageDigest digest;
63 
64     /**
65      * Creates a digest output stream, using the specified output stream
66      * and message digest.
67      *
68      * @param stream the output stream.
69      *
70      * @param digest the message digest to associate with this stream.
71      */
DigestOutputStream(OutputStream stream, MessageDigest digest)72     public DigestOutputStream(OutputStream stream, MessageDigest digest) {
73         super(stream);
74         setMessageDigest(digest);
75     }
76 
77     /**
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      */
getMessageDigest()83     public MessageDigest getMessageDigest() {
84         return digest;
85     }
86 
87     /**
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      */
setMessageDigest(MessageDigest digest)93     public void setMessageDigest(MessageDigest digest) {
94         this.digest = digest;
95     }
96 
97     /**
98      * Updates the message digest (if the digest function is on) using
99      * the specified byte, and in any case writes the byte
100      * to the output stream. That is, if the digest function is on
101      * (see {@link #on(boolean) on}), this method calls
102      * {@code update} on the message digest associated with this
103      * stream, passing it the byte {@code b}. This method then
104      * writes the byte to the output stream, blocking until the byte
105      * is actually written.
106      *
107      * @param b the byte to be used for updating and writing to the
108      * output stream.
109      *
110      * @exception IOException if an I/O error occurs.
111      *
112      * @see MessageDigest#update(byte)
113      */
write(int b)114     public void write(int b) throws IOException {
115         out.write(b);
116         if (on) {
117             digest.update((byte)b);
118         }
119     }
120 
121     /**
122      * Updates the message digest (if the digest function is on) using
123      * the specified subarray, and in any case writes the subarray to
124      * the output stream. That is, if the digest function is on (see
125      * {@link #on(boolean) on}), this method calls {@code update}
126      * on the message digest associated with this stream, passing it
127      * the subarray specifications. This method then writes the subarray
128      * bytes to the output stream, blocking until the bytes are actually
129      * written.
130      *
131      * @param b the array containing the subarray to be used for updating
132      * and writing to the output stream.
133      *
134      * @param off the offset into {@code b} of the first byte to
135      * be updated and written.
136      *
137      * @param len the number of bytes of data to be updated and written
138      * from {@code b}, starting at offset {@code off}.
139      *
140      * @exception IOException if an I/O error occurs.
141      *
142      * @see MessageDigest#update(byte[], int, int)
143      */
write(byte[] b, int off, int len)144     public void write(byte[] b, int off, int len) throws IOException {
145         // BEGIN Android-added: perform checks for parameters first.
146         // See org.apache.harmony.security.tests.j.s.DigestOutputStreamTest#test_write$BII_6
147         if (b == null || off + len > b.length) {
148             throw new IllegalArgumentException("wrong parameters for write");
149         }
150         if (off < 0 || len < 0) {
151             throw new IndexOutOfBoundsException("wrong index for write");
152         }
153         // END Android-added
154         out.write(b, off, len);
155         if (on) {
156             digest.update(b, off, len);
157         }
158     }
159 
160     /**
161      * Turns the digest function on or off. The default is on.  When
162      * it is on, a call to one of the {@code write} methods results in an
163      * update on the message digest.  But when it is off, the message
164      * digest is not updated.
165      *
166      * @param on true to turn the digest function on, false to turn it
167      * off.
168      */
on(boolean on)169     public void on(boolean on) {
170         this.on = on;
171     }
172 
173     /**
174      * Prints a string representation of this digest output stream and
175      * its associated message digest object.
176      */
toString()177      public String toString() {
178          return "[Digest Output Stream] " + digest.toString();
179      }
180 }
181