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

PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause

XMLWordPrintable

    • b14
    • x86
    • os_x

      A DESCRIPTION OF THE REQUEST :
      FilterOutputStream#write(byte[]) is the only method that PrintStream doesn’t override. The method specifies that it can throw an IOException, but in practice, this will never happen if the instance is a PrintStream proper (the only expression that could have thrown one is the method call that is dispatched to PrintStream#write(byte[], int, int) in such an instance).

      JUSTIFICATION :
      One of the features of PrintStream is that “a PrintStream never throws an IOException”, so I think it’s reasonable to expect that I can write a call to any of the methods that are available to a PrintStream proper without needing to catch or specify IOException, but the existing implementation yields a compile error for such a call to #write(byte[]).

      The existing implementation also allows a subclass of PrintStream to override FilterOutputStream#write(byte[]) with a method that can or does throw an IOException, but an instance of a subtype of PrintStream is a PrintStream, and “a PrintStream never throws an IOException”.

      PrintStream.java contains a section with the comment “Exception-catching, synchronized output operations, which also implement the write() methods of OutputStream”, but the class does not implement all of “the write() methods of OutputStream”.

      On the other hand, the problem causes very little inconvenience, and the enhancement will break any subclasses of PrintStream in which #write(byte[]) has a throws clause (though I would argue that such a subclass violates the spirit of PrintStream).


      ---------- BEGIN SOURCE ----------
      public class Example {
          public static void main(String[] arguments) {
              byte[] buffer = new byte[0];

              // this call works
              System.out.write(buffer, 0, buffer.length);

              // unreported exception IOException; must be caught or declared to be thrown
              // System.out.write(buffer);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Change the affected expression from “write(x)” to “write(x,0,x.length)”.

            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: