JDK-4787155 : ByteArrayOutputStream.write(byte[]) should not throw IOException
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: solaris_8
  • CPU: sparc
  • Submitted: 2002-12-02
  • Updated: 2017-05-16
  • Resolved: 2005-11-27
Related Reports
Relates :  
Description
Name: nt126004			Date: 12/02/2002


FULL PRODUCT VERSION :
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)

FULL OPERATING SYSTEM VERSION : Any


A DESCRIPTION OF THE PROBLEM :
ByteArrayOutputStream does not override write(byte[])
method of OutputStream. write(byte[]) of
OutputStream throws IOException

As a result one always
has to do something like this:

ByteArrayOutputStream b;

try {
   b.write(buffer);
} catch (IOException e) {
   // never happens
}

Otherwise compiler will complain.


Note that I can avoid this by doing

   b.write(buffer, 0, buffer.length);

Since this method is implemented in ByteArrayOutputStream

This is very annoying.




STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
add the following in you program:


ByteArrayOutputStream bos = new ByteArrayOutputStream();


bos.write(new byte[1]);

This will not compile and you will have to
wrap it into a try block which is not necessary since
this method actually never throws IOException


try {
     bos.write(new byte[1]);
} catch (IOException e){
    // never happens
}






EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected - method should not need a try block around it
Actual - try block needed, otherwise this will not compile

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Compiler: IOException musr be caught or declared thrown.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
include java.io.*;

// this will not compile
public class Test {
   public static void main(String[] x) {
        new ByteArrayOutputStream.write(new byte[1]);
   }
}


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

CUSTOMER WORKAROUND :
Always include unnecessaty try/catch block -
very awkward
(Review ID: 167129) 
======================================================================

Comments
EVALUATION Sigh. ByteArrayOutputStream does not consistently throw IOException with respect to its write methods: throws Method IOEx? Notes ----------------------- ------ ----------------------------------------- write(int) no - override in BAOS write(byte[]) yes - inherited from abstract OutputStream - trivially invokes write(byte[],int,int) write(byte[], int, int) no - override in BAOS Unfortunately, there's not much we can do to fix this. Since IOException is a checked exception, providing an override for write(byte) which does not throw it would cause compatibility problems.
27-11-2005

WORK AROUND Use write(byte [], int, int).
27-11-2005

EVALUATION Given that ByteArrayOutputStream is not marked as final, I don't think we can do this without potentially breaking existing apps.
01-08-2005

EVALUATION The suggested change is possible for a future release. ###@###.### 2002-12-03
03-12-2002