Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check programmatically if encoding is supported

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.

like image 223
woezelmann Avatar asked Jan 20 '26 18:01

woezelmann


2 Answers

Try the following:

Charset.isSupported("UTF-8")

this method may throw RuntimeExceptions when name is null or the name is invalid.

like image 114
Uwe Plonus Avatar answered Jan 23 '26 06:01

Uwe Plonus


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);
}
like image 36
jlordo Avatar answered Jan 23 '26 07:01

jlordo