< prev index next >

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

Print this page




   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.Arrays;
  29 import java.util.Objects;
  30 
  31 /**
  32  * This abstract class is the superclass of all classes representing
  33  * an input stream of bytes.
  34  *
  35  * <p> Applications that need to define a subclass of <code>InputStream</code>
  36  * must always provide a method that returns the next byte of input.
  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  */


 212      *
 213      * <p> The behavior for the case where the input stream is <i>asynchronously
 214      * closed</i>, or the thread interrupted during the read, is highly input
 215      * stream specific, and therefore not specified.
 216      *
 217      * <p> If an I/O error occurs reading from the input stream, then it may do
 218      * so after some, but not all, bytes have been read. Consequently the input
 219      * stream may not be at end of stream and may be in an inconsistent state.
 220      * It is strongly recommended that the stream be promptly closed if an I/O
 221      * error occurs.
 222      *
 223      * @return a byte array containing the bytes read from this input stream
 224      * @throws IOException if an I/O error occurs
 225      * @throws OutOfMemoryError if an array of the required size cannot be
 226      *         allocated. For example, if an array larger than {@code 2GB} would
 227      *         be required to store the bytes.
 228      *
 229      * @since 9
 230      */
 231     public byte[] readAllBytes() throws IOException {
 232         byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
 233         int capacity = buf.length;
 234         int nread = 0;
 235         int n;
 236         for (;;) {
 237             // read to EOF which may read more or less than initial buffer size
 238             while ((n = read(buf, nread, capacity - nread)) > 0)
 239                 nread += n;
 240 
 241             // if the last call to read returned -1, then we're done
 242             if (n < 0)
 243                 break;

 244 
 245             // need to allocate a larger buffer
 246             if (capacity <= MAX_BUFFER_SIZE - capacity) {
 247                 capacity = capacity << 1;
 248             } else {
 249                 if (capacity == MAX_BUFFER_SIZE)
 250                     throw new OutOfMemoryError("Required array size too large");
 251                 capacity = MAX_BUFFER_SIZE;
 252             }
 253             buf = Arrays.copyOf(buf, capacity);



 254         }
 255         return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);













 256     }
 257 
 258     /**
 259      * Reads the requested number of bytes from the input stream into the given
 260      * byte array. This method blocks until {@code len} bytes of input data have
 261      * been read, end of stream is detected, or an exception is thrown. The
 262      * number of bytes actually read, possibly zero, is returned. This method
 263      * does not close the input stream.
 264      *
 265      * <p> In the case where end of stream is reached before {@code len} bytes
 266      * have been read, then the actual number of bytes read will be returned.
 267      * When this stream reaches end of stream, further invocations of this
 268      * method will return zero.
 269      *
 270      * <p> If {@code len} is zero, then no bytes are read and {@code 0} is
 271      * returned; otherwise, there is an attempt to read up to {@code len} bytes.
 272      *
 273      * <p> The first byte read is stored into element {@code b[off]}, the next
 274      * one in to {@code b[off+1]}, and so on. The number of bytes read is, at
 275      * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually




   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.Objects;
  31 
  32 /**
  33  * This abstract class is the superclass of all classes representing
  34  * an input stream of bytes.
  35  *
  36  * <p> Applications that need to define a subclass of <code>InputStream</code>
  37  * must always provide a method that returns the next byte of input.
  38  *
  39  * @author  Arthur van Hoff
  40  * @see     java.io.BufferedInputStream
  41  * @see     java.io.ByteArrayInputStream
  42  * @see     java.io.DataInputStream
  43  * @see     java.io.FilterInputStream
  44  * @see     java.io.InputStream#read()
  45  * @see     java.io.OutputStream
  46  * @see     java.io.PushbackInputStream
  47  * @since   1.0
  48  */


 213      *
 214      * <p> The behavior for the case where the input stream is <i>asynchronously
 215      * closed</i>, or the thread interrupted during the read, is highly input
 216      * stream specific, and therefore not specified.
 217      *
 218      * <p> If an I/O error occurs reading from the input stream, then it may do
 219      * so after some, but not all, bytes have been read. Consequently the input
 220      * stream may not be at end of stream and may be in an inconsistent state.
 221      * It is strongly recommended that the stream be promptly closed if an I/O
 222      * error occurs.
 223      *
 224      * @return a byte array containing the bytes read from this input stream
 225      * @throws IOException if an I/O error occurs
 226      * @throws OutOfMemoryError if an array of the required size cannot be
 227      *         allocated. For example, if an array larger than {@code 2GB} would
 228      *         be required to store the bytes.
 229      *
 230      * @since 9
 231      */
 232     public byte[] readAllBytes() throws IOException {
 233         var bufs = new ArrayList<byte[]>(8);
 234         var buf = new byte[DEFAULT_BUFFER_SIZE];
 235         int total = 0;
 236         int n;
 237         do {
 238             int nread = 0;


 239 
 240             // read to EOF which may read more or less than buffer size
 241             while ((n = read(buf, nread, buf.length - nread)) > 0) {
 242                 nread += n;
 243             }
 244 
 245             if (nread > 0) {
 246                 if (MAX_BUFFER_SIZE - total < nread) {



 247                     throw new OutOfMemoryError("Required array size too large");

 248                 }
 249                 total += nread;
 250                 var copy = (n < 0 && nread == DEFAULT_BUFFER_SIZE) ?
 251                     buf : Arrays.copyOf(buf, nread);
 252                 bufs.add(copy);
 253             }
 254         } while (n >= 0); // if the last call to read returned -1, then break
 255 
 256         if (bufs.size() == 1) {
 257             return bufs.get(0);
 258         }
 259 
 260         var result = new byte[total];
 261         int offset = 0;
 262         for (var b : bufs) {
 263             System.arraycopy(b, 0, result, offset, b.length);
 264             offset += b.length;
 265         }
 266 
 267         return result;
 268     }
 269 
 270     /**
 271      * Reads the requested number of bytes from the input stream into the given
 272      * byte array. This method blocks until {@code len} bytes of input data have
 273      * been read, end of stream is detected, or an exception is thrown. The
 274      * number of bytes actually read, possibly zero, is returned. This method
 275      * does not close the input stream.
 276      *
 277      * <p> In the case where end of stream is reached before {@code len} bytes
 278      * have been read, then the actual number of bytes read will be returned.
 279      * When this stream reaches end of stream, further invocations of this
 280      * method will return zero.
 281      *
 282      * <p> If {@code len} is zero, then no bytes are read and {@code 0} is
 283      * returned; otherwise, there is an attempt to read up to {@code len} bytes.
 284      *
 285      * <p> The first byte read is stored into element {@code b[off]}, the next
 286      * one in to {@code b[off+1]}, and so on. The number of bytes read is, at
 287      * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually


< prev index next >