Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8194957

new method InputStream.readAllBytes(int n) to read up to n bytes



    • CSR
    • Resolution: Withdrawn
    • P4
    • tbd
    • core-libs
    • None



      Add a method to InputStream to read up to a maximum number of bytes returned in a byte array similarly to the existing parameter-less InputStream.readAllByes().


      Using the extant InputStream.readAllBytes() can result in excessive memory allocation if the number of bytes available is larger than expected. Limiting the number of bytes to read to a given upper bound can guard against excessive unexpected input while not requiring an array allocation for the full remaining size of the stream.


      Add to InputStream a method readAllBytes(int) which returns an array containing at most the number of bytes specified. Fewer bytes will be returned if end of stream is encountered first.


           * Reads up to a specified number of bytes from the input stream. This
           * method blocks until the requested number of bytes have been read, end
           * of stream is detected, or an exception is thrown. This method does not
           * close the input stream.
           * <p> When this stream reaches end of stream, further invocations of this
           * method will return an empty byte array.
           * <p> Note that this method is intended for simple cases where it is
           * convenient to read the specified number of bytes into a byte array. It
           * is not intended for reading large amounts of data.
           * <p> The behavior for the case where the input stream is <i>asynchronously
           * closed</i>, or the thread interrupted during the read, is highly input
           * stream specific, and therefore not specified.
           * <p> If an I/O error occurs reading from the input stream, then it may do
           * so after some, but not all, bytes have been read. Consequently the input
           * stream may not be at end of stream and may be in an inconsistent state.
           * It is strongly recommended that the stream be promptly closed if an I/O
           * error occurs.
           * @param len the maximum number of bytes to read
           * @return a byte array containing the bytes read from this input stream
           * @throws IndexOutOfBoundsException if {@code length} is negative
           * @throws IOException if an I/O error occurs
           * @throws OutOfMemoryError if an array of the required size cannot be
           *         allocated. For example, if an array larger than {@code 2GB} would
           *         be required to store the bytes.
           * @since 11
          public byte[] readAllBytes(int len) throws IOException {}


        Issue Links



              bpb Brian Burkhalter
              rriggs Roger Riggs
              0 Vote for this issue
              1 Start watching this issue