Is there a way to find out if an encoding is supported or not? E. g. a method like this:
isSupported("UTF-8") == true
and
isSupported("UTF-13") == false
I need this to validate if the Content-Disposition-Header of my MimeMessages is correct.
Try the following:
Charset.isSupported("UTF-8")
this method may throw RuntimeExceptions when name is null or the name is invalid.
IllegalCharsetNameException if the given charset name is illegalRuntimeException when the name is invalid.boolean isCharsetSupported(String name) {
try {
Charset.forName(name);
return true;
} catch (UnsupportedCharsetException | IllegalCharsetNameException | IllegalArgumentException e) {
return false;
}
}
or without a try/catch block:
boolean isCharsetSupported(String name) {
return Charset.availableCharsets().keySet().contains(name);
}
Exceptions as flow control is discouraged in Java.