< prev index next >

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

Print this page
rev 48211 : imported patch 4358774-nullISAndOS.01
rev 48210 : 4358774: Add null InputStream and OutputStream
Reviewed-by: reinhapa
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             private void ensureOpen() throws IOException {
  82                 if (closed) {
  83                     throw new IOException("Stream closed");
  84                 }
  85             }
  86 
  87             @Override
  88             public int available () throws IOException {
  89                 ensureOpen();
  90                 return 0;
  91             }
  92 
  93             @Override
  94             public int read() throws IOException {
  95                 ensureOpen();
  96                 return -1;
  97             }
  98 
  99             @Override
 100             public int read(byte[] b, int off, int len) throws IOException {
 101                 if (b == null) {
 102                     throw new NullPointerException();
 103                 } else if (off < 0 || len < 0 || len > b.length - off) {
 104                     throw new IndexOutOfBoundsException();
 105                 }
 106                 if (len == 0) {
 107                     return 0;
 108                 }
 109                 ensureOpen();
 110                 return -1;
 111             }
 112 
 113             @Override
 114             // overridden for efficiency
 115             public byte[] readAllBytes() throws IOException {
 116                 ensureOpen();
 117                 return new byte[0];
 118             }
 119 
 120             @Override
 121             // overridden for efficiency
 122             public int readNBytes(byte[] b, int off, int len)
 123                 throws IOException {
 124                 if (b == null) {
 125                     throw new NullPointerException();
 126                 } else if (off < 0 || len < 0 || len > b.length - off) {
 127                     throw new IndexOutOfBoundsException();
 128                 }
 129                 ensureOpen();
 130                 return 0;
 131             }
 132 
 133             @Override
 134             // overridden for efficiency
 135             public long skip(long n) throws IOException {
 136                 ensureOpen();
 137                 return 0L;
 138             }
 139 
 140             @Override
 141             // overridden for efficiency
 142             public long transferTo(OutputStream out) throws IOException {
 143                 if (out == null) {
 144                     throw new NullPointerException();
 145                 }
 146                 ensureOpen();
 147                 return 0L;
 148             }
 149 
 150             @Override
 151             public void close() throws IOException {
 152                 closed = true;
 153             }
 154         };
 155     }
 156 
 157     /**
 158      * Reads the next byte of data from the input stream. The value byte is
 159      * returned as an <code>int</code> in the range <code>0</code> to
 160      * <code>255</code>. If no byte is available because the end of the stream
 161      * has been reached, the value <code>-1</code> is returned. This method
 162      * blocks until input data is available, the end of the stream is detected,
 163      * or an exception is thrown.
 164      *
 165      * <p> A subclass must provide an implementation of this method.
 166      *
 167      * @return     the next byte of data, or <code>-1</code> if the end of the
 168      *             stream is reached.
 169      * @exception  IOException  if an I/O error occurs.
 170      */
 171     public abstract int read() throws IOException;
 172 
 173     /**
 174      * Reads some number of bytes from the input stream and stores them into
 175      * the buffer array <code>b</code>. The number of bytes actually read is
 176      * returned as an integer.  This method blocks until input data is
 177      * available, end of file is detected, or an exception is thrown.


< prev index next >