Canonical names of encodings is clear - here is the official list: we have Canonical Names for java.nio API and Canonical Names for java.io API and java.lang API.

1) But what is the list of historical encoding names?

OuputStreamWriter Javadoc for getEncoding():

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.

Charset JavaDoc:

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.

2) Canonical names of encodings official list states that for java.io canonical name "UTF8" shall be used, while "UTF-8" is a canoical name for the new java.nio.

But InputStreamReader/OutputStreamWriter does accept "UTF-8" argument!

try {
    FileOutputStream stream1 = new FileOutputStream("d:\\file.txt");
    OutputStreamWriter writer1 = new OutputStreamWriter(stream1, "UTF-8");
    System.out.println(writer1.getEncoding());
    writer1.close();
} catch (UnsupportedEncodingException e) {
    System.out.println("wrong encoding detected"); // never triggered
}

It seems "UTF-8" is recognized fine, no UnsupportedEncodingException fired!

P.S. I think that any API (even non-NIO) now (SE8) can accept NIO-style, and java.lang/java.io style is called historical. It seems true at least for InputStreamReader/OutputStreamWriter.

Does my proposition hold true?

0 Answers