There is an incompatible behavior between 1.3.1_XX and 1.4.2_0X.
The attached test program works well in 1.3.1_0X(1.4.1_0X also),
but not in 1.4.2_0X(1.5.betab38).
REPRODUCE :
(1) Compile test.java
(2) Launch "java test windows-31j" in 1.3.1_11
You will see only the message, "2nd close"
K:\illegal-excp>java test windows-31j
2nd close
(3) Launch "java test windows-31j" in 1.4.2_03
You will see the stack trace as follows.
K:\illegal-excp>java test windows-31j
2nd close
catch in main
java.lang.IllegalStateException: Current state = FLUSHED, new state = CODING_END
at java.nio.charset.CharsetEncoder.throwIllegalStateException(CharsetEnc
oder.java:933)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:529)
at sun.nio.cs.StreamEncoder$CharsetSE.flushLeftoverChar(StreamEncoder.ja
va:358)
at sun.nio.cs.StreamEncoder$CharsetSE.implClose(StreamEncoder.java:414)
at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:160)
at java.io.OutputStreamWriter.close(OutputStreamWriter.java:222)
at java.io.PrintWriter.close(PrintWriter.java:137)
at test.main(test.java:10)
The java.lang.IllegalStateException occurs in 1.4.2( and Tiger b38 ),
not in 1.3.1(1.4.0).
INVESTIGATION:
===== Test Program ( test.java ) =========
import java.io.*;
class test {
public static void main( String arg[] ) {
try {
OutputStreamWriter osw =
new OutputStreamWriter( new MyOutputStream("file"), arg[0] );
PrintWriter pw = new PrintWriter( osw );
pw.close(); // 1st close
System.out.println("2nd close");
pw.close(); // 2nd close
}
catch( Exception e ) {
System.out.println("catch in main");
e.printStackTrace();
}
}
}
class MyOutputStream extends FileOutputStream {
MyOutputStream( String s ) throws FileNotFoundException {
super(s);
}
public void close() throws IOException {
throw new IOException("Dummy Exception");
}
}
==========================================
The above program is implemented as an exception appears on PrintWriter close.
(Please don't consider the sample code has problem.)
Please consider the above behavior of MyOutputStream simulates "some exception"
in the 3rd line of followings.
- OutputStreamWriter object is created with char. encoding
which is supported by nio .
- Printwriter object is created based on OutputStreamWriter object.
- Some exception occurs in close() for PrintWriter (pw.close() in "1st close" line)
and close() is launched again.(pw.close() in "2nd close" line.)
Indeed, the API specifications in java.nio.charset.CharsetEncoder#encode mentions
the IllegalStateException in "Throw" section.
The users can be only aware of the OutputStreamWriter and PrintWriter.
However, the API docs of those classes does not mention IllegalStateException.
As a result, Java users don't write the code which can catch IllegalStateException
and will be confused because of an unexpected exception.
It means that users can not port the programs written in 1.3.1 to environment
based on 1.4.2.
PROBLEM:
- losing portability from 1.3.1(1.4.0) to 1.4.2 (and Tiger)
===========================================================================