1 /* 2 * Copyright (c) 1994, 2018, 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 23 * questions. 24 */ 25 26 package java.io; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.List; 31 import java.util.Objects; 32 33 /** 34 * This abstract class is the superclass of all classes representing 35 * an input stream of bytes. 36 * 37 * <p> Applications that need to define a subclass of <code>InputStream</code> 38 * must always provide a method that returns the next byte of input. 39 * 40 * @author Arthur van Hoff 41 * @see java.io.BufferedInputStream 42 * @see java.io.ByteArrayInputStream 43 * @see java.io.DataInputStream 44 * @see java.io.FilterInputStream 45 * @see java.io.InputStream#read() 46 * @see java.io.OutputStream 47 * @see java.io.PushbackInputStream 48 * @since 1.0 49 */ 50 public abstract class InputStream implements Closeable { 51 52 // MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to 53 // use when skipping. 54 private static final int MAX_SKIP_BUFFER_SIZE = 2048; 55 56 private static final int DEFAULT_BUFFER_SIZE = 8192; 57 58 /** 59 * Returns a new {@code InputStream} that reads no bytes. The returned 60 * stream is initially open. The stream is closed by calling the 61 * {@code close()} method. Subsequent calls to {@code close()} have no 62 * effect. 63 * 64 * <p> While the stream is open, the {@code available()}, {@code read()}, 65 * {@code read(byte[])}, {@code read(byte[], int, int)}, 66 * {@code readAllBytes()}, {@code readNBytes(byte[], int, int)}, 67 * {@code readNBytes(int)}, {@code skip(long)}, and 68 * {@code transferTo()} methods all behave as if end of stream has been 69 * reached. After the stream has been closed, these methods all throw 70 * {@code IOException}. 71 * 72 * <p> The {@code markSupported()} method returns {@code false}. The 73 * {@code mark()} method does nothing, and the {@code reset()} method 74 * throws {@code IOException}. 75 * 76 * @return an {@code InputStream} which contains no bytes 77 * 78 * @since 11 79 */ 80 public static InputStream nullInputStream() { 81 return new InputStream() { 82 private volatile boolean closed; 83 84 private void ensureOpen() throws IOException { 85 if (closed) { 86 throw new IOException("Stream closed"); 87 } 88 } 89 90 @Override 91 public int available () throws IOException { 92 ensureOpen(); 93 return 0; 94 } 95 96 @Override 97 public int read() throws IOException { 98 ensureOpen(); 99 return -1; 100 } 101 102 @Override 103 public int read(byte[] b, int off, int len) throws IOException { 104 Objects.checkFromIndexSize(off, len, b.length); 105 if (len == 0) { 106 return 0; 107 } 108 ensureOpen(); 109 return -1; 110 } 111 112 @Override 113 public byte[] readAllBytes() throws IOException { 114 ensureOpen(); 115 return new byte[0]; 116 } 117 118 @Override 119 public int readNBytes(byte[] b, int off, int len) 120 throws IOException { 121 Objects.checkFromIndexSize(off, len, b.length); 122 ensureOpen(); 123 return 0; 124 } 125 126 @Override 127 public byte[] readNBytes(int len) throws IOException { 128 if (len < 0) { 129 throw new IllegalArgumentException("len < 0"); 130 } 131 ensureOpen(); 132 return new byte[0]; 133 } 134 135 @Override 136 public long skip(long n) throws IOException { 137 ensureOpen(); 138 return 0L; 139 } 140 141 @Override 142 public long transferTo(OutputStream out) throws IOException { 143 Objects.requireNonNull(out); 144 ensureOpen(); 145 return 0L; 146 } 147 148 @Override 149 public void close() throws IOException { 150 closed = true; 151 } 152 }; 153 } 154 155 /** 156 * Reads the next byte of data from the input stream. The value byte is 157 * returned as an <code>int</code> in the range <code>0</code> to 158 * <code>255</code>. If no byte is available because the end of the stream 159 * has been reached, the value <code>-1</code> is returned. This method 160 * blocks until input data is available, the end of the stream is detected, 161 * or an exception is thrown. 162 * 163 * <p> A subclass must provide an implementation of this method. 164 * 165 * @return the next byte of data, or <code>-1</code> if the end of the 166 * stream is reached. 167 * @exception IOException if an I/O error occurs. 168 */ 169 public abstract int read() throws IOException; 170 171 /** 172 * Reads some number of bytes from the input stream and stores them into 173 * the buffer array <code>b</code>. The number of bytes actually read is 174 * returned as an integer. This method blocks until input data is 175 * available, end of file is detected, or an exception is thrown. 176 * 177 * <p> If the length of <code>b</code> is zero, then no bytes are read and 178 * <code>0</code> is returned; otherwise, there is an attempt to read at 179 * least one byte. If no byte is available because the stream is at the 180 * end of the file, the value <code>-1</code> is returned; otherwise, at 181 * least one byte is read and stored into <code>b</code>. 182 * 183 * <p> The first byte read is stored into element <code>b[0]</code>, the 184 * next one into <code>b[1]</code>, and so on. The number of bytes read is, 185 * at most, equal to the length of <code>b</code>. Let <i>k</i> be the 186 * number of bytes actually read; these bytes will be stored in elements 187 * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>, 188 * leaving elements <code>b[</code><i>k</i><code>]</code> through 189 * <code>b[b.length-1]</code> unaffected. 190 * 191 * <p> The <code>read(b)</code> method for class <code>InputStream</code> 192 * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre> 193 * 194 * @param b the buffer into which the data is read. 195 * @return the total number of bytes read into the buffer, or 196 * <code>-1</code> if there is no more data because the end of 197 * the stream has been reached. 198 * @exception IOException If the first byte cannot be read for any reason 199 * other than the end of the file, if the input stream has been closed, or 200 * if some other I/O error occurs. 201 * @exception NullPointerException if <code>b</code> is <code>null</code>. 202 * @see java.io.InputStream#read(byte[], int, int) 203 */ 204 public int read(byte b[]) throws IOException { 205 return read(b, 0, b.length); 206 } 207 208 /** 209 * Reads up to <code>len</code> bytes of data from the input stream into 210 * an array of bytes. An attempt is made to read as many as 211 * <code>len</code> bytes, but a smaller number may be read. 212 * The number of bytes actually read is returned as an integer. 213 * 214 * <p> This method blocks until input data is available, end of file is 215 * detected, or an exception is thrown. 216 * 217 * <p> If <code>len</code> is zero, then no bytes are read and 218 * <code>0</code> is returned; otherwise, there is an attempt to read at 219 * least one byte. If no byte is available because the stream is at end of 220 * file, the value <code>-1</code> is returned; otherwise, at least one 221 * byte is read and stored into <code>b</code>. 222 * 223 * <p> The first byte read is stored into element <code>b[off]</code>, the 224 * next one into <code>b[off+1]</code>, and so on. The number of bytes read 225 * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of 226 * bytes actually read; these bytes will be stored in elements 227 * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>, 228 * leaving elements <code>b[off+</code><i>k</i><code>]</code> through 229 * <code>b[off+len-1]</code> unaffected. 230 * 231 * <p> In every case, elements <code>b[0]</code> through 232 * <code>b[off]</code> and elements <code>b[off+len]</code> through 233 * <code>b[b.length-1]</code> are unaffected. 234 * 235 * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method 236 * for class <code>InputStream</code> simply calls the method 237 * <code>read()</code> repeatedly. If the first such call results in an 238 * <code>IOException</code>, that exception is returned from the call to 239 * the <code>read(b,</code> <code>off,</code> <code>len)</code> method. If 240 * any subsequent call to <code>read()</code> results in a 241 * <code>IOException</code>, the exception is caught and treated as if it 242 * were end of file; the bytes read up to that point are stored into 243 * <code>b</code> and the number of bytes read before the exception 244 * occurred is returned. The default implementation of this method blocks 245 * until the requested amount of input data <code>len</code> has been read, 246 * end of file is detected, or an exception is thrown. Subclasses are 247 * encouraged to provide a more efficient implementation of this method. 248 * 249 * @param b the buffer into which the data is read. 250 * @param off the start offset in array <code>b</code> 251 * at which the data is written. 252 * @param len the maximum number of bytes to read. 253 * @return the total number of bytes read into the buffer, or 254 * <code>-1</code> if there is no more data because the end of 255 * the stream has been reached. 256 * @exception IOException If the first byte cannot be read for any reason 257 * other than end of file, or if the input stream has been closed, or if 258 * some other I/O error occurs. 259 * @exception NullPointerException If <code>b</code> is <code>null</code>. 260 * @exception IndexOutOfBoundsException If <code>off</code> is negative, 261 * <code>len</code> is negative, or <code>len</code> is greater than 262 * <code>b.length - off</code> 263 * @see java.io.InputStream#read() 264 */ 265 public int read(byte b[], int off, int len) throws IOException { 266 Objects.checkFromIndexSize(off, len, b.length); 267 if (len == 0) { 268 return 0; 269 } 270 271 int c = read(); 272 if (c == -1) { 273 return -1; 274 } 275 b[off] = (byte)c; 276 277 int i = 1; 278 try { 279 for (; i < len ; i++) { 280 c = read(); 281 if (c == -1) { 282 break; 283 } 284 b[off + i] = (byte)c; 285 } 286 } catch (IOException ee) { 287 } 288 return i; 289 } 290 291 /** 292 * The maximum size of array to allocate. 293 * Some VMs reserve some header words in an array. 294 * Attempts to allocate larger arrays may result in 295 * OutOfMemoryError: Requested array size exceeds VM limit 296 */ 297 private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8; 298 299 /** 300 * Reads all remaining bytes from the input stream. This method blocks until 301 * all remaining bytes have been read and end of stream is detected, or an 302 * exception is thrown. This method does not close the input stream. 303 * 304 * <p> When this stream reaches end of stream, further invocations of this 305 * method will return an empty byte array. 306 * 307 * <p> Note that this method is intended for simple cases where it is 308 * convenient to read all bytes into a byte array. It is not intended for 309 * reading input streams with large amounts of data. 310 * 311 * <p> The behavior for the case where the input stream is <i>asynchronously 312 * closed</i>, or the thread interrupted during the read, is highly input 313 * stream specific, and therefore not specified. 314 * 315 * <p> If an I/O error occurs reading from the input stream, then it may do 316 * so after some, but not all, bytes have been read. Consequently the input 317 * stream may not be at end of stream and may be in an inconsistent state. 318 * It is strongly recommended that the stream be promptly closed if an I/O 319 * error occurs. 320 * 321 * @implSpec 322 * This method invokes {@link #readNBytes(int)} with a length of 323 * {@link Integer#MAX_VALUE}. 324 * 325 * @return a byte array containing the bytes read from this input stream 326 * @throws IOException if an I/O error occurs 327 * @throws OutOfMemoryError if an array of the required size cannot be 328 * allocated. 329 * 330 * @since 9 331 */ 332 public byte[] readAllBytes() throws IOException { 333 return readNBytes(Integer.MAX_VALUE); 334 } 335 336 /** 337 * Reads up to a specified number of bytes from the input stream. This 338 * method blocks until the requested number of bytes have been read, end 339 * of stream is detected, or an exception is thrown. This method does not 340 * close the input stream. 341 * 342 * <p> The length of the returned array equals the number of bytes read 343 * from the stream. If {@code len} is zero, then no bytes are read and 344 * an empty byte array is returned. Otherwise, up to {@code len} bytes 345 * are read from the stream. Fewer than {@code len} bytes may be read if 346 * end of stream is encountered. 347 * 348 * <p> When this stream reaches end of stream, further invocations of this 349 * method will return an empty byte array. 350 * 351 * <p> Note that this method is intended for simple cases where it is 352 * convenient to read the specified number of bytes into a byte array. The 353 * total amount of memory allocated by this method is proportional to the 354 * number of bytes read from the stream which is bounded by {@code len}. 355 * Therefore, the method may be safely called with very large values of 356 * {@code len} provided sufficient memory is available. 357 * 358 * <p> The behavior for the case where the input stream is <i>asynchronously 359 * closed</i>, or the thread interrupted during the read, is highly input 360 * stream specific, and therefore not specified. 361 * 362 * <p> If an I/O error occurs reading from the input stream, then it may do 363 * so after some, but not all, bytes have been read. Consequently the input 364 * stream may not be at end of stream and may be in an inconsistent state. 365 * It is strongly recommended that the stream be promptly closed if an I/O 366 * error occurs. 367 * 368 * @param len the maximum number of bytes to read 369 * @return a byte array containing the bytes read from this input stream 370 * @throws IllegalArgumentException if {@code length} is negative 371 * @throws IOException if an I/O error occurs 372 * @throws OutOfMemoryError if an array of the required size cannot be 373 * allocated. 374 * 375 * @since 11 376 */ 377 public byte[] readNBytes(int len) throws IOException { 378 if (len < 0) { 379 throw new IllegalArgumentException("len < 0"); 380 } 381 382 List<byte[]> bufs = null; 383 byte[] result = null; 384 int total = 0; 385 int remaining = len; 386 int n; 387 do { 388 byte[] buf = new byte[Math.min(len, DEFAULT_BUFFER_SIZE)]; 389 int nread = 0; 390 391 // read to EOF which may read more or less than buffer size 392 while ((n = read(buf, nread, 393 Math.min(buf.length - nread, remaining))) > 0) { 394 nread += n; 395 remaining -= n; 396 } 397 398 if (nread > 0) { 399 if (MAX_BUFFER_SIZE - total < nread) { 400 throw new OutOfMemoryError("Required array size too large"); 401 } 402 total += nread; 403 if (result == null) { 404 result = buf; 405 } else { 406 if (bufs == null) { 407 bufs = new ArrayList<>(); 408 bufs.add(result); 409 } 410 bufs.add(buf); 411 } 412 } 413 // if the last call to read returned -1 or the number of bytes 414 // requested have been read then break 415 } while (n >= 0 && remaining > 0); 416 417 if (bufs == null) { 418 if (result == null) { 419 return new byte[0]; 420 } 421 return result.length == total ? 422 result : Arrays.copyOf(result, total); 423 } 424 425 result = new byte[total]; 426 int offset = 0; 427 remaining = total; 428 for (byte[] b : bufs) { 429 int count = Math.min(b.length, remaining); 430 System.arraycopy(b, 0, result, offset, count); 431 offset += count; 432 remaining -= count; 433 } 434 435 return result; 436 } 437 438 /** 439 * Reads the requested number of bytes from the input stream into the given 440 * byte array. This method blocks until {@code len} bytes of input data have 441 * been read, end of stream is detected, or an exception is thrown. The 442 * number of bytes actually read, possibly zero, is returned. This method 443 * does not close the input stream. 444 * 445 * <p> In the case where end of stream is reached before {@code len} bytes 446 * have been read, then the actual number of bytes read will be returned. 447 * When this stream reaches end of stream, further invocations of this 448 * method will return zero. 449 * 450 * <p> If {@code len} is zero, then no bytes are read and {@code 0} is 451 * returned; otherwise, there is an attempt to read up to {@code len} bytes. 452 * 453 * <p> The first byte read is stored into element {@code b[off]}, the next 454 * one in to {@code b[off+1]}, and so on. The number of bytes read is, at 455 * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually 456 * read; these bytes will be stored in elements {@code b[off]} through 457 * {@code b[off+}<i>k</i>{@code -1]}, leaving elements {@code b[off+}<i>k</i> 458 * {@code ]} through {@code b[off+len-1]} unaffected. 459 * 460 * <p> The behavior for the case where the input stream is <i>asynchronously 461 * closed</i>, or the thread interrupted during the read, is highly input 462 * stream specific, and therefore not specified. 463 * 464 * <p> If an I/O error occurs reading from the input stream, then it may do 465 * so after some, but not all, bytes of {@code b} have been updated with 466 * data from the input stream. Consequently the input stream and {@code b} 467 * may be in an inconsistent state. It is strongly recommended that the 468 * stream be promptly closed if an I/O error occurs. 469 * 470 * @param b the byte array into which the data is read 471 * @param off the start offset in {@code b} at which the data is written 472 * @param len the maximum number of bytes to read 473 * @return the actual number of bytes read into the buffer 474 * @throws IOException if an I/O error occurs 475 * @throws NullPointerException if {@code b} is {@code null} 476 * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len} 477 * is negative, or {@code len} is greater than {@code b.length - off} 478 * 479 * @since 9 480 */ 481 public int readNBytes(byte[] b, int off, int len) throws IOException { 482 Objects.checkFromIndexSize(off, len, b.length); 483 484 int n = 0; 485 while (n < len) { 486 int count = read(b, off + n, len - n); 487 if (count < 0) 488 break; 489 n += count; 490 } 491 return n; 492 } 493 494 /** 495 * Skips over and discards <code>n</code> bytes of data from this input 496 * stream. The <code>skip</code> method may, for a variety of reasons, end 497 * up skipping over some smaller number of bytes, possibly <code>0</code>. 498 * This may result from any of a number of conditions; reaching end of file 499 * before <code>n</code> bytes have been skipped is only one possibility. 500 * The actual number of bytes skipped is returned. If {@code n} is 501 * negative, the {@code skip} method for class {@code InputStream} always 502 * returns 0, and no bytes are skipped. Subclasses may handle the negative 503 * value differently. 504 * 505 * <p> The <code>skip</code> method implementation of this class creates a 506 * byte array and then repeatedly reads into it until <code>n</code> bytes 507 * have been read or the end of the stream has been reached. Subclasses are 508 * encouraged to provide a more efficient implementation of this method. 509 * For instance, the implementation may depend on the ability to seek. 510 * 511 * @param n the number of bytes to be skipped. 512 * @return the actual number of bytes skipped. 513 * @throws IOException if an I/O error occurs. 514 */ 515 public long skip(long n) throws IOException { 516 517 long remaining = n; 518 int nr; 519 520 if (n <= 0) { 521 return 0; 522 } 523 524 int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining); 525 byte[] skipBuffer = new byte[size]; 526 while (remaining > 0) { 527 nr = read(skipBuffer, 0, (int)Math.min(size, remaining)); 528 if (nr < 0) { 529 break; 530 } 531 remaining -= nr; 532 } 533 534 return n - remaining; 535 } 536 537 /** 538 * Returns an estimate of the number of bytes that can be read (or 539 * skipped over) from this input stream without blocking by the next 540 * invocation of a method for this input stream. The next invocation 541 * might be the same thread or another thread. A single read or skip of this 542 * many bytes will not block, but may read or skip fewer bytes. 543 * 544 * <p> Note that while some implementations of {@code InputStream} will return 545 * the total number of bytes in the stream, many will not. It is 546 * never correct to use the return value of this method to allocate 547 * a buffer intended to hold all data in this stream. 548 * 549 * <p> A subclass' implementation of this method may choose to throw an 550 * {@link IOException} if this input stream has been closed by 551 * invoking the {@link #close()} method. 552 * 553 * <p> The {@code available} method for class {@code InputStream} always 554 * returns {@code 0}. 555 * 556 * <p> This method should be overridden by subclasses. 557 * 558 * @return an estimate of the number of bytes that can be read (or skipped 559 * over) from this input stream without blocking or {@code 0} when 560 * it reaches the end of the input stream. 561 * @exception IOException if an I/O error occurs. 562 */ 563 public int available() throws IOException { 564 return 0; 565 } 566 567 /** 568 * Closes this input stream and releases any system resources associated 569 * with the stream. 570 * 571 * <p> The <code>close</code> method of <code>InputStream</code> does 572 * nothing. 573 * 574 * @exception IOException if an I/O error occurs. 575 */ 576 public void close() throws IOException {} 577 578 /** 579 * Marks the current position in this input stream. A subsequent call to 580 * the <code>reset</code> method repositions this stream at the last marked 581 * position so that subsequent reads re-read the same bytes. 582 * 583 * <p> The <code>readlimit</code> arguments tells this input stream to 584 * allow that many bytes to be read before the mark position gets 585 * invalidated. 586 * 587 * <p> The general contract of <code>mark</code> is that, if the method 588 * <code>markSupported</code> returns <code>true</code>, the stream somehow 589 * remembers all the bytes read after the call to <code>mark</code> and 590 * stands ready to supply those same bytes again if and whenever the method 591 * <code>reset</code> is called. However, the stream is not required to 592 * remember any data at all if more than <code>readlimit</code> bytes are 593 * read from the stream before <code>reset</code> is called. 594 * 595 * <p> Marking a closed stream should not have any effect on the stream. 596 * 597 * <p> The <code>mark</code> method of <code>InputStream</code> does 598 * nothing. 599 * 600 * @param readlimit the maximum limit of bytes that can be read before 601 * the mark position becomes invalid. 602 * @see java.io.InputStream#reset() 603 */ 604 public synchronized void mark(int readlimit) {} 605 606 /** 607 * Repositions this stream to the position at the time the 608 * <code>mark</code> method was last called on this input stream. 609 * 610 * <p> The general contract of <code>reset</code> is: 611 * 612 * <ul> 613 * <li> If the method <code>markSupported</code> returns 614 * <code>true</code>, then: 615 * 616 * <ul><li> If the method <code>mark</code> has not been called since 617 * the stream was created, or the number of bytes read from the stream 618 * since <code>mark</code> was last called is larger than the argument 619 * to <code>mark</code> at that last call, then an 620 * <code>IOException</code> might be thrown. 621 * 622 * <li> If such an <code>IOException</code> is not thrown, then the 623 * stream is reset to a state such that all the bytes read since the 624 * most recent call to <code>mark</code> (or since the start of the 625 * file, if <code>mark</code> has not been called) will be resupplied 626 * to subsequent callers of the <code>read</code> method, followed by 627 * any bytes that otherwise would have been the next input data as of 628 * the time of the call to <code>reset</code>. </ul> 629 * 630 * <li> If the method <code>markSupported</code> returns 631 * <code>false</code>, then: 632 * 633 * <ul><li> The call to <code>reset</code> may throw an 634 * <code>IOException</code>. 635 * 636 * <li> If an <code>IOException</code> is not thrown, then the stream 637 * is reset to a fixed state that depends on the particular type of the 638 * input stream and how it was created. The bytes that will be supplied 639 * to subsequent callers of the <code>read</code> method depend on the 640 * particular type of the input stream. </ul></ul> 641 * 642 * <p>The method <code>reset</code> for class <code>InputStream</code> 643 * does nothing except throw an <code>IOException</code>. 644 * 645 * @exception IOException if this stream has not been marked or if the 646 * mark has been invalidated. 647 * @see java.io.InputStream#mark(int) 648 * @see java.io.IOException 649 */ 650 public synchronized void reset() throws IOException { 651 throw new IOException("mark/reset not supported"); 652 } 653 654 /** 655 * Tests if this input stream supports the <code>mark</code> and 656 * <code>reset</code> methods. Whether or not <code>mark</code> and 657 * <code>reset</code> are supported is an invariant property of a 658 * particular input stream instance. The <code>markSupported</code> method 659 * of <code>InputStream</code> returns <code>false</code>. 660 * 661 * @return <code>true</code> if this stream instance supports the mark 662 * and reset methods; <code>false</code> otherwise. 663 * @see java.io.InputStream#mark(int) 664 * @see java.io.InputStream#reset() 665 */ 666 public boolean markSupported() { 667 return false; 668 } 669 670 /** 671 * Reads all bytes from this input stream and writes the bytes to the 672 * given output stream in the order that they are read. On return, this 673 * input stream will be at end of stream. This method does not close either 674 * stream. 675 * <p> 676 * This method may block indefinitely reading from the input stream, or 677 * writing to the output stream. The behavior for the case where the input 678 * and/or output stream is <i>asynchronously closed</i>, or the thread 679 * interrupted during the transfer, is highly input and output stream 680 * specific, and therefore not specified. 681 * <p> 682 * If an I/O error occurs reading from the input stream or writing to the 683 * output stream, then it may do so after some bytes have been read or 684 * written. Consequently the input stream may not be at end of stream and 685 * one, or both, streams may be in an inconsistent state. It is strongly 686 * recommended that both streams be promptly closed if an I/O error occurs. 687 * 688 * @param out the output stream, non-null 689 * @return the number of bytes transferred 690 * @throws IOException if an I/O error occurs when reading or writing 691 * @throws NullPointerException if {@code out} is {@code null} 692 * 693 * @since 9 694 */ 695 public long transferTo(OutputStream out) throws IOException { 696 Objects.requireNonNull(out, "out"); 697 long transferred = 0; 698 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 699 int read; 700 while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) { 701 out.write(buffer, 0, read); 702 transferred += read; 703 } 704 return transferred; 705 } 706 }