OPERATING SYSTEM
----------------
All
FULL JDK VERSION(S):
-------------------
All since 1.3.1
DESCRIPTION:
------------
When the UTF-16 charset alias is used, java.io.OutputStreamWriter writes two bytes to the stream even though no data has been written to it.
To reproduce the issue, compile and run the testcase provided. You will see the following results:
File length after creating FileOutputStream: 0
File length after creating OutputStreamWriter: 0
File length after flushing OutputStreamWriter: 0
File length after closing OutputStreamWriter: 2
Unexpected byte 1 = 0xfe
Unexpected byte 2 = 0xff
The two bytes form the UTF-16 big endian byte order mark (FE FF). It seems that the OutputStreamWriter is writing the BOM to the file even though no actual data is written to the stream. Even stranger, this happens when the stream is closed (note that nothing is written when the stream is flushed, indicating that the BOM wasn't in the buffer before close() was called).
Additionally, the problem does not appear at all with the 1.3.1 JRE. I suspect the issue was introduced with NIO in 1.4.x. The OutputStreamWriter class is actually backed up by the NIO StreamWriter class.
TESTCASE SOURCE
---------------
import java.io.*;
public class Test {
private static String VM = System.getProperty("java.vm.name")+System.getProperty("java.vm.version");
public static void main(String[] args) throws Exception {
String encoding = "UTF-16";
File outfile = new File(VM+'_'+encoding+'_');
FileOutputStream fos = new FileOutputStream(outfile);
System.out.println("File length after creating FileOutputStream: " + outfile.length());
OutputStreamWriter osw = new OutputStreamWriter(fos, encoding);
System.out.println("File length after creating OutputStreamWriter: " + outfile.length());
osw.flush();
System.out.println("File length after flushing OutputStreamWriter: " + outfile.length());
osw.close();
System.out.println("File length after closing OutputStreamWriter: " + outfile.length());
DataInputStream dis = new DataInputStream(new FileInputStream(outfile));
try {
for (int i = 1;;i++) {
System.out.println("Unexpected byte " + i + " = 0x" + Integer.toHexString((int)dis.readUnsignedByte()));
}
} catch (EOFException eofe) {
}
}
}
----------------
All
FULL JDK VERSION(S):
-------------------
All since 1.3.1
DESCRIPTION:
------------
When the UTF-16 charset alias is used, java.io.OutputStreamWriter writes two bytes to the stream even though no data has been written to it.
To reproduce the issue, compile and run the testcase provided. You will see the following results:
File length after creating FileOutputStream: 0
File length after creating OutputStreamWriter: 0
File length after flushing OutputStreamWriter: 0
File length after closing OutputStreamWriter: 2
Unexpected byte 1 = 0xfe
Unexpected byte 2 = 0xff
The two bytes form the UTF-16 big endian byte order mark (FE FF). It seems that the OutputStreamWriter is writing the BOM to the file even though no actual data is written to the stream. Even stranger, this happens when the stream is closed (note that nothing is written when the stream is flushed, indicating that the BOM wasn't in the buffer before close() was called).
Additionally, the problem does not appear at all with the 1.3.1 JRE. I suspect the issue was introduced with NIO in 1.4.x. The OutputStreamWriter class is actually backed up by the NIO StreamWriter class.
TESTCASE SOURCE
---------------
import java.io.*;
public class Test {
private static String VM = System.getProperty("java.vm.name")+System.getProperty("java.vm.version");
public static void main(String[] args) throws Exception {
String encoding = "UTF-16";
File outfile = new File(VM+'_'+encoding+'_');
FileOutputStream fos = new FileOutputStream(outfile);
System.out.println("File length after creating FileOutputStream: " + outfile.length());
OutputStreamWriter osw = new OutputStreamWriter(fos, encoding);
System.out.println("File length after creating OutputStreamWriter: " + outfile.length());
osw.flush();
System.out.println("File length after flushing OutputStreamWriter: " + outfile.length());
osw.close();
System.out.println("File length after closing OutputStreamWriter: " + outfile.length());
DataInputStream dis = new DataInputStream(new FileInputStream(outfile));
try {
for (int i = 1;;i++) {
System.out.println("Unexpected byte " + i + " = 0x" + Integer.toHexString((int)dis.readUnsignedByte()));
}
} catch (EOFException eofe) {
}
}
}
- duplicates
-
JDK-6415373 (cs) UnicodeEncoder emits BOM when there are no bytes to encode
-
- Closed
-