KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > DigestOutputStream


1 /*
2  * @(#)DigestOutputStream.java 1.31 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.OutputStream JavaDoc;
13 import java.io.FilterOutputStream JavaDoc;
14 import java.io.PrintStream JavaDoc;
15 import java.io.ByteArrayOutputStream 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 ouput stream's
24  * {@link #write(int) write} 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>write</code> methods results in
29  * an update on the message digest. But when it is off, the message
30  * digest is not updated. The default is for the stream to be on.
31  *
32  * @see MessageDigest
33  * @see DigestInputStream
34  *
35  * @version 1.31 03/12/19
36  * @author Benjamin Renaud
37  */

38 public class DigestOutputStream extends FilterOutputStream JavaDoc {
39
40     private boolean on = true;
41
42     /**
43      * The message digest associated with this stream.
44      */

45     protected MessageDigest JavaDoc digest;
46
47     /**
48      * Creates a digest output stream, using the specified output stream
49      * and message digest.
50      *
51      * @param stream the output stream.
52      *
53      * @param digest the message digest to associate with this stream.
54      */

55     public DigestOutputStream(OutputStream JavaDoc stream, MessageDigest JavaDoc digest) {
56     super(stream);
57     setMessageDigest(digest);
58     }
59
60     /**
61      * Returns the message digest associated with this stream.
62      *
63      * @return the message digest associated with this stream.
64      * @see #setMessageDigest(java.security.MessageDigest)
65      */

66     public MessageDigest JavaDoc getMessageDigest() {
67     return digest;
68     }
69
70     /**
71      * Associates the specified message digest with this stream.
72      *
73      * @param digest the message digest to be associated with this stream.
74      * @see #getMessageDigest()
75      */

76     public void setMessageDigest(MessageDigest JavaDoc digest) {
77     this.digest = digest;
78     }
79
80     /**
81      * Updates the message digest (if the digest function is on) using
82      * the specified byte, and in any case writes the byte
83      * to the output stream. That is, if the digest function is on
84      * (see {@link #on(boolean) on}), this method calls
85      * <code>update</code> on the message digest associated with this
86      * stream, passing it the byte <code>b</code>. This method then
87      * writes the byte to the output stream, blocking until the byte
88      * is actually written.
89      *
90      * @param b the byte to be used for updating and writing to the
91      * output stream.
92      *
93      * @exception IOException if an I/O error occurs.
94      *
95      * @see MessageDigest#update(byte)
96      */

97     public void write(int b) throws IOException JavaDoc {
98     if (on) {
99         digest.update((byte)b);
100     }
101     out.write(b);
102     }
103
104     /**
105      * Updates the message digest (if the digest function is on) using
106      * the specified subarray, and in any case writes the subarray to
107      * the output stream. That is, if the digest function is on (see
108      * {@link #on(boolean) on}), this method calls <code>update</code>
109      * on the message digest associated with this stream, passing it
110      * the subarray specifications. This method then writes the subarray
111      * bytes to the output stream, blocking until the bytes are actually
112      * written.
113      *
114      * @param b the array containing the subarray to be used for updating
115      * and writing to the output stream.
116      *
117      * @param off the offset into <code>b</code> of the first byte to
118      * be updated and written.
119      *
120      * @param len the number of bytes of data to be updated and written
121      * from <code>b</code>, starting at offset <code>off</code>.
122      *
123      * @exception IOException if an I/O error occurs.
124      *
125      * @see MessageDigest#update(byte[], int, int)
126      */

127     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
128     if (on) {
129         digest.update(b, off, len);
130     }
131     out.write(b, off, len);
132     }
133
134     /**
135      * Turns the digest function on or off. The default is on. When
136      * it is on, a call to one of the <code>write</code> methods results in an
137      * update on the message digest. But when it is off, the message
138      * digest is not updated.
139      *
140      * @param on true to turn the digest function on, false to turn it
141      * off.
142      */

143     public void on(boolean on) {
144     this.on = on;
145     }
146
147     /**
148      * Prints a string representation of this digest output stream and
149      * its associated message digest object.
150      */

151      public String JavaDoc toString() {
152      return "[Digest Output Stream] " + digest.toString();
153      }
154 }
155
156
157
158
159
Popular Tags