KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > DigestInputStream


1 /*
2  * @(#)DigestInputStream.java 1.37 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.security;
9
10 import java.io.IOException JavaDoc;
11 import java.io.EOFException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.FilterInputStream JavaDoc;
14 import java.io.PrintStream JavaDoc;
15 import java.io.ByteArrayInputStream JavaDoc;
16
17 /**
18  * A transparent stream that updates the associated message digest using
19  * the bits going through the stream.
20  *
21  * <p>To complete the message digest computation, call one of the
22  * <code>digest</code> methods on the associated message
23  * digest after your calls to one of this digest input stream's
24  * {@link #read() read} methods.
25  *
26  * <p>It is possible to turn this stream on or off (see
27  * {@link #on(boolean) on}). When it is on, a call to one of the
28  * <code>read</code> methods
29  * results in an update on the message digest. But when it is off,
30  * the message digest is not updated. The default is for the stream
31  * to be on.
32  *
33  * <p>Note that digest objects can compute only one digest (see
34  * {@link MessageDigest}),
35  * so that in order to compute intermediate digests, a caller should
36  * retain a handle onto the digest object, and clone it for each
37  * digest to be computed, leaving the orginal digest untouched.
38  *
39  * @see MessageDigest
40  *
41  * @see DigestOutputStream
42  *
43  * @version 1.37 03/12/19
44  * @author Benjamin Renaud
45  */

46
47 public class DigestInputStream extends FilterInputStream JavaDoc {
48
49     /* NOTE: This should be made a generic UpdaterInputStream */
50
51     /* Are we on or off? */
52     private boolean on = true;
53
54     /**
55      * The message digest associated with this stream.
56      */

57     protected MessageDigest JavaDoc digest;
58
59     /**
60      * Creates a digest input stream, using the specified input stream
61      * and message digest.
62      *
63      * @param stream the input stream.
64      *
65      * @param digest the message digest to associate with this stream.
66      */

67     public DigestInputStream(InputStream JavaDoc stream, MessageDigest JavaDoc digest) {
68     super(stream);
69     setMessageDigest(digest);
70     }
71
72     /**
73      * Returns the message digest associated with this stream.
74      *
75      * @return the message digest associated with this stream.
76      * @see #setMessageDigest(java.security.MessageDigest)
77      */

78     public MessageDigest JavaDoc getMessageDigest() {
79     return digest;
80     }
81
82     /**
83      * Associates the specified message digest with this stream.
84      *
85      * @param digest the message digest to be associated with this stream.
86      * @see #getMessageDigest()
87      */

88     public void setMessageDigest(MessageDigest JavaDoc digest) {
89     this.digest = digest;
90     }
91
92     /**
93      * Reads a byte, and updates the message digest (if the digest
94      * function is on). That is, this method reads a byte from the
95      * input stream, blocking until the byte is actually read. If the
96      * digest function is on (see {@link #on(boolean) on}), this method
97      * will then call <code>update</code> on the message digest associated
98      * with this stream, passing it the byte read.
99      *
100      * @return the byte read.
101      *
102      * @exception IOException if an I/O error occurs.
103      *
104      * @see MessageDigest#update(byte)
105      */

106     public int read() throws IOException JavaDoc {
107     int ch = in.read();
108     if (on && ch != -1) {
109         digest.update((byte)ch);
110     }
111     return ch;
112     }
113
114     /**
115      * Reads into a byte array, and updates the message digest (if the
116      * digest function is on). That is, this method reads up to
117      * <code>len</code> bytes from the input stream into the array
118      * <code>b</code>, starting at offset <code>off</code>. This method
119      * blocks until the data is actually
120      * read. If the digest function is on (see
121      * {@link #on(boolean) on}), this method will then call <code>update</code>
122      * on the message digest associated with this stream, passing it
123      * the data.
124      *
125      * @param b the array into which the data is read.
126      *
127      * @param off the starting offset into <code>b</code> of where the
128      * data should be placed.
129      *
130      * @param len the maximum number of bytes to be read from the input
131      * stream into b, starting at offset <code>off</code>.
132      *
133      * @return the actual number of bytes read. This is less than
134      * <code>len</code> if the end of the stream is reached prior to
135      * reading <code>len</code> bytes. -1 is returned if no bytes were
136      * read because the end of the stream had already been reached when
137      * the call was made.
138      *
139      * @exception IOException if an I/O error occurs.
140      *
141      * @see MessageDigest#update(byte[], int, int)
142      */

143     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
144     int result = in.read(b, off, len);
145     if (on && result != -1) {
146         digest.update(b, off, result);
147     }
148     return result;
149     }
150
151     /**
152      * Turns the digest function on or off. The default is on. When
153      * it is on, a call to one of the <code>read</code> methods results in an
154      * update on the message digest. But when it is off, the message
155      * digest is not updated.
156      *
157      * @param on true to turn the digest function on, false to turn
158      * it off.
159      */

160     public void on(boolean on) {
161     this.on = on;
162     }
163
164     /**
165      * Prints a string representation of this digest input stream and
166      * its associated message digest object.
167      */

168      public String JavaDoc toString() {
169      return "[Digest Input Stream] " + digest.toString();
170      }
171 }
172
173
174
175
176
Popular Tags