1 /*
   2  * Copyright (c) 1994, 2019, 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      * Constructor for subclasses to call.
  60      */
  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      */
  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</code> in the range <code>0</code> to
 171      * <code>255</code>. If no byte is available because the end of the stream
 172      * has been reached, the value <code>-1</code> 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</code> if the end of the
 179      *             stream is reached.
 180      * @exception  IOException  if an I/O error occurs.
 181      */
 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</code>. 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</code> is zero, then no bytes are read and
 191      * <code>0</code> 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</code> is returned; otherwise, at
 194      * least one byte is read and stored into <code>b</code>.
 195      *
 196      * <p> The first byte read is stored into element <code>b[0]</code>, the
 197      * next one into <code>b[1]</code>, and so on. The number of bytes read is,
 198      * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
 199      * number of bytes actually read; these bytes will be stored in elements
 200      * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
 201      * leaving elements <code>b[</code><i>k</i><code>]</code> through
 202      * <code>b[b.length-1]</code> unaffected.
 203      *
 204      * <p> The <code>read(b)</code> method for class <code>InputStream</code>
 205      * has the same effect as: <pre><code> read(b, 0, b.length) </code></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</code> if there is no more data because the end of
 210      *             the stream has been reached.
 211      * @exception  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 closed, or
 213      * if some other I/O error occurs.
 214      * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
 215      * @see        java.io.InputStream#read(byte[], int, int)
 216      */
 217     public int read(byte b[]) throws IOException {
 218         return read(b, 0, b.length);
 219     }
 220 
 221     /**
 222      * Reads up to <code>len</code> 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</code> 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</code> is zero, then no bytes are read and
 231      * <code>0</code> 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</code> is returned; otherwise, at least one
 234      * byte is read and stored into <code>b</code>.
 235      *
 236      * <p> The first byte read is stored into element <code>b[off]</code>, the
 237      * next one into <code>b[off+1]</code>, and so on. The number of bytes read
 238      * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
 239      * bytes actually read; these bytes will be stored in elements
 240      * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
 241      * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
 242      * <code>b[off+len-1]</code> unaffected.
 243      *
 244      * <p> In every case, elements <code>b[0]</code> through
 245      * <code>b[off-1]</code> and elements <code>b[off+len]</code> through
 246      * <code>b[b.length-1]</code> are unaffected.
 247      *
 248      * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
 249      * for class <code>InputStream</code> simply calls the method
 250      * <code>read()</code> repeatedly. If the first such call results in an
 251      * <code>IOException</code>, that exception is returned from the call to
 252      * the <code>read(b,</code> <code>off,</code> <code>len)</code> method.  If
 253      * any subsequent call to <code>read()</code> results in a
 254      * <code>IOException</code>, 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</code> 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</code> 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</code>
 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</code> if there is no more data because the end of
 268      *             the stream has been reached.
 269      * @exception  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, or if
 271      * some other I/O error occurs.
 272      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 273      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 274      * <code>len</code> is negative, or <code>len</code> is greater than
 275      * <code>b.length - off</code>
 276      * @see        java.io.InputStream#read()
 277      */
 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      */
 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 have 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      */
 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                 total += nread;
 420                 if (result == null) {
 421                     result = buf;
 422                 } else {
 423                     if (bufs == null) {
 424                         bufs = new ArrayList<>();
 425                         bufs.add(result);
 426                     }
 427                     bufs.add(buf);
 428                 }
 429             }
 430             // if the last call to read returned -1 or the number of bytes
 431             // requested have been read then break
 432         } while (n >= 0 && remaining > 0);
 433 
 434         if (bufs == null) {
 435             if (result == null) {
 436                 return new byte[0];
 437             }
 438             return result.length == total ?
 439                 result : Arrays.copyOf(result, total);
 440         }
 441 
 442         result = new byte[total];
 443         int offset = 0;
 444         remaining = total;
 445         for (byte[] b : bufs) {
 446             int count = Math.min(b.length, remaining);
 447             System.arraycopy(b, 0, result, offset, count);
 448             offset += count;
 449             remaining -= count;
 450         }
 451 
 452         return result;
 453     }
 454 
 455     /**
 456      * Reads the requested number of bytes from the input stream into the given
 457      * byte array. This method blocks until {@code len} bytes of input data have
 458      * been read, end of stream is detected, or an exception is thrown. The
 459      * number of bytes actually read, possibly zero, is returned. This method
 460      * does not close the input stream.
 461      *
 462      * <p> In the case where end of stream is reached before {@code len} bytes
 463      * have been read, then the actual number of bytes read will be returned.
 464      * When this stream reaches end of stream, further invocations of this
 465      * method will return zero.
 466      *
 467      * <p> If {@code len} is zero, then no bytes are read and {@code 0} is
 468      * returned; otherwise, there is an attempt to read up to {@code len} bytes.
 469      *
 470      * <p> The first byte read is stored into element {@code b[off]}, the next
 471      * one in to {@code b[off+1]}, and so on. The number of bytes read is, at
 472      * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually
 473      * read; these bytes will be stored in elements {@code b[off]} through
 474      * {@code b[off+}<i>k</i>{@code -1]}, leaving elements {@code b[off+}<i>k</i>
 475      * {@code ]} through {@code b[off+len-1]} unaffected.
 476      *
 477      * <p> The behavior for the case where the input stream is <i>asynchronously
 478      * closed</i>, or the thread interrupted during the read, is highly input
 479      * stream specific, and therefore not specified.
 480      *
 481      * <p> If an I/O error occurs reading from the input stream, then it may do
 482      * so after some, but not all, bytes of {@code b} have been updated with
 483      * data from the input stream. Consequently the input stream and {@code b}
 484      * may be in an inconsistent state. It is strongly recommended that the
 485      * stream be promptly closed if an I/O error occurs.
 486      *
 487      * @param  b the byte array into which the data is read
 488      * @param  off the start offset in {@code b} at which the data is written
 489      * @param  len the maximum number of bytes to read
 490      * @return the actual number of bytes read into the buffer
 491      * @throws IOException if an I/O error occurs
 492      * @throws NullPointerException if {@code b} is {@code null}
 493      * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
 494      *         is negative, or {@code len} is greater than {@code b.length - off}
 495      *
 496      * @since 9
 497      */
 498     public int readNBytes(byte[] b, int off, int len) throws IOException {
 499         Objects.checkFromIndexSize(off, len, b.length);
 500 
 501         int n = 0;
 502         while (n < len) {
 503             int count = read(b, off + n, len - n);
 504             if (count < 0)
 505                 break;
 506             n += count;
 507         }
 508         return n;
 509     }
 510 
 511     /**
 512      * Skips over and discards <code>n</code> bytes of data from this input
 513      * stream. The <code>skip</code> method may, for a variety of reasons, end
 514      * up skipping over some smaller number of bytes, possibly <code>0</code>.
 515      * This may result from any of a number of conditions; reaching end of file
 516      * before <code>n</code> bytes have been skipped is only one possibility.
 517      * The actual number of bytes skipped is returned. If {@code n} is
 518      * negative, the {@code skip} method for class {@code InputStream} always
 519      * returns 0, and no bytes are skipped. Subclasses may handle the negative
 520      * value differently.
 521      *
 522      * <p> The <code>skip</code> method implementation of this class creates a
 523      * byte array and then repeatedly reads into it until <code>n</code> bytes
 524      * have been read or the end of the stream has been reached. Subclasses are
 525      * encouraged to provide a more efficient implementation of this method.
 526      * For instance, the implementation may depend on the ability to seek.
 527      *
 528      * @param      n   the number of bytes to be skipped.
 529      * @return     the actual number of bytes skipped which might be zero.
 530      * @throws     IOException  if an I/O error occurs.
 531      * @see        java.io.InputStream#skipNBytes(long)
 532      */
 533     public long skip(long n) throws IOException {
 534         long remaining = n;
 535         int nr;
 536 
 537         if (n <= 0) {
 538             return 0;
 539         }
 540 
 541         int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
 542         byte[] skipBuffer = new byte[size];
 543         while (remaining > 0) {
 544             nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
 545             if (nr < 0) {
 546                 break;
 547             }
 548             remaining -= nr;
 549         }
 550 
 551         return n - remaining;
 552     }
 553 
 554     /**
 555      * Skips over and discards exactly {@code n} bytes of data from this input
 556      * stream.  If {@code n} is zero, then no bytes are skipped.
 557      * If {@code n} is negative, then no bytes are skipped.
 558      * Subclasses may handle the negative value differently.
 559      *
 560      * <p> This method blocks until the requested number of bytes have been
 561      * skipped, end of file is reached, or an exception is thrown.
 562      *
 563      * <p> If end of stream is reached before the stream is at the desired
 564      * position, then an {@code EOFException} is thrown.
 565      *
 566      * <p> If an I/O error occurs, then the input stream may be
 567      * in an inconsistent state. It is strongly recommended that the
 568      * stream be promptly closed if an I/O error occurs.
 569      *
 570      * @implNote
 571      * Subclasses are encouraged to provide a more efficient implementation
 572      * of this method.
 573      *
 574      * @implSpec
 575      * If {@code n} is zero or negative, then no bytes are skipped.
 576      * If {@code n} is positive, the default implementation of this method
 577      * invokes {@link #skip(long) skip()} with parameter {@code n}.  If the
 578      * return value of {@code skip(n)} is non-negative and less than {@code n},
 579      * then {@link #read()} is invoked repeatedly until the stream is {@code n}
 580      * bytes beyond its position when this method was invoked or end of stream
 581      * is reached.  If the return value of {@code skip(n)} is negative or
 582      * greater than {@code n}, then an {@code IOException} is thrown.  Any
 583      * exception thrown by {@code skip()} or {@code read()} will be propagated.
 584      *
 585      * @param      n   the number of bytes to be skipped.
 586      * @throws     EOFException if end of stream is encountered before the
 587      *             stream can be positioned {@code n} bytes beyond its position
 588      *             when this method was invoked.
 589      * @throws     IOException  if the stream cannot be positioned properly or
 590      *             if an I/O error occurs.
 591      * @see        java.io.InputStream#skip(long)
 592      */
 593     public void skipNBytes(long n) throws IOException {
 594         if (n > 0) {
 595             long ns = skip(n);
 596             if (ns >= 0 && ns < n) { // skipped too few bytes
 597                 // adjust number to skip
 598                 n -= ns;
 599                 // read until requested number skipped or EOS reached
 600                 while (n > 0 && read() != -1) {
 601                     n--;
 602                 }
 603                 // if not enough skipped, then EOFE
 604                 if (n != 0) {
 605                     throw new EOFException();
 606                 }
 607             } else if (ns != n) { // skipped negative or too many bytes
 608                 throw new IOException("Unable to skip exactly");
 609             }
 610         }
 611     }
 612 
 613     /**
 614      * Returns an estimate of the number of bytes that can be read (or skipped
 615      * over) from this input stream without blocking, which may be 0, or 0 when
 616      * end of stream is detected.  The read might be on the same thread or
 617      * another thread.  A single read or skip of this many bytes will not block,
 618      * but may read or skip fewer bytes.
 619      *
 620      * <p> Note that while some implementations of {@code InputStream} will
 621      * return the total number of bytes in the stream, many will not.  It is
 622      * never correct to use the return value of this method to allocate
 623      * a buffer intended to hold all data in this stream.
 624      *
 625      * <p> A subclass's implementation of this method may choose to throw an
 626      * {@link IOException} if this input stream has been closed by invoking the
 627      * {@link #close()} method.
 628      *
 629      * <p> The {@code available} method of {@code InputStream} always returns
 630      * {@code 0}.
 631      *
 632      * <p> This method should be overridden by subclasses.
 633      *
 634      * @return     an estimate of the number of bytes that can be read (or
 635      *             skipped over) from this input stream without blocking or
 636      *             {@code 0} when it reaches the end of the input stream.
 637      * @exception  IOException if an I/O error occurs.
 638      */
 639     public int available() throws IOException {
 640         return 0;
 641     }
 642 
 643     /**
 644      * Closes this input stream and releases any system resources associated
 645      * with the stream.
 646      *
 647      * <p> The <code>close</code> method of <code>InputStream</code> does
 648      * nothing.
 649      *
 650      * @exception  IOException  if an I/O error occurs.
 651      */
 652     public void close() throws IOException {}
 653 
 654     /**
 655      * Marks the current position in this input stream. A subsequent call to
 656      * the <code>reset</code> method repositions this stream at the last marked
 657      * position so that subsequent reads re-read the same bytes.
 658      *
 659      * <p> The <code>readlimit</code> arguments tells this input stream to
 660      * allow that many bytes to be read before the mark position gets
 661      * invalidated.
 662      *
 663      * <p> The general contract of <code>mark</code> is that, if the method
 664      * <code>markSupported</code> returns <code>true</code>, the stream somehow
 665      * remembers all the bytes read after the call to <code>mark</code> and
 666      * stands ready to supply those same bytes again if and whenever the method
 667      * <code>reset</code> is called.  However, the stream is not required to
 668      * remember any data at all if more than <code>readlimit</code> bytes are
 669      * read from the stream before <code>reset</code> is called.
 670      *
 671      * <p> Marking a closed stream should not have any effect on the stream.
 672      *
 673      * <p> The <code>mark</code> method of <code>InputStream</code> does
 674      * nothing.
 675      *
 676      * @param   readlimit   the maximum limit of bytes that can be read before
 677      *                      the mark position becomes invalid.
 678      * @see     java.io.InputStream#reset()
 679      */
 680     public synchronized void mark(int readlimit) {}
 681 
 682     /**
 683      * Repositions this stream to the position at the time the
 684      * <code>mark</code> method was last called on this input stream.
 685      *
 686      * <p> The general contract of <code>reset</code> is:
 687      *
 688      * <ul>
 689      * <li> If the method <code>markSupported</code> returns
 690      * <code>true</code>, then:
 691      *
 692      *     <ul><li> If the method <code>mark</code> has not been called since
 693      *     the stream was created, or the number of bytes read from the stream
 694      *     since <code>mark</code> was last called is larger than the argument
 695      *     to <code>mark</code> at that last call, then an
 696      *     <code>IOException</code> might be thrown.
 697      *
 698      *     <li> If such an <code>IOException</code> is not thrown, then the
 699      *     stream is reset to a state such that all the bytes read since the
 700      *     most recent call to <code>mark</code> (or since the start of the
 701      *     file, if <code>mark</code> has not been called) will be resupplied
 702      *     to subsequent callers of the <code>read</code> method, followed by
 703      *     any bytes that otherwise would have been the next input data as of
 704      *     the time of the call to <code>reset</code>. </ul>
 705      *
 706      * <li> If the method <code>markSupported</code> returns
 707      * <code>false</code>, then:
 708      *
 709      *     <ul><li> The call to <code>reset</code> may throw an
 710      *     <code>IOException</code>.
 711      *
 712      *     <li> If an <code>IOException</code> is not thrown, then the stream
 713      *     is reset to a fixed state that depends on the particular type of the
 714      *     input stream and how it was created. The bytes that will be supplied
 715      *     to subsequent callers of the <code>read</code> method depend on the
 716      *     particular type of the input stream. </ul></ul>
 717      *
 718      * <p>The method <code>reset</code> for class <code>InputStream</code>
 719      * does nothing except throw an <code>IOException</code>.
 720      *
 721      * @exception  IOException  if this stream has not been marked or if the
 722      *               mark has been invalidated.
 723      * @see     java.io.InputStream#mark(int)
 724      * @see     java.io.IOException
 725      */
 726     public synchronized void reset() throws IOException {
 727         throw new IOException("mark/reset not supported");
 728     }
 729 
 730     /**
 731      * Tests if this input stream supports the <code>mark</code> and
 732      * <code>reset</code> methods. Whether or not <code>mark</code> and
 733      * <code>reset</code> are supported is an invariant property of a
 734      * particular input stream instance. The <code>markSupported</code> method
 735      * of <code>InputStream</code> returns <code>false</code>.
 736      *
 737      * @return  <code>true</code> if this stream instance supports the mark
 738      *          and reset methods; <code>false</code> otherwise.
 739      * @see     java.io.InputStream#mark(int)
 740      * @see     java.io.InputStream#reset()
 741      */
 742     public boolean markSupported() {
 743         return false;
 744     }
 745 
 746     /**
 747      * Reads all bytes from this input stream and writes the bytes to the
 748      * given output stream in the order that they are read. On return, this
 749      * input stream will be at end of stream. This method does not close either
 750      * stream.
 751      * <p>
 752      * This method may block indefinitely reading from the input stream, or
 753      * writing to the output stream. The behavior for the case where the input
 754      * and/or output stream is <i>asynchronously closed</i>, or the thread
 755      * interrupted during the transfer, is highly input and output stream
 756      * specific, and therefore not specified.
 757      * <p>
 758      * If an I/O error occurs reading from the input stream or writing to the
 759      * output stream, then it may do so after some bytes have been read or
 760      * written. Consequently the input stream may not be at end of stream and
 761      * one, or both, streams may be in an inconsistent state. It is strongly
 762      * recommended that both streams be promptly closed if an I/O error occurs.
 763      *
 764      * @param  out the output stream, non-null
 765      * @return the number of bytes transferred
 766      * @throws IOException if an I/O error occurs when reading or writing
 767      * @throws NullPointerException if {@code out} is {@code null}
 768      *
 769      * @since 9
 770      */
 771     public long transferTo(OutputStream out) throws IOException {
 772         Objects.requireNonNull(out, "out");
 773         long transferred = 0;
 774         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
 775         int read;
 776         while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
 777             out.write(buffer, 0, read);
 778             transferred += read;
 779         }
 780         return transferred;
 781     }
 782 }