< prev index next >

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

Print this page
rev 48083 : [mq]: 4358774-nullISAndOS.01
rev 48082 : 4358774: Add null InputStream and OutputStream
Reviewed-by: XXX
rev 47920 : 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
Summary: Reduce parameter bounds checks from five to three as in InputStream::read
Reviewed-by: psandoz
rev 47216 : 8187443: Forest Consolidation: Move files to unified layout
Reviewed-by: darcy, ihse


  37  *
  38  * @author  Arthur van Hoff
  39  * @see     java.io.BufferedInputStream
  40  * @see     java.io.ByteArrayInputStream
  41  * @see     java.io.DataInputStream
  42  * @see     java.io.FilterInputStream
  43  * @see     java.io.InputStream#read()
  44  * @see     java.io.OutputStream
  45  * @see     java.io.PushbackInputStream
  46  * @since   1.0
  47  */
  48 public abstract class InputStream implements Closeable {
  49 
  50     // MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to
  51     // use when skipping.
  52     private static final int MAX_SKIP_BUFFER_SIZE = 2048;
  53 
  54     private static final int DEFAULT_BUFFER_SIZE = 8192;
  55 
  56     /**




































































































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




  37  *
  38  * @author  Arthur van Hoff
  39  * @see     java.io.BufferedInputStream
  40  * @see     java.io.ByteArrayInputStream
  41  * @see     java.io.DataInputStream
  42  * @see     java.io.FilterInputStream
  43  * @see     java.io.InputStream#read()
  44  * @see     java.io.OutputStream
  45  * @see     java.io.PushbackInputStream
  46  * @since   1.0
  47  */
  48 public abstract class InputStream implements Closeable {
  49 
  50     // MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to
  51     // use when skipping.
  52     private static final int MAX_SKIP_BUFFER_SIZE = 2048;
  53 
  54     private static final int DEFAULT_BUFFER_SIZE = 8192;
  55 
  56     /**
  57      * Returns a new {@code InputStream} that contains no bytes. The returned
  58      * stream is initially open.  The stream is closed by calling the
  59      * {@code close()} method.  Subsequent calls to {@code close()} have no
  60      * effect.
  61      *
  62      * <p> While the stream is open, the {@code available()}, {@code read()},
  63      * {@code read(byte[])}, {@code read(byte[], int, int)},
  64      * {@code readAllBytes()}, {@code readNBytes()}, {@code skip()}, and
  65      * {@code transferTo()} methods all behave as if end of stream has been
  66      * reached.  After the stream has been closed, these methods all throw
  67      * {@code IOException}.
  68      *
  69      * <p> The {@code markSupported()} method returns {@code false}.  The
  70      * {@code mark()} method does nothing, and the {@code reset()} method
  71      * throws {@code IOException}.
  72      *
  73      * @return an {@code InputStream} which contains no bytes
  74      *
  75      * @since 10
  76      */
  77     public static InputStream nullStream() {
  78         return new InputStream() {
  79             private volatile boolean closed;
  80 
  81             @Override
  82             public int available () throws IOException {
  83                 if (closed) {
  84                     throw new IOException("Stream closed");
  85                 }
  86                 return 0;
  87             }
  88 
  89             @Override
  90             public int read() throws IOException {
  91                 if (closed) {
  92                     throw new IOException("Stream closed");
  93                 }
  94                 return -1;
  95             }
  96 
  97             @Override
  98             public int read(byte[] b, int off, int len) throws IOException {
  99                 Objects.requireNonNull(b);
 100                 Objects.checkFromIndexSize(off, len, b.length);
 101                 if (len == 0) {
 102                     return 0;
 103                 } else if (closed) {
 104                     throw new IOException("Stream closed");
 105                 }
 106                 return -1;
 107             }
 108 
 109             @Override
 110             // overridden for efficiency
 111             public byte[] readAllBytes() throws IOException {
 112                 if (closed) {
 113                     throw new IOException("Stream closed");
 114                 }
 115                 return new byte[0];
 116             }
 117 
 118             @Override
 119             // overridden for efficiency
 120             public int readNBytes(byte[] b, int off, int len)
 121                 throws IOException {
 122                 Objects.requireNonNull(b);
 123                 Objects.checkFromIndexSize(off, len, b.length);
 124                 if (closed) {
 125                     throw new IOException("Stream closed");
 126                 }
 127                 return 0;
 128             }
 129 
 130             @Override
 131             // overridden for efficiency
 132             public long skip(long n) throws IOException {
 133                 if (closed) {
 134                     throw new IOException("Stream closed");
 135                 }
 136                 return 0L;
 137             }
 138 
 139             @Override
 140             // overridden for efficiency
 141             public long transferTo(OutputStream out) throws IOException {
 142                 Objects.requireNonNull(out);
 143                 if (closed) {
 144                     throw new IOException("Stream closed");
 145                 }
 146                 return 0L;
 147             }
 148 
 149             @Override
 150             public void close() throws IOException {
 151                 closed = true;
 152             }
 153         };
 154     }
 155 
 156     /**
 157      * Reads the next byte of data from the input stream. The value byte is
 158      * returned as an <code>int</code> in the range <code>0</code> to
 159      * <code>255</code>. If no byte is available because the end of the stream
 160      * has been reached, the value <code>-1</code> is returned. This method
 161      * blocks until input data is available, the end of the stream is detected,
 162      * or an exception is thrown.
 163      *
 164      * <p> A subclass must provide an implementation of this method.
 165      *
 166      * @return     the next byte of data, or <code>-1</code> if the end of the
 167      *             stream is reached.
 168      * @exception  IOException  if an I/O error occurs.
 169      */
 170     public abstract int read() throws IOException;
 171 
 172     /**
 173      * Reads some number of bytes from the input stream and stores them into
 174      * the buffer array <code>b</code>. The number of bytes actually read is
 175      * returned as an integer.  This method blocks until input data is
 176      * available, end of file is detected, or an exception is thrown.


< prev index next >