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.InputStream;
31 import java.io.FilterInputStream;
32 import java.io.PrintStream;
33 import java.io.ByteArrayInputStream;
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 input stream's
42  * {@link #read() read} 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 read} methods
47  * results in an update on the message digest.  But when it is off,
48  * the message digest is not updated. The default is for the stream
49  * to be on.
50  *
51  * <p>Note that digest objects can compute only one digest (see
52  * {@link MessageDigest}),
53  * so that in order to compute intermediate digests, a caller should
54  * retain a handle onto the digest object, and clone it for each
55  * digest to be computed, leaving the original digest untouched.
56  *
57  * @see MessageDigest
58  *
59  * @see DigestOutputStream
60  *
61  * @author Benjamin Renaud
62  * @since 1.2
63  */
64 
65 public class DigestInputStream extends FilterInputStream {
66 
67     /* NOTE: This should be made a generic UpdaterInputStream */
68 
69     /* Are we on or off? */
70     private boolean on = true;
71 
72     /**
73      * The message digest associated with this stream.
74      */
75     protected MessageDigest digest;
76 
77     /**
78      * Creates a digest input stream, using the specified input stream
79      * and message digest.
80      *
81      * @param stream the input stream.
82      *
83      * @param digest the message digest to associate with this stream.
84      */
DigestInputStream(InputStream stream, MessageDigest digest)85     public DigestInputStream(InputStream stream, MessageDigest digest) {
86         super(stream);
87         setMessageDigest(digest);
88     }
89 
90     /**
91      * Returns the message digest associated with this stream.
92      *
93      * @return the message digest associated with this stream.
94      * @see #setMessageDigest(java.security.MessageDigest)
95      */
getMessageDigest()96     public MessageDigest getMessageDigest() {
97         return digest;
98     }
99 
100     /**
101      * Associates the specified message digest with this stream.
102      *
103      * @param digest the message digest to be associated with this stream.
104      * @see #getMessageDigest()
105      */
setMessageDigest(MessageDigest digest)106     public void setMessageDigest(MessageDigest digest) {
107         this.digest = digest;
108     }
109 
110     /**
111      * Reads a byte, and updates the message digest (if the digest
112      * function is on).  That is, this method reads a byte from the
113      * input stream, blocking until the byte is actually read. If the
114      * digest function is on (see {@link #on(boolean) on}), this method
115      * will then call {@code update} on the message digest associated
116      * with this stream, passing it the byte read.
117      *
118      * @return the byte read.
119      *
120      * @exception IOException if an I/O error occurs.
121      *
122      * @see MessageDigest#update(byte)
123      */
read()124     public int read() throws IOException {
125         int ch = in.read();
126         if (on && ch != -1) {
127             digest.update((byte)ch);
128         }
129         return ch;
130     }
131 
132     /**
133      * Reads into a byte array, and updates the message digest (if the
134      * digest function is on).  That is, this method reads up to
135      * {@code len} bytes from the input stream into the array
136      * {@code b}, starting at offset {@code off}. This method
137      * blocks until the data is actually
138      * read. If the digest function is on (see
139      * {@link #on(boolean) on}), this method will then call {@code update}
140      * on the message digest associated with this stream, passing it
141      * the data.
142      *
143      * @param b the array into which the data is read.
144      *
145      * @param off the starting offset into {@code b} of where the
146      * data should be placed.
147      *
148      * @param len the maximum number of bytes to be read from the input
149      * stream into b, starting at offset {@code off}.
150      *
151      * @return  the actual number of bytes read. This is less than
152      * {@code len} if the end of the stream is reached prior to
153      * reading {@code len} bytes. -1 is returned if no bytes were
154      * read because the end of the stream had already been reached when
155      * the call was made.
156      *
157      * @exception IOException if an I/O error occurs.
158      *
159      * @see MessageDigest#update(byte[], int, int)
160      */
read(byte[] b, int off, int len)161     public int read(byte[] b, int off, int len) throws IOException {
162         int result = in.read(b, off, len);
163         if (on && result != -1) {
164             digest.update(b, off, result);
165         }
166         return result;
167     }
168 
169     /**
170      * Turns the digest function on or off. The default is on.  When
171      * it is on, a call to one of the {@code read} methods results in an
172      * update on the message digest.  But when it is off, the message
173      * digest is not updated.
174      *
175      * @param on true to turn the digest function on, false to turn
176      * it off.
177      */
on(boolean on)178     public void on(boolean on) {
179         this.on = on;
180     }
181 
182     /**
183      * Prints a string representation of this digest input stream and
184      * its associated message digest object.
185      */
toString()186      public String toString() {
187          return "[Digest Input Stream] " + digest.toString();
188      }
189 }
190