211 * <p> Note that this method is intended for simple cases where it is
212 * convenient to read all bytes into a byte array. It is not intended for
213 * reading input streams with large amounts of data.
214 *
215 * <p> The behavior for the case where the input stream is <i>asynchronously
216 * closed</i>, or the thread interrupted during the read, is highly input
217 * stream specific, and therefore not specified.
218 *
219 * <p> If an I/O error occurs reading from the input stream, then it may do
220 * so after some, but not all, bytes have been read. Consequently the input
221 * stream may not be at end of stream and may be in an inconsistent state.
222 * It is strongly recommended that the stream be promptly closed if an I/O
223 * error occurs.
224 *
225 * @return a byte array containing the bytes read from this input stream
226 * @throws IOException if an I/O error occurs
227 * @throws OutOfMemoryError if an array of the required size cannot be
228 * allocated. For example, if an array larger than {@code 2GB} would
229 * be required to store the bytes.
230 *
231 * @since 1.9
232 */
233 public byte[] readAllBytes() throws IOException {
234 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
235 int capacity = buf.length;
236 int nread = 0;
237 int n;
238 for (;;) {
239 // read to EOF which may read more or less than initial buffer size
240 while ((n = read(buf, nread, capacity - nread)) > 0)
241 nread += n;
242
243 // if the last call to read returned -1, then we're done
244 if (n < 0)
245 break;
246
247 // need to allocate a larger buffer
248 if (capacity <= MAX_BUFFER_SIZE - capacity) {
249 capacity = capacity << 1;
250 } else {
251 if (capacity == MAX_BUFFER_SIZE)
281 *
282 * <p> The behavior for the case where the input stream is <i>asynchronously
283 * closed</i>, or the thread interrupted during the read, is highly input
284 * stream specific, and therefore not specified.
285 *
286 * <p> If an I/O error occurs reading from the input stream, then it may do
287 * so after some, but not all, bytes of {@code b} have been updated with
288 * data from the input stream. Consequently the input stream and {@code b}
289 * may be in an inconsistent state. It is strongly recommended that the
290 * stream be promptly closed if an I/O error occurs.
291 *
292 * @param b the byte array into which the data is read
293 * @param off the start offset in {@code b} at which the data is written
294 * @param len the maximum number of bytes to read
295 * @return the actual number of bytes read into the buffer
296 * @throws IOException if an I/O error occurs
297 * @throws NullPointerException if {@code b} is {@code null}
298 * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
299 * is negative, or {@code len} is greater than {@code b.length - off}
300 *
301 * @since 1.9
302 */
303 public int readNBytes(byte[] b, int off, int len) throws IOException {
304 Objects.requireNonNull(b);
305 if (off < 0 || len < 0 || len > b.length - off)
306 throw new IndexOutOfBoundsException();
307 int n = 0;
308 while (n < len) {
309 int count = read(b, off + n, len - n);
310 if (count < 0)
311 break;
312 n += count;
313 }
314 return n;
315 }
316
317 /**
318 * Skips over and discards <code>n</code> bytes of data from this input
319 * stream. The <code>skip</code> method may, for a variety of reasons, end
320 * up skipping over some smaller number of bytes, possibly <code>0</code>.
321 * This may result from any of a number of conditions; reaching end of file
497 * input stream will be at end of stream. This method does not close either
498 * stream.
499 * <p>
500 * This method may block indefinitely reading from the input stream, or
501 * writing to the output stream. The behavior for the case where the input
502 * and/or output stream is <i>asynchronously closed</i>, or the thread
503 * interrupted during the transfer, is highly input and output stream
504 * specific, and therefore not specified.
505 * <p>
506 * If an I/O error occurs reading from the input stream or writing to the
507 * output stream, then it may do so after some bytes have been read or
508 * written. Consequently the input stream may not be at end of stream and
509 * one, or both, streams may be in an inconsistent state. It is strongly
510 * recommended that both streams be promptly closed if an I/O error occurs.
511 *
512 * @param out the output stream, non-null
513 * @return the number of bytes transferred
514 * @throws IOException if an I/O error occurs when reading or writing
515 * @throws NullPointerException if {@code out} is {@code null}
516 *
517 * @since 1.9
518 */
519 public long transferTo(OutputStream out) throws IOException {
520 Objects.requireNonNull(out, "out");
521 long transferred = 0;
522 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
523 int read;
524 while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
525 out.write(buffer, 0, read);
526 transferred += read;
527 }
528 return transferred;
529 }
530 }
|
211 * <p> Note that this method is intended for simple cases where it is
212 * convenient to read all bytes into a byte array. It is not intended for
213 * reading input streams with large amounts of data.
214 *
215 * <p> The behavior for the case where the input stream is <i>asynchronously
216 * closed</i>, or the thread interrupted during the read, is highly input
217 * stream specific, and therefore not specified.
218 *
219 * <p> If an I/O error occurs reading from the input stream, then it may do
220 * so after some, but not all, bytes have been read. Consequently the input
221 * stream may not be at end of stream and may be in an inconsistent state.
222 * It is strongly recommended that the stream be promptly closed if an I/O
223 * error occurs.
224 *
225 * @return a byte array containing the bytes read from this input stream
226 * @throws IOException if an I/O error occurs
227 * @throws OutOfMemoryError if an array of the required size cannot be
228 * allocated. For example, if an array larger than {@code 2GB} would
229 * be required to store the bytes.
230 *
231 * @since 9
232 */
233 public byte[] readAllBytes() throws IOException {
234 byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
235 int capacity = buf.length;
236 int nread = 0;
237 int n;
238 for (;;) {
239 // read to EOF which may read more or less than initial buffer size
240 while ((n = read(buf, nread, capacity - nread)) > 0)
241 nread += n;
242
243 // if the last call to read returned -1, then we're done
244 if (n < 0)
245 break;
246
247 // need to allocate a larger buffer
248 if (capacity <= MAX_BUFFER_SIZE - capacity) {
249 capacity = capacity << 1;
250 } else {
251 if (capacity == MAX_BUFFER_SIZE)
281 *
282 * <p> The behavior for the case where the input stream is <i>asynchronously
283 * closed</i>, or the thread interrupted during the read, is highly input
284 * stream specific, and therefore not specified.
285 *
286 * <p> If an I/O error occurs reading from the input stream, then it may do
287 * so after some, but not all, bytes of {@code b} have been updated with
288 * data from the input stream. Consequently the input stream and {@code b}
289 * may be in an inconsistent state. It is strongly recommended that the
290 * stream be promptly closed if an I/O error occurs.
291 *
292 * @param b the byte array into which the data is read
293 * @param off the start offset in {@code b} at which the data is written
294 * @param len the maximum number of bytes to read
295 * @return the actual number of bytes read into the buffer
296 * @throws IOException if an I/O error occurs
297 * @throws NullPointerException if {@code b} is {@code null}
298 * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
299 * is negative, or {@code len} is greater than {@code b.length - off}
300 *
301 * @since 9
302 */
303 public int readNBytes(byte[] b, int off, int len) throws IOException {
304 Objects.requireNonNull(b);
305 if (off < 0 || len < 0 || len > b.length - off)
306 throw new IndexOutOfBoundsException();
307 int n = 0;
308 while (n < len) {
309 int count = read(b, off + n, len - n);
310 if (count < 0)
311 break;
312 n += count;
313 }
314 return n;
315 }
316
317 /**
318 * Skips over and discards <code>n</code> bytes of data from this input
319 * stream. The <code>skip</code> method may, for a variety of reasons, end
320 * up skipping over some smaller number of bytes, possibly <code>0</code>.
321 * This may result from any of a number of conditions; reaching end of file
497 * input stream will be at end of stream. This method does not close either
498 * stream.
499 * <p>
500 * This method may block indefinitely reading from the input stream, or
501 * writing to the output stream. The behavior for the case where the input
502 * and/or output stream is <i>asynchronously closed</i>, or the thread
503 * interrupted during the transfer, is highly input and output stream
504 * specific, and therefore not specified.
505 * <p>
506 * If an I/O error occurs reading from the input stream or writing to the
507 * output stream, then it may do so after some bytes have been read or
508 * written. Consequently the input stream may not be at end of stream and
509 * one, or both, streams may be in an inconsistent state. It is strongly
510 * recommended that both streams be promptly closed if an I/O error occurs.
511 *
512 * @param out the output stream, non-null
513 * @return the number of bytes transferred
514 * @throws IOException if an I/O error occurs when reading or writing
515 * @throws NullPointerException if {@code out} is {@code null}
516 *
517 * @since 9
518 */
519 public long transferTo(OutputStream out) throws IOException {
520 Objects.requireNonNull(out, "out");
521 long transferred = 0;
522 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
523 int read;
524 while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
525 out.write(buffer, 0, read);
526 transferred += read;
527 }
528 return transferred;
529 }
530 }
|