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
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 */
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 */
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 */
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 */
|
1 /*
2 * Copyright (c) 1996, 2019, 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
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 */
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 * @throws IOException if an I/O error occurs.
121 *
122 * @see MessageDigest#update(byte)
123 */
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 * @throws IOException if an I/O error occurs.
158 *
159 * @see MessageDigest#update(byte[], int, int)
160 */
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 */
|