JDK-4147276 : java.io.OutputStreamWriter throws exception instead of flushing internal buffer
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1.3,1.1.5,1.1.6,1.2.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS:
    generic,solaris_2.5.1,solaris_2.6,windows_nt generic,solaris_2.5.1,solaris_2.6,windows_nt
  • CPU: generic,x86,sparc
  • Submitted: 1998-06-10
  • Updated: 1999-07-20
  • Resolved: 1999-07-20
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.
Other Other
1.1.8 1.1.8Fixed 1.2.0Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Description

Name: mf23781			Date: 06/10/98


(KO)OutputStreamWriter incorrectly throw CharConversionException.

java.io.OutputStreamWriter incorrectly throws CharConversionException.
We've tested it on three platforms, Merlin-K, Warp Server 4 and Warp Server SMP.
There should be no CharConversionException but it occured.
Here's a Test program.

-------------------Begin of Source----------------------------------------------import java.util.*;


import java.io.*;

class BugChecker4
{
  public static void main(String[] args)
  throws IOException
  {
    System.out.println( "java.vendor: " +
    System.getProperty("java.vendor") );
    System.out.println( "java.version: " +
    System.getProperty("java.version") );
    System.out.println( "os.name: " + System.getProperty("os.name"));
    System.out.println( "os.arch: " + System.getProperty("os.arch"));
    System.out.println();

    ByteArrayOutputStream bos;
    OutputStreamWriter osw;
    byte[] array;

    bos = new ByteArrayOutputStream();
    osw = new OutputStreamWriter(bos, "EUCJIS");
    osw.write('a');
    try{
      for(int count = 0; count < 10000; ++count)
      osw.write('\u3042'); // Hiragana
      osw.close();
      array = bos.toByteArray();
      if ( array.length != 20001 )
        System.out.println( "OutputStreamWriter bug: number of EUCJIS bytes written should be 20001, " + "not be " + array.length );
    } 
    catch( CharConversionException ex){ 
      System.out.println( "OutputStreamWriter bug: CharConversionException should not occur." );
    }

    bos = new ByteArrayOutputStream();
    osw = new OutputStreamWriter(bos, "KSC5601");
    osw.write('a');
    try{
      for(int count = 0; count < 10000; ++count)
        osw.write('\uac00'); // Hangul syllable 
      osw.close();
      array = bos.toByteArray();
      if ( array.length != 20001 )
        System.out.println( "OutputStreamWriter bug: number of KSC5601 bytes written should be 20001, " + "not be " + array.length );
    } 
    catch( CharConversionException ex ){ 
      System.out.println( "OutputStreamWriter bug: CharConversionException should not occur." );
    }
  }
}


---------------------End of Source--------------------------------------------

The problem also fails on AIX ( a116-19980516 ) and Win95.

There is a related Sun defect reported against 1.1.3 for the same
testcase, but with a different failure and actually occuring later.
( Bug ID 4066847 ).

The problem seems to be with the method "writer" in OutputStreamWriter.
The writer converts characters to bytes using the chartobyte converter.
This uses a buffer of fixed length 8192. When the buffer becomes full
it should automatically be flushed.

The problem occurs when we have written out 8191 bytes and then
attempt to write out a multi-byte character. The buffer has not
yet been flushed, but the buffer thinks it cannot even hold one
character. At this point we throw:
> java.io.CharConversionException: Output buffer too small

As this is a cross-platform problem in the shared code I suggest
not to fix it for GA but to raise it as a Sun defect.

This problem can also be re-created on Solaris (duke).


======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.1.8 FIXED IN: 1.1.8 1.2fcs INTEGRATED IN: 1.1.8 1.2fcs VERIFIED IN: 1.1.8
14-06-2004

EVALUATION The analysis in the description is correct. The write(char[], int, int) method in OutputStreamWriter should attempt to flush the buffer before concluding that the buffer is too small for even one character. -- mr@eng 6/10/1998 Backported fix into 1.1.8 stuart.lawrence@eng 1998-12-18
10-06-1998