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