JDK-8193148 : ByteArrayInputStream should override readAllBytes, readNBytes, and transferTo
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.io
  • Priority: P5
  • Status: Closed
  • Resolution: Withdrawn
  • Fix Versions: 10
  • Submitted: 2017-12-06
  • Updated: 2017-12-11
  • Resolved: 2017-12-11
Related Reports
CSR :  
Description
Summary
-------

Override readAllBytes, readNBytes, and transferTo in ByteArrayInputStream.

Problem
-------

The methods readAllBytes, readNBytes, and transferTo were added to InputStream in JDK 9.  When invoked on a ByteArrayInputStream however their implementations will be less efficient that if the internal byte array were used directly.

Solution
--------

Override readAllBytes, readNBytes, and transferTo in ByteArrayInputStream to use the internal byte array directly.

Specification
-------------

Overrides in `java.io.ByteArrayInputStream`:

```
    /**
     * Reads all remaining bytes from the input stream.  This method does not
     * block.  This method does not close the input stream.
     *
     * When this stream reaches end of stream, further invocations of this
     * method will return an empty byte array.
     *
     * @throws OutOfMemoryError {@inheritDoc}
     *
     * @return {@inheritDoc}
     *
     * @since 10
     */
    public byte[] readAllBytes() {}
```

```
    /**
     * Reads the requested number of bytes from the input stream into the
     * given byte array.  This method does not close the input stream.  When
     * this stream reaches end of stream, further invocations of this method
     * will return zero.
     *
     * @apiNote
     * The {@code readNBytes(b, off, len)} method for this
     * class has the same effect as {@link #read(byte[],int,int)
     * read(b, off, len)} except that zero will be returned instead of
     * {@code -1} at end of stream.
     *
     * @param   b     {@inheritDoc}
     * @param   off   {@inheritDoc}
     * @param   len   {@inheritDoc}
     * @return  {@inheritDoc}
     * @throws  NullPointerException {@inheritDoc}
     * @throws  IndexOutOfBoundsException {@inheritDoc}
     *
     * @since 10
     */
    public int readNBytes(byte[] b, int off, int len) {}
```

```
    /**
     * Reads the remaining bytes from this input stream and writes the bytes
     * to the given output stream in the order that they were read.  On return,
     * this input stream will be at end of stream.  This method does not close
     * the output stream.
     * <p>
     * This method may block indefinitely writing to the output stream.  The
     * behavior for the case where the output stream is <i>asynchronously
     * closed</i>, or the thread is interrupted during the transfer, is highly
     * output stream specific, and therefore not specified.
     * <p>
     * If an I/O error occurs writing to the output stream, then it may do so
     * after some bytes have been written.  Consequently the input stream may
     * not be at end of stream and one, or both, streams may be in an
     * inconsistent state.  It is strongly recommended that the output stream
     * be promptly closed if an I/O error occurs.
     *
     * @param  out {@inheritDoc}
     * @return {@inheritDoc}
     * @throws IOException if an I/O error occurs when writing
     * @throws NullPointerException {@inheritDoc}
     *
     * @since 10
     */
    public long transferTo(OutputStream out) throws IOException {}
```