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

ByteArrayInputStream should override readAllBytes, readNBytes, and transferTo

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P5
    • Resolution: Withdrawn
    • Fix Version/s: 10
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      source
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      Hide
      The proposed methods readAllBytes() and readNBytes() do not throw IOException as do the InputStream methods they override. This is consistent with the read(int) and read(byte[],int,int) methods already in ByteArrayInputStream. This means however that while code such as

              ByteArrayInputStream bais = new ByteArrayInputStream(new byte[999]);
              try {
                  byte[] b = bais.readAllBytes();
              } catch(IOException e) {
              }

      will continue to link, i.e., existing class files will continue to be able to be run, the code would not compile using a javac from a JDK version prior to the one including the proposed change. As the methods readAllBytes() and readNBytes() were added to InputStream only recently in JDK 9, it seems unlikely that this change would cause serious problems. Also, if the change is deemed desirable, it would be better to make it sooner when it is less likely to cause problems.
      Show
      The proposed methods readAllBytes() and readNBytes() do not throw IOException as do the InputStream methods they override. This is consistent with the read(int) and read(byte[],int,int) methods already in ByteArrayInputStream. This means however that while code such as         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[999]);         try {             byte[] b = bais.readAllBytes();         } catch(IOException e) {         } will continue to link, i.e., existing class files will continue to be able to be run, the code would not compile using a javac from a JDK version prior to the one including the proposed change. As the methods readAllBytes() and readNBytes() were added to InputStream only recently in JDK 9, it seems unlikely that this change would cause serious problems. Also, if the change is deemed desirable, it would be better to make it sooner when it is less likely to cause problems.

      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 {}

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                bpb Brian Burkhalter
                Reporter:
                smarks Stuart Marks
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: