JDK-8187898 : PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86
  • Submitted: 2017-09-23
  • Updated: 2019-09-26
  • Resolved: 2019-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 14
14 b14Fixed
Related Reports
CSR :  
Relates :  
Description
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)������.


Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/930551e8ac62 User: bpb Date: 2019-09-05 23:27:26 +0000
05-09-2019