java boilerplate

Thursday, December 01, 2005

Unneccessary UnsupportedEncodingException

A number of charsets are guaranteed supported by any JVM implementation, such as UTF-8 and ISO-8859-1. However, when explicitly specifying character encoding, even with one of these guaranteed character sets, you still have to deal with an UnsupportedEncodingException which can't actually happen. For example: the String constructor with parameters (byte[] data, String encoding) or for example the InputStreamReader and its (InputStream stream, String encoding) constructor.

We propose fitting an overloaded copy of all constructors and methods that have a String encoding parameter, where this parameter is replaced with the BasicCharset enum, which contains an option for each guaranteed encoding.

old code:
     String string;
try {
//data is a byte array.
string = new String(data, "UTF-8");
} catch ( UnsupportedEncodingException e ) {
throw new InternalError("Can't happen.");
}


new code:
     String string = new String(data, BasicCharset.UTF8);


The new enum based methods and constructors will of course no longer throw UnsupportedEncodingException.

BACKWARDS COMPATIBILITY: No problems at all. The only change is an API one, and it only consists of new classes and functions. Old code will continue to work.

0 Comments:

Post a Comment

<< Home