1 |
/* DigestInputStream.java --- An Input stream tied to a message digest |
/* DigestInputStream.java --- An Input stream tied to a message digest |
2 |
Copyright (C) 1999, 2003 Free Software Foundation, Inc. |
Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
38 |
|
|
39 |
package java.security; |
package java.security; |
40 |
//import java.security.MessageDigest; |
|
|
import java.io.InputStream; |
|
41 |
import java.io.FilterInputStream; |
import java.io.FilterInputStream; |
42 |
import java.io.IOException; |
import java.io.IOException; |
43 |
|
import java.io.InputStream; |
44 |
|
|
45 |
/** |
/** |
46 |
DigestInputStream is a class that ties an InputStream with a |
* DigestInputStream is a class that ties an InputStream with a |
47 |
MessageDigest. The Message Digest is used by the class to |
* MessageDigest. The Message Digest is used by the class to |
48 |
update it self as bytes are read from the InputStream. |
* update it self as bytes are read from the InputStream. |
49 |
|
* |
50 |
The updating to the digest depends on the on flag which is set |
* The updating to the digest depends on the on flag which is set |
51 |
to true by default to tell the class to update the data |
* to true by default to tell the class to update the data |
52 |
in the message digest. |
* in the message digest. |
53 |
|
* |
54 |
@version 0.0 |
* @version 0.0 |
55 |
@author Mark Benvenuto <ivymccough@worldnet.att.net> |
* @author Mark Benvenuto <ivymccough@worldnet.att.net> |
56 |
*/ |
*/ |
57 |
public class DigestInputStream extends FilterInputStream |
public class DigestInputStream extends FilterInputStream |
58 |
{ |
{ |
59 |
/** |
/** |
60 |
The message digest for the DigestInputStream |
* The message digest for the DigestInputStream |
61 |
*/ |
*/ |
62 |
protected MessageDigest digest; |
protected MessageDigest digest; |
63 |
|
|
65 |
private boolean state = true; |
private boolean state = true; |
66 |
|
|
67 |
/** |
/** |
68 |
Constructs a new DigestInputStream. |
* Constructs a new DigestInputStream. |
69 |
It associates a MessageDigest with the stream to |
* It associates a MessageDigest with the stream to |
70 |
compute the stream as data is written. |
* compute the stream as data is written. |
71 |
|
* |
72 |
@param stream An InputStream to associate this stream with |
* @param stream An InputStream to associate this stream with |
73 |
@param digest A MessageDigest to hash the stream with |
* @param digest A MessageDigest to hash the stream with |
74 |
*/ |
*/ |
75 |
public DigestInputStream(InputStream stream, MessageDigest digest) |
public DigestInputStream(InputStream stream, MessageDigest digest) |
76 |
{ |
{ |
80 |
} |
} |
81 |
|
|
82 |
/** |
/** |
83 |
Returns the MessageDigest associated with this DigestInputStream |
* Returns the MessageDigest associated with this DigestInputStream |
84 |
|
* |
85 |
@return The MessageDigest used to hash this stream |
* @return The MessageDigest used to hash this stream |
86 |
*/ |
*/ |
87 |
public MessageDigest getMessageDigest() |
public MessageDigest getMessageDigest() |
88 |
{ |
{ |
90 |
} |
} |
91 |
|
|
92 |
/** |
/** |
93 |
Sets the current MessageDigest to current parameter |
* Sets the current MessageDigest to current parameter |
94 |
|
* |
95 |
@param digest A MessageDigest to associate with this stream |
* @param digest A MessageDigest to associate with this stream |
96 |
*/ |
*/ |
97 |
public void setMessageDigest(MessageDigest digest) |
public void setMessageDigest(MessageDigest digest) |
98 |
{ |
{ |
100 |
} |
} |
101 |
|
|
102 |
/** |
/** |
103 |
Reads a byte from the input stream and updates the digest. |
* Reads a byte from the input stream and updates the digest. |
104 |
This method reads the underlying input stream and if the |
* This method reads the underlying input stream and if the |
105 |
on flag is true then updates the message digest. |
* on flag is true then updates the message digest. |
106 |
|
* |
107 |
@return Returns a byte from the input stream, -1 is returned to indicate that |
* @return Returns a byte from the input stream, -1 is returned to indicate that |
108 |
the end of stream was reached before this read call |
* the end of stream was reached before this read call |
109 |
|
* |
110 |
@throws IOException if an IO error occurs in the underlying input stream, |
* @throws IOException if an IO error occurs in the underlying input stream, |
111 |
this error is thrown |
* this error is thrown |
112 |
*/ |
*/ |
113 |
public int read() throws IOException |
public int read() throws IOException |
114 |
{ |
{ |
121 |
} |
} |
122 |
|
|
123 |
/** |
/** |
124 |
Reads bytes from the input stream and updates the digest. |
* Reads bytes from the input stream and updates the digest. |
125 |
This method reads the underlying input stream and if the |
* This method reads the underlying input stream and if the |
126 |
on flag is true then updates the message digest. |
* on flag is true then updates the message digest. |
127 |
|
* |
128 |
@param b a byte array to store the data from the input stream |
* @param b a byte array to store the data from the input stream |
129 |
@param off an offset to start at in the array |
* @param off an offset to start at in the array |
130 |
@param len length of data to read |
* @param len length of data to read |
131 |
@return Returns count of bytes read, -1 is returned to indicate that |
* @return Returns count of bytes read, -1 is returned to indicate that |
132 |
the end of stream was reached before this read call |
* the end of stream was reached before this read call |
133 |
|
* |
134 |
@throws IOException if an IO error occurs in the underlying input stream, |
* @throws IOException if an IO error occurs in the underlying input stream, |
135 |
this error is thrown |
* this error is thrown |
136 |
*/ |
*/ |
137 |
public int read(byte[]b, int off, int len) throws IOException |
public int read(byte[]b, int off, int len) throws IOException |
138 |
{ |
{ |
145 |
} |
} |
146 |
|
|
147 |
/** |
/** |
148 |
Sets the flag specifing if this DigestInputStream updates the |
* Sets the flag specifing if this DigestInputStream updates the |
149 |
digest in the write() methods. The default is on; |
* digest in the write() methods. The default is on; |
150 |
|
* |
151 |
@param on True means it digests stream, false means it does not |
* @param on True means it digests stream, false means it does not |
152 |
*/ |
*/ |
153 |
public void on(boolean on) |
public void on(boolean on) |
154 |
{ |
{ |
156 |
} |
} |
157 |
|
|
158 |
/** |
/** |
159 |
Converts the input stream and underlying message digest to a string. |
* Converts the input stream and underlying message digest to a string. |
160 |
|
* |
161 |
@return A string representing the input stream and message digest. |
* @return A string representing the input stream and message digest. |
162 |
*/ |
*/ |
163 |
public String toString() |
public String toString() |
164 |
{ |
{ |