< prev index next >

src/java.base/share/classes/java/io/InputStream.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2017, 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


  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      * Reads the next byte of data from the input stream. The value byte is
  60      * returned as an <code>int</code> in the range <code>0</code> to
  61      * <code>255</code>. If no byte is available because the end of the stream
  62      * has been reached, the value <code>-1</code> is returned. This method
  63      * blocks until input data is available, the end of the stream is detected,
  64      * or an exception is thrown.
  65      *
  66      * <p> A subclass must provide an implementation of this method.
  67      *
  68      * @return     the next byte of data, or <code>-1</code> if the end of the
  69      *             stream is reached.
  70      * @exception  IOException  if an I/O error occurs.
  71      */
  72     public abstract int read() throws IOException;
  73 
  74     /**
  75      * Reads some number of bytes from the input stream and stores them into
  76      * the buffer array <code>b</code>. The number of bytes actually read is
  77      * returned as an integer.  This method blocks until input data is
  78      * available, end of file is detected, or an exception is thrown.


 149      * end of file is detected, or an exception is thrown. Subclasses are encouraged
 150      * to provide a more efficient implementation of this method.
 151      *
 152      * @param      b     the buffer into which the data is read.
 153      * @param      off   the start offset in array <code>b</code>
 154      *                   at which the data is written.
 155      * @param      len   the maximum number of bytes to read.
 156      * @return     the total number of bytes read into the buffer, or
 157      *             <code>-1</code> if there is no more data because the end of
 158      *             the stream has been reached.
 159      * @exception  IOException If the first byte cannot be read for any reason
 160      * other than end of file, or if the input stream has been closed, or if
 161      * some other I/O error occurs.
 162      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 163      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 164      * <code>len</code> is negative, or <code>len</code> is greater than
 165      * <code>b.length - off</code>
 166      * @see        java.io.InputStream#read()
 167      */
 168     public int read(byte b[], int off, int len) throws IOException {
 169         Objects.requireNonNull(b);
 170         Objects.checkFromIndexSize(off, len, b.length);
 171         if (len == 0) {
 172             return 0;
 173         }
 174 
 175         int c = read();
 176         if (c == -1) {
 177             return -1;
 178         }
 179         b[off] = (byte)c;
 180 
 181         int i = 1;
 182         try {
 183             for (; i < len ; i++) {
 184                 c = read();
 185                 if (c == -1) {
 186                     break;
 187                 }
 188                 b[off + i] = (byte)c;
 189             }


 309      * stream specific, and therefore not specified.
 310      *
 311      * <p> If an I/O error occurs reading from the input stream, then it may do
 312      * so after some, but not all, bytes of {@code b} have been updated with
 313      * data from the input stream. Consequently the input stream and {@code b}
 314      * may be in an inconsistent state. It is strongly recommended that the
 315      * stream be promptly closed if an I/O error occurs.
 316      *
 317      * @param  b the byte array into which the data is read
 318      * @param  off the start offset in {@code b} at which the data is written
 319      * @param  len the maximum number of bytes to read
 320      * @return the actual number of bytes read into the buffer
 321      * @throws IOException if an I/O error occurs
 322      * @throws NullPointerException if {@code b} is {@code null}
 323      * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
 324      *         is negative, or {@code len} is greater than {@code b.length - off}
 325      *
 326      * @since 9
 327      */
 328     public int readNBytes(byte[] b, int off, int len) throws IOException {
 329         Objects.requireNonNull(b);
 330         Objects.checkFromIndexSize(off, len, b.length);
 331 
 332         int n = 0;
 333         while (n < len) {
 334             int count = read(b, off + n, len - n);
 335             if (count < 0)
 336                 break;
 337             n += count;
 338         }
 339         return n;
 340     }
 341 
 342     /**
 343      * Skips over and discards <code>n</code> bytes of data from this input
 344      * stream. The <code>skip</code> method may, for a variety of reasons, end
 345      * up skipping over some smaller number of bytes, possibly <code>0</code>.
 346      * This may result from any of a number of conditions; reaching end of file
 347      * before <code>n</code> bytes have been skipped is only one possibility.
 348      * The actual number of bytes skipped is returned. If {@code n} is
 349      * negative, the {@code skip} method for class {@code InputStream} always


   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


  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 contains 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()}, {@code skip()}, and
  67      * {@code transferTo()} methods all behave as if end of stream has been
  68      * reached.  After the stream has been closed, these methods all throw
  69      * {@code IOException}.
  70      *
  71      * <p> The {@code markSupported()} method returns {@code false}.  The
  72      * {@code mark()} method does nothing, and the {@code reset()} method
  73      * throws {@code IOException}.
  74      *
  75      * @return an {@code InputStream} which contains no bytes
  76      *
  77      * @since 11
  78      */
  79     public static InputStream nullInputStream() {
  80         return new InputStream() {
  81             private volatile boolean closed;
  82 
  83             private void ensureOpen() throws IOException {
  84                 if (closed) {
  85                     throw new IOException("Stream closed");
  86                 }
  87             }
  88 
  89             @Override
  90             public int available () throws IOException {
  91                 ensureOpen();
  92                 return 0;
  93             }
  94 
  95             @Override
  96             public int read() throws IOException {
  97                 ensureOpen();
  98                 return -1;
  99             }
 100 
 101             @Override
 102             public int read(byte[] b, int off, int len) throws IOException {
 103                 Objects.checkFromIndexSize(off, len, b.length);
 104                 if (len == 0) {
 105                     return 0;
 106                 }
 107                 ensureOpen();
 108                 return -1;
 109             }
 110 
 111             @Override
 112             public byte[] readAllBytes() throws IOException {
 113                 ensureOpen();
 114                 return new byte[0];
 115             }
 116 
 117             @Override
 118             public int readNBytes(byte[] b, int off, int len)
 119                 throws IOException {
 120                 Objects.checkFromIndexSize(off, len, b.length);
 121                 ensureOpen();
 122                 return 0;
 123             }
 124 
 125             @Override
 126             public long skip(long n) throws IOException {
 127                 ensureOpen();
 128                 return 0L;
 129             }
 130 
 131             @Override
 132             public long transferTo(OutputStream out) throws IOException {
 133                 Objects.requireNonNull(out);
 134                 ensureOpen();
 135                 return 0L;
 136             }
 137 
 138             @Override
 139             public void close() throws IOException {
 140                 closed = true;
 141             }
 142         };
 143     }
 144 
 145     /**
 146      * Reads the next byte of data from the input stream. The value byte is
 147      * returned as an <code>int</code> in the range <code>0</code> to
 148      * <code>255</code>. If no byte is available because the end of the stream
 149      * has been reached, the value <code>-1</code> is returned. This method
 150      * blocks until input data is available, the end of the stream is detected,
 151      * or an exception is thrown.
 152      *
 153      * <p> A subclass must provide an implementation of this method.
 154      *
 155      * @return     the next byte of data, or <code>-1</code> if the end of the
 156      *             stream is reached.
 157      * @exception  IOException  if an I/O error occurs.
 158      */
 159     public abstract int read() throws IOException;
 160 
 161     /**
 162      * Reads some number of bytes from the input stream and stores them into
 163      * the buffer array <code>b</code>. The number of bytes actually read is
 164      * returned as an integer.  This method blocks until input data is
 165      * available, end of file is detected, or an exception is thrown.


 236      * end of file is detected, or an exception is thrown. Subclasses are encouraged
 237      * to provide a more efficient implementation of this method.
 238      *
 239      * @param      b     the buffer into which the data is read.
 240      * @param      off   the start offset in array <code>b</code>
 241      *                   at which the data is written.
 242      * @param      len   the maximum number of bytes to read.
 243      * @return     the total number of bytes read into the buffer, or
 244      *             <code>-1</code> if there is no more data because the end of
 245      *             the stream has been reached.
 246      * @exception  IOException If the first byte cannot be read for any reason
 247      * other than end of file, or if the input stream has been closed, or if
 248      * some other I/O error occurs.
 249      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 250      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 251      * <code>len</code> is negative, or <code>len</code> is greater than
 252      * <code>b.length - off</code>
 253      * @see        java.io.InputStream#read()
 254      */
 255     public int read(byte b[], int off, int len) throws IOException {

 256         Objects.checkFromIndexSize(off, len, b.length);
 257         if (len == 0) {
 258             return 0;
 259         }
 260 
 261         int c = read();
 262         if (c == -1) {
 263             return -1;
 264         }
 265         b[off] = (byte)c;
 266 
 267         int i = 1;
 268         try {
 269             for (; i < len ; i++) {
 270                 c = read();
 271                 if (c == -1) {
 272                     break;
 273                 }
 274                 b[off + i] = (byte)c;
 275             }


 395      * stream specific, and therefore not specified.
 396      *
 397      * <p> If an I/O error occurs reading from the input stream, then it may do
 398      * so after some, but not all, bytes of {@code b} have been updated with
 399      * data from the input stream. Consequently the input stream and {@code b}
 400      * may be in an inconsistent state. It is strongly recommended that the
 401      * stream be promptly closed if an I/O error occurs.
 402      *
 403      * @param  b the byte array into which the data is read
 404      * @param  off the start offset in {@code b} at which the data is written
 405      * @param  len the maximum number of bytes to read
 406      * @return the actual number of bytes read into the buffer
 407      * @throws IOException if an I/O error occurs
 408      * @throws NullPointerException if {@code b} is {@code null}
 409      * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
 410      *         is negative, or {@code len} is greater than {@code b.length - off}
 411      *
 412      * @since 9
 413      */
 414     public int readNBytes(byte[] b, int off, int len) throws IOException {

 415         Objects.checkFromIndexSize(off, len, b.length);
 416 
 417         int n = 0;
 418         while (n < len) {
 419             int count = read(b, off + n, len - n);
 420             if (count < 0)
 421                 break;
 422             n += count;
 423         }
 424         return n;
 425     }
 426 
 427     /**
 428      * Skips over and discards <code>n</code> bytes of data from this input
 429      * stream. The <code>skip</code> method may, for a variety of reasons, end
 430      * up skipping over some smaller number of bytes, possibly <code>0</code>.
 431      * This may result from any of a number of conditions; reaching end of file
 432      * before <code>n</code> bytes have been skipped is only one possibility.
 433      * The actual number of bytes skipped is returned. If {@code n} is
 434      * negative, the {@code skip} method for class {@code InputStream} always


< prev index next >