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