Guava tips: Charsets

Guava contains the tiny, yet very useful class Charsets, which has constants for all the charsets that any JVM implementation must support:

  • UTF-8
  • UTF-16 in big-endian and little-endian byte orders or with the BOM (byte order mark)
  • ASCII
  • ISO-8859-1, also known as Latin-1.

These are available as constants such as Charsets.UTF_8. This can help you avoid having to write silly code such as:

try {
    myString.getBytes(Charset.forName("UTF-8"));
} catch (UnsupportedCharsetException e) {
    // How can this happen?!
}

With Charsets, you can simply write:

myString.getBytes(Charsets.UTF_8);

Note about Java >= 7 Link to heading

Note that Charsets should be used only when working with Java 6 - which might sounds strange in 2015, but believe me, it’s not that uncommon in the enterprise world… Anyway, with Java 7 and above, you can use the built-in class StandardCharsets, which has exactly the same API and behavior.