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

ByteArrayOutputStream should not throw IOExceptions

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      Hide
      This change would only add a new method which cannot have any compatibility impact.

      (The potential override clash with a writeBytes(byte b[]) method in an existing subclass is within the boundary of the minimal compatibility region.)
      Show
      This change would only add a new method which cannot have any compatibility impact. (The potential override clash with a writeBytes(byte b[]) method in an existing subclass is within the boundary of the minimal compatibility region.)
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Add writeBytes method to ByteArrayOutputStream.

      Problem

      The variant of ByteArrayOutputStream.write which accepts a single byte array parameter is inherited from OutputStream where it is declared to throw an IOException but when invoked on a ByteArrayOutputStream it cannot throw an IOException. Thus use of this method requires a useless try-catch block:

      try {
          baos.write(b);
      } catch (IOException e) {
          assert false : "cannot get here";
      }

      Solution

      Add a new method ByteArrayOutputStream.writeBytes which does not throw an IOException. Overriding OutputStream.write not to throw an IOException is not a good option as it could cause an unwelcome source incompatibility especially for those that are compiling to multiple releases without specifying --release (or -source/-target).

      Specification

      Please refer to the attachment specdiff-baos-writeBytes-8180410.zip.

      Principal Change

      Add ByteArrayOutputStream.writeBytes:

      /**
       * Writes the complete contents of the specified byte array
       * to this {@code ByteArrayOutputStream}.
       *
       * @apiNote
       * This method is equivalent to {@link #write(byte[],int,int)
       * write(b, 0, b.length)}.
       *
       * @param   b     the data.
       * @throws  NullPointerException if {@code b} is {@code null}.
       * @since   11
       */
      public void writeBytes(byte b[]) {}

      Incidental changes

      1. Document that write(b,i,i) throws NullPointerException and IndexOutOfBoundsException.
      2. Document that writeTo throws NullPointerException.
      3. Globally replace verbiage "byte array output stream" with ByteArrayOutputStream.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                bpb Brian Burkhalter
                Reporter:
                webbuggrp Webbug Group
                Reviewed By:
                Roger Riggs
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: