# HG changeset patch # User bpb # Date 1517506838 28800 # Node ID 917868f73209e559f7c73ee12e3789a83da4192b # Parent a7ce228abcd71041b8550c5736e00a652b23f211 8139206: Add InputStream readNBytes(int len) Reviewed-by: alanb, chegar, plevart, apetcher, rriggs, weijun diff -r a7ce228abcd7 -r 917868f73209 src/java.base/share/classes/java/io/InputStream.java --- a/src/java.base/share/classes/java/io/InputStream.java Thu Feb 01 09:20:40 2018 -0800 +++ b/src/java.base/share/classes/java/io/InputStream.java Thu Feb 01 09:40:38 2018 -0800 @@ -63,7 +63,8 @@ * *
While the stream is open, the {@code available()}, {@code read()},
* {@code read(byte[])}, {@code read(byte[], int, int)},
- * {@code readAllBytes()}, {@code readNBytes()}, {@code skip()}, and
+ * {@code readAllBytes()}, {@code readNBytes(byte[], int, int)},
+ * {@code readNBytes(int)}, {@code skip(long)}, and
* {@code transferTo()} methods all behave as if end of stream has been
* reached. After the stream has been closed, these methods all throw
* {@code IOException}.
@@ -123,6 +124,15 @@
}
@Override
+ public byte[] readNBytes(int len) throws IOException {
+ if (len < 0) {
+ throw new IllegalArgumentException("len < 0");
+ }
+ ensureOpen();
+ return new byte[0];
+ }
+
+ @Override
public long skip(long n) throws IOException {
ensureOpen();
return 0L;
@@ -233,8 +243,8 @@
* b
and the number of bytes read before the exception
* occurred is returned. The default implementation of this method blocks
* until the requested amount of input data len
has been read,
- * end of file is detected, or an exception is thrown. Subclasses are encouraged
- * to provide a more efficient implementation of this method.
+ * end of file is detected, or an exception is thrown. Subclasses are
+ * encouraged to provide a more efficient implementation of this method.
*
* @param b the buffer into which the data is read.
* @param off the start offset in array b
@@ -308,26 +318,85 @@
* It is strongly recommended that the stream be promptly closed if an I/O
* error occurs.
*
+ * @implSpec
+ * This method invokes {@link #readNBytes(int)} with a length of
+ * {@link Integer#MAX_VALUE}.
+ *
* @return a byte array containing the bytes read from this input stream
* @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.
+ * allocated.
*
* @since 9
*/
public byte[] readAllBytes() throws IOException {
+ return readNBytes(Integer.MAX_VALUE);
+ }
+
+ /**
+ * 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.
+ *
+ *
The length of the returned array equals the number of bytes read + * from the stream. If {@code len} is zero, then no bytes are read and + * an empty byte array is returned. Otherwise, up to {@code len} bytes + * are read from the stream. Fewer than {@code len} bytes may be read if + * end of stream is encountered. + * + *
When this stream reaches end of stream, further invocations of this + * method will return an empty byte array. + * + *
Note that this method is intended for simple cases where it is + * convenient to read the specified number of bytes into a byte array. The + * total amount of memory allocated by this method is proportional to the + * number of bytes read from the stream which is bounded by {@code len}. + * Therefore, the method may be safely called with very large values of + * {@code len} provided sufficient memory is available. + * + *
The behavior for the case where the input stream is asynchronously + * closed, or the thread interrupted during the read, is highly input + * stream specific, and therefore not specified. + * + *
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.
+ *
+ * @implNote
+ * The number of bytes allocated to read data from this stream and return
+ * the result is bounded by {@code 2*(long)len}, inclusive.
+ *
+ * @param len the maximum number of bytes to read
+ * @return a byte array containing the bytes read from this input stream
+ * @throws IllegalArgumentException 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.
+ *
+ * @since 11
+ */
+ public byte[] readNBytes(int len) throws IOException {
+ if (len < 0) {
+ throw new IllegalArgumentException("len < 0");
+ }
+
List