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)â.
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)â.
- csr for
-
JDK-8230625 PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
-
- Closed
-
- relates to
-
JDK-8180410 ByteArrayOutputStream should not throw IOExceptions
-
- Resolved
-
- links to