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  * @since 1.2
55  */
56 public class DigestOutputStream extends FilterOutputStream {
57 
58     private boolean on = true;
59 
60     /**
61      * The message digest associated with this stream.
62      */
63     protected MessageDigest digest;
64 
65     /**
66      * Creates a digest output stream, using the specified output stream
67      * and message digest.
68      *
69      * @param stream the output stream.
70      *
71      * @param digest the message digest to associate with this stream.
72      */
DigestOutputStream(OutputStream stream, MessageDigest digest)73     public DigestOutputStream(OutputStream stream, MessageDigest digest) {
74         super(stream);
75         setMessageDigest(digest);
76     }
77 
78     /**
79      * Returns the message digest associated with this stream.
80      *
81      * @return the message digest associated with this stream.
82      * @see #setMessageDigest(java.security.MessageDigest)
83      */
getMessageDigest()84     public MessageDigest getMessageDigest() {
85         return digest;
86     }
87 
88     /**
89      * Associates the specified message digest with this stream.
90      *
91      * @param digest the message digest to be associated with this stream.
92      * @see #getMessageDigest()
93      */
setMessageDigest(MessageDigest digest)94     public void setMessageDigest(MessageDigest digest) {
95         this.digest = digest;
96     }
97 
98     /**
99      * Updates the message digest (if the digest function is on) using
100      * the specified byte, and in any case writes the byte
101      * to the output stream. That is, if the digest function is on
102      * (see {@link #on(boolean) on}), this method calls
103      * {@code update} on the message digest associated with this
104      * stream, passing it the byte {@code b}. This method then
105      * writes the byte to the output stream, blocking until the byte
106      * is actually written.
107      *
108      * @param b the byte to be used for updating and writing to the
109      * output stream.
110      *
111      * @exception IOException if an I/O error occurs.
112      *
113      * @see MessageDigest#update(byte)
114      */
write(int b)115     public void write(int b) throws IOException {
116         out.write(b);
117         if (on) {
118             digest.update((byte)b);
119         }
120     }
121 
122     /**
123      * Updates the message digest (if the digest function is on) using
124      * the specified subarray, and in any case writes the subarray to
125      * the output stream. That is, if the digest function is on (see
126      * {@link #on(boolean) on}), this method calls {@code update}
127      * on the message digest associated with this stream, passing it
128      * the subarray specifications. This method then writes the subarray
129      * bytes to the output stream, blocking until the bytes are actually
130      * written.
131      *
132      * @param b the array containing the subarray to be used for updating
133      * and writing to the output stream.
134      *
135      * @param off the offset into {@code b} of the first byte to
136      * be updated and written.
137      *
138      * @param len the number of bytes of data to be updated and written
139      * from {@code b}, starting at offset {@code off}.
140      *
141      * @exception IOException if an I/O error occurs.
142      *
143      * @see MessageDigest#update(byte[], int, int)
144      */
write(byte[] b, int off, int len)145     public void write(byte[] b, int off, int len) throws IOException {
146         // BEGIN Android-added: perform checks for parameters first.
147         // See org.apache.harmony.security.tests.j.s.DigestOutputStreamTest#test_write$BII_6
148         if (b == null || off + len > b.length) {
149             throw new IllegalArgumentException("wrong parameters for write");
150         }
151         if (off < 0 || len < 0) {
152             throw new IndexOutOfBoundsException("wrong index for write");
153         }
154         // END Android-added: perform checks for parameters first.
155         out.write(b, off, len);
156         if (on) {
157             digest.update(b, off, len);
158         }
159     }
160 
161     /**
162      * Turns the digest function on or off. The default is on.  When
163      * it is on, a call to one of the {@code write} methods results in an
164      * update on the message digest.  But when it is off, the message
165      * digest is not updated.
166      *
167      * @param on true to turn the digest function on, false to turn it
168      * off.
169      */
on(boolean on)170     public void on(boolean on) {
171         this.on = on;
172     }
173 
174     /**
175      * Prints a string representation of this digest output stream and
176      * its associated message digest object.
177      */
toString()178      public String toString() {
179          return "[Digest Output Stream] " + digest.toString();
180      }
181 }
182