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
23 * questions.
24 */
25
26 package java.io;
27
28 import java.util.Arrays;
29 import java.util.Objects;
30
31 /**
32 * This abstract class is the superclass of all classes representing
33 * an input stream of bytes.
34 *
35 * <p> Applications that need to define a subclass of <code>InputStream</code>
36 * must always provide a method that returns the next byte of input.
37 *
38 * @author Arthur van Hoff
39 * @see java.io.BufferedInputStream
40 * @see java.io.ByteArrayInputStream
41 * @see java.io.DataInputStream
42 * @see java.io.FilterInputStream
43 * @see java.io.InputStream#read()
44 * @see java.io.OutputStream
45 * @see java.io.PushbackInputStream
46 * @since 1.0
47 */
212 *
213 * <p> The behavior for the case where the input stream is <i>asynchronously
214 * closed</i>, or the thread interrupted during the read, is highly input
215 * stream specific, and therefore not specified.
216 *
217 * <p> If an I/O error occurs reading from the input stream, then it may do
218 * so after some, but not all, bytes have been read. Consequently the input
219 * stream may not be at end of stream and may be in an inconsistent state.
220 * It is strongly recommended that the stream be promptly closed if an I/O
221 * error occurs.
222 *
223 * @return a byte array containing the bytes read from this input stream
224 * @throws IOException if an I/O error occurs
225 * @throws OutOfMemoryError if an array of the required size cannot be
226 * allocated. For example, if an array larger than {@code 2GB} would
227 * be required to store the bytes.
228 *
229 * @since 9
230 */
231 public byte[] readAllBytes() throws IOException {
232 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
233 int capacity = buf.length;
234 int nread = 0;
235 int n;
236 for (;;) {
237 // read to EOF which may read more or less than initial buffer size
238 while ((n = read(buf, nread, capacity - nread)) > 0)
239 nread += n;
240
241 // if the last call to read returned -1, then we're done
242 if (n < 0)
243 break;
244
245 // need to allocate a larger buffer
246 if (capacity <= MAX_BUFFER_SIZE - capacity) {
247 capacity = capacity << 1;
248 } else {
249 if (capacity == MAX_BUFFER_SIZE)
250 throw new OutOfMemoryError("Required array size too large");
251 capacity = MAX_BUFFER_SIZE;
252 }
253 buf = Arrays.copyOf(buf, capacity);
254 }
255 return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
256 }
257
258 /**
259 * Reads the requested number of bytes from the input stream into the given
260 * byte array. This method blocks until {@code len} bytes of input data have
261 * been read, end of stream is detected, or an exception is thrown. The
262 * number of bytes actually read, possibly zero, is returned. This method
263 * does not close the input stream.
264 *
265 * <p> In the case where end of stream is reached before {@code len} bytes
266 * have been read, then the actual number of bytes read will be returned.
267 * When this stream reaches end of stream, further invocations of this
268 * method will return zero.
269 *
270 * <p> If {@code len} is zero, then no bytes are read and {@code 0} is
271 * returned; otherwise, there is an attempt to read up to {@code len} bytes.
272 *
273 * <p> The first byte read is stored into element {@code b[off]}, the next
274 * one in to {@code b[off+1]}, and so on. The number of bytes read is, at
275 * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually
|
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
23 * questions.
24 */
25
26 package java.io;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Objects;
31
32 /**
33 * This abstract class is the superclass of all classes representing
34 * an input stream of bytes.
35 *
36 * <p> Applications that need to define a subclass of <code>InputStream</code>
37 * must always provide a method that returns the next byte of input.
38 *
39 * @author Arthur van Hoff
40 * @see java.io.BufferedInputStream
41 * @see java.io.ByteArrayInputStream
42 * @see java.io.DataInputStream
43 * @see java.io.FilterInputStream
44 * @see java.io.InputStream#read()
45 * @see java.io.OutputStream
46 * @see java.io.PushbackInputStream
47 * @since 1.0
48 */
213 *
214 * <p> The behavior for the case where the input stream is <i>asynchronously
215 * closed</i>, or the thread interrupted during the read, is highly input
216 * stream specific, and therefore not specified.
217 *
218 * <p> If an I/O error occurs reading from the input stream, then it may do
219 * so after some, but not all, bytes have been read. Consequently the input
220 * stream may not be at end of stream and may be in an inconsistent state.
221 * It is strongly recommended that the stream be promptly closed if an I/O
222 * error occurs.
223 *
224 * @return a byte array containing the bytes read from this input stream
225 * @throws IOException if an I/O error occurs
226 * @throws OutOfMemoryError if an array of the required size cannot be
227 * allocated. For example, if an array larger than {@code 2GB} would
228 * be required to store the bytes.
229 *
230 * @since 9
231 */
232 public byte[] readAllBytes() throws IOException {
233 ArrayList<byte[]> list = new ArrayList<byte[]>(128);
234 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
235 int nread = 0;
236 int total = 0;
237 int n;
238 for (;;) {
239 // read to EOF which may read more or less than buffer size
240 while ((n = read(buf, nread, buf.length - nread)) > 0)
241 nread += n;
242
243 if (nread > 0) {
244 if (MAX_BUFFER_SIZE - total < nread) {
245 throw new OutOfMemoryError("Required array size too large");
246 }
247 total += nread;
248 list.add(Arrays.copyOf(buf, nread));
249 }
250
251 // if the last call to read returned -1, then we're done
252 if (n < 0)
253 break;
254
255 nread = 0;
256 }
257
258 if (list.size() == 1) {
259 return list.get(0);
260 }
261
262 byte[] output = new byte[total];
263 int offset = 0;
264 int numCached = list.size();
265 for (int i = 0; i < numCached; i++) {
266 byte[] b = list.get(i);
267 System.arraycopy(b, 0, output, offset, b.length);
268 offset += b.length;
269 }
270
271 return output;
272 }
273
274 /**
275 * Reads the requested number of bytes from the input stream into the given
276 * byte array. This method blocks until {@code len} bytes of input data have
277 * been read, end of stream is detected, or an exception is thrown. The
278 * number of bytes actually read, possibly zero, is returned. This method
279 * does not close the input stream.
280 *
281 * <p> In the case where end of stream is reached before {@code len} bytes
282 * have been read, then the actual number of bytes read will be returned.
283 * When this stream reaches end of stream, further invocations of this
284 * method will return zero.
285 *
286 * <p> If {@code len} is zero, then no bytes are read and {@code 0} is
287 * returned; otherwise, there is an attempt to read up to {@code len} bytes.
288 *
289 * <p> The first byte read is stored into element {@code b[off]}, the next
290 * one in to {@code b[off+1]}, and so on. The number of bytes read is, at
291 * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually
|