The Charset.availableCharsets and Charset.name both provide ways to access the canonical names of the available Charsets. In the current implementation, these methods provide conflicting names for almost all available Charsets (windows-1252 is the only Charset where both methods return the same name).
import java.nio.Charset;
import java.util.Iterator;
public class CheckAvailableCharsets {
public static void main (String[] args) {
Iterator charsetIterator = Charset.availableCharsets().keySet().iterator();
while (charsetIterator.hasNext()) {
String charsetName = (String) charsetIterator.next();
Charset charset = Charset.forName(charsetName);
if (!charset.name().equals(charsetName)) {
System.out.println("Error: Charset name mismatch - expected "
+ charsetName + ", got " + charset.name());
}
}
}
}
produces:
Error: Charset name mismatch - expected iso-8859-1, got ISO-8859-1
Error: Charset name mismatch - expected iso-8859-15, got ISO-8859-15
Error: Charset name mismatch - expected us-ascii, got US-ASCII
Error: Charset name mismatch - expected utf-16, got UTF-16
Error: Charset name mismatch - expected utf-16be, got UTF-16BE
Error: Charset name mismatch - expected utf-16le, got UTF-16LE
Error: Charset name mismatch - expected utf-8, got UTF-8
import java.nio.Charset;
import java.util.Iterator;
public class CheckAvailableCharsets {
public static void main (String[] args) {
Iterator charsetIterator = Charset.availableCharsets().keySet().iterator();
while (charsetIterator.hasNext()) {
String charsetName = (String) charsetIterator.next();
Charset charset = Charset.forName(charsetName);
if (!charset.name().equals(charsetName)) {
System.out.println("Error: Charset name mismatch - expected "
+ charsetName + ", got " + charset.name());
}
}
}
}
produces:
Error: Charset name mismatch - expected iso-8859-1, got ISO-8859-1
Error: Charset name mismatch - expected iso-8859-15, got ISO-8859-15
Error: Charset name mismatch - expected us-ascii, got US-ASCII
Error: Charset name mismatch - expected utf-16, got UTF-16
Error: Charset name mismatch - expected utf-16be, got UTF-16BE
Error: Charset name mismatch - expected utf-16le, got UTF-16LE
Error: Charset name mismatch - expected utf-8, got UTF-8