Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
188 lines (174 loc) · 5.4 KB

File metadata and controls

188 lines (174 loc) · 5.4 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.security;
import java.io.IOException;
import java.io.EOFException;
import java.io.InputStream;
import java.io.FilterInputStream;
import java.io.PrintStream;
import java.io.ByteArrayInputStream;
/**
* A transparent stream that updates the associated message digest using
* the bits going through the stream.
*
* <p>To complete the message digest computation, call one of the
* {@code digest} methods on the associated message
* digest after your calls to one of this digest input stream's
* {@link #read() read} methods.
*
* <p>It is possible to turn this stream on or off (see
* {@link #on(boolean) on}). When it is on, a call to one of the
* {@code read} methods
* results in an update on the message digest. But when it is off,
* the message digest is not updated. The default is for the stream
* to be on.
*
* <p>Note that digest objects can compute only one digest (see
* {@link MessageDigest}),
* so that in order to compute intermediate digests, a caller should
* retain a handle onto the digest object, and clone it for each
* digest to be computed, leaving the orginal digest untouched.
*
* @see MessageDigest
*
* @see DigestOutputStream
*
* @author Benjamin Renaud
*/
public class DigestInputStream extends FilterInputStream {
/* NOTE: This should be made a generic UpdaterInputStream */
/* Are we on or off? */
private boolean on = true;
/**
* The message digest associated with this stream.
*/
protected MessageDigest digest;
/**
* Creates a digest input stream, using the specified input stream
* and message digest.
*
* @param stream the input stream.
*
* @param digest the message digest to associate with this stream.
*/
public DigestInputStream(InputStream stream, MessageDigest digest) {
super(stream);
setMessageDigest(digest);
}
/**
* Returns the message digest associated with this stream.
*
* @return the message digest associated with this stream.
* @see #setMessageDigest(java.security.MessageDigest)
*/
public MessageDigest getMessageDigest() {
return digest;
}
/**
* Associates the specified message digest with this stream.
*
* @param digest the message digest to be associated with this stream.
* @see #getMessageDigest()
*/
public void setMessageDigest(MessageDigest digest) {
this.digest = digest;
}
/**
* Reads a byte, and updates the message digest (if the digest
* function is on). That is, this method reads a byte from the
* input stream, blocking until the byte is actually read. If the
* digest function is on (see {@link #on(boolean) on}), this method
* will then call {@code update} on the message digest associated
* with this stream, passing it the byte read.
*
* @return the byte read.
*
* @exception IOException if an I/O error occurs.
*
* @see MessageDigest#update(byte)
*/
public int read() throws IOException {
int ch = in.read();
if (on && ch != -1) {
digest.update((byte)ch);
}
return ch;
}
/**
* Reads into a byte array, and updates the message digest (if the
* digest function is on). That is, this method reads up to
* {@code len} bytes from the input stream into the array
* {@code b}, starting at offset {@code off}. This method
* blocks until the data is actually
* read. If the digest function is on (see
* {@link #on(boolean) on}), this method will then call {@code update}
* on the message digest associated with this stream, passing it
* the data.
*
* @param b the array into which the data is read.
*
* @param off the starting offset into {@code b} of where the
* data should be placed.
*
* @param len the maximum number of bytes to be read from the input
* stream into b, starting at offset {@code off}.
*
* @return the actual number of bytes read. This is less than
* {@code len} if the end of the stream is reached prior to
* reading {@code len} bytes. -1 is returned if no bytes were
* read because the end of the stream had already been reached when
* the call was made.
*
* @exception IOException if an I/O error occurs.
*
* @see MessageDigest#update(byte[], int, int)
*/
public int read(byte[] b, int off, int len) throws IOException {
int result = in.read(b, off, len);
if (on && result != -1) {
digest.update(b, off, result);
}
return result;
}
/**
* Turns the digest function on or off. The default is on. When
* it is on, a call to one of the {@code read} methods results in an
* update on the message digest. But when it is off, the message
* digest is not updated.
*
* @param on true to turn the digest function on, false to turn
* it off.
*/
public void on(boolean on) {
this.on = on;
}
/**
* Prints a string representation of this digest input stream and
* its associated message digest object.
*/
public String toString() {
return "[Digest Input Stream] " + digest.toString();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.