< prev index next >

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

Print this page




 184      * next one into <code>b[1]</code>, and so on. The number of bytes read is,
 185      * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
 186      * number of bytes actually read; these bytes will be stored in elements
 187      * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
 188      * leaving elements <code>b[</code><i>k</i><code>]</code> through
 189      * <code>b[b.length-1]</code> unaffected.
 190      *
 191      * <p> The <code>read(b)</code> method for class <code>InputStream</code>
 192      * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
 193      *
 194      * @param      b   the buffer into which the data is read.
 195      * @return     the total number of bytes read into the buffer, or
 196      *             <code>-1</code> if there is no more data because the end of
 197      *             the stream has been reached.
 198      * @exception  IOException  If the first byte cannot be read for any reason
 199      * other than the end of the file, if the input stream has been closed, or
 200      * if some other I/O error occurs.
 201      * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
 202      * @see        java.io.InputStream#read(byte[], int, int)
 203      */
 204     public int read(byte b[]) throws IOException {
 205         return read(b, 0, b.length);
 206     }
 207 
 208     /**
 209      * Reads up to <code>len</code> bytes of data from the input stream into
 210      * an array of bytes.  An attempt is made to read as many as
 211      * <code>len</code> bytes, but a smaller number may be read.
 212      * The number of bytes actually read is returned as an integer.
 213      *
 214      * <p> This method blocks until input data is available, end of file is
 215      * detected, or an exception is thrown.
 216      *
 217      * <p> If <code>len</code> is zero, then no bytes are read and
 218      * <code>0</code> is returned; otherwise, there is an attempt to read at
 219      * least one byte. If no byte is available because the stream is at end of
 220      * file, the value <code>-1</code> is returned; otherwise, at least one
 221      * byte is read and stored into <code>b</code>.
 222      *
 223      * <p> The first byte read is stored into element <code>b[off]</code>, the
 224      * next one into <code>b[off+1]</code>, and so on. The number of bytes read


 245      * until the requested amount of input data <code>len</code> has been read,
 246      * end of file is detected, or an exception is thrown. Subclasses are
 247      * encouraged to provide a more efficient implementation of this method.
 248      *
 249      * @param      b     the buffer into which the data is read.
 250      * @param      off   the start offset in array <code>b</code>
 251      *                   at which the data is written.
 252      * @param      len   the maximum number of bytes to read.
 253      * @return     the total number of bytes read into the buffer, or
 254      *             <code>-1</code> if there is no more data because the end of
 255      *             the stream has been reached.
 256      * @exception  IOException If the first byte cannot be read for any reason
 257      * other than end of file, or if the input stream has been closed, or if
 258      * some other I/O error occurs.
 259      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 260      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 261      * <code>len</code> is negative, or <code>len</code> is greater than
 262      * <code>b.length - off</code>
 263      * @see        java.io.InputStream#read()
 264      */
 265     public int read(byte b[], int off, int len) throws IOException {
 266         Objects.checkFromIndexSize(off, len, b.length);
 267         if (len == 0) {
 268             return 0;
 269         }
 270 
 271         int c = read();
 272         if (c == -1) {
 273             return -1;
 274         }
 275         b[off] = (byte)c;
 276 
 277         int i = 1;
 278         try {
 279             for (; i < len ; i++) {
 280                 c = read();
 281                 if (c == -1) {
 282                     break;
 283                 }
 284                 b[off + i] = (byte)c;
 285             }




 184      * next one into <code>b[1]</code>, and so on. The number of bytes read is,
 185      * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
 186      * number of bytes actually read; these bytes will be stored in elements
 187      * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
 188      * leaving elements <code>b[</code><i>k</i><code>]</code> through
 189      * <code>b[b.length-1]</code> unaffected.
 190      *
 191      * <p> The <code>read(b)</code> method for class <code>InputStream</code>
 192      * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
 193      *
 194      * @param      b   the buffer into which the data is read.
 195      * @return     the total number of bytes read into the buffer, or
 196      *             <code>-1</code> if there is no more data because the end of
 197      *             the stream has been reached.
 198      * @exception  IOException  If the first byte cannot be read for any reason
 199      * other than the end of the file, if the input stream has been closed, or
 200      * if some other I/O error occurs.
 201      * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
 202      * @see        java.io.InputStream#read(byte[], int, int)
 203      */
 204     public int read(byte[] b) throws IOException {
 205         return read(b, 0, b.length);
 206     }
 207 
 208     /**
 209      * Reads up to <code>len</code> bytes of data from the input stream into
 210      * an array of bytes.  An attempt is made to read as many as
 211      * <code>len</code> bytes, but a smaller number may be read.
 212      * The number of bytes actually read is returned as an integer.
 213      *
 214      * <p> This method blocks until input data is available, end of file is
 215      * detected, or an exception is thrown.
 216      *
 217      * <p> If <code>len</code> is zero, then no bytes are read and
 218      * <code>0</code> is returned; otherwise, there is an attempt to read at
 219      * least one byte. If no byte is available because the stream is at end of
 220      * file, the value <code>-1</code> is returned; otherwise, at least one
 221      * byte is read and stored into <code>b</code>.
 222      *
 223      * <p> The first byte read is stored into element <code>b[off]</code>, the
 224      * next one into <code>b[off+1]</code>, and so on. The number of bytes read


 245      * until the requested amount of input data <code>len</code> has been read,
 246      * end of file is detected, or an exception is thrown. Subclasses are
 247      * encouraged to provide a more efficient implementation of this method.
 248      *
 249      * @param      b     the buffer into which the data is read.
 250      * @param      off   the start offset in array <code>b</code>
 251      *                   at which the data is written.
 252      * @param      len   the maximum number of bytes to read.
 253      * @return     the total number of bytes read into the buffer, or
 254      *             <code>-1</code> if there is no more data because the end of
 255      *             the stream has been reached.
 256      * @exception  IOException If the first byte cannot be read for any reason
 257      * other than end of file, or if the input stream has been closed, or if
 258      * some other I/O error occurs.
 259      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 260      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 261      * <code>len</code> is negative, or <code>len</code> is greater than
 262      * <code>b.length - off</code>
 263      * @see        java.io.InputStream#read()
 264      */
 265     public int read(byte[] b, int off, int len) throws IOException {
 266         Objects.checkFromIndexSize(off, len, b.length);
 267         if (len == 0) {
 268             return 0;
 269         }
 270 
 271         int c = read();
 272         if (c == -1) {
 273             return -1;
 274         }
 275         b[off] = (byte)c;
 276 
 277         int i = 1;
 278         try {
 279             for (; i < len ; i++) {
 280                 c = read();
 281                 if (c == -1) {
 282                     break;
 283                 }
 284                 b[off + i] = (byte)c;
 285             }


< prev index next >