We found some inconsistencies and lack of documentation in the way OutputStreamWriter getEncoding() method is formatting the returned encoding names. See below, sample programs and output that illustrate the issue.
UTF-8 Encoding
FileOutputStream stream1 = new FileOutputStream("C:\\test\\out.svg"); OutputStreamWriter writer1 = new OutputStreamWriter(stream1 , "UTF-8"); System.out.println(writer1.getEncoding()); writer1.close(); FileOutputStream stream2 = new FileOutputStream("C:\\test\\out.svg"); OutputStreamWriter writer2 = new OutputStreamWriter(stream2, "UTF8"); System.out.println(writer2.getEncoding()); writer2.close(); |
The output to this program is
UTF8 UTF8
UTF-16 Encoding
FileOutputStream stream1 = new FileOutputStream("C:\\test\\out.svg"); OutputStreamWriter writer1 = new OutputStreamWriter(stream1, "UTF-16"); System.out.println(writer1.getEncoding()); writer1.close(); FileOutputStream stream1 = new FileOutputStream("C:\\test\\out.svg"); OutputStreamWriter writer2 = new OutputStreamWriter(stream2, "UTF16"); OutputStreamWriter System.out.println(writer2.getEncoding()); writer2.close(); |
The output to this program is:
UTF-16 UTF-16
Unclear JavaDoc
The OuputStreamWriter Javadoc for the getEncoding() method is unclear about the encoding naming convention.
public String getEncoding() Returns the name of the character encoding being used by this stream. If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned. If this instance was created with the OutputStreamWriter(OutputStream, String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.
And in the Charset JavaDoc, one can read:
Some charsets have an historical name that is defined for compatibility with previous versions of the Java platform. A charset's historical name is either its canonical name or one of its aliases. The historical name is returned by the getEncoding() methods of the InputStreamReader and OutputStreamWriter classes.