Wednesday, July 6, 2011

Groovy One-Liner for Viewing Available Charsets

I've stated several times on this blog that I particularly like Groovy as a scripting language in a Java development environment. Groovy is often the easiest approach to learning more about the available Java libraries and the Java execution environment (JRE). I have posted several examples of this in posts such as Groovy: JVM-based Scripting with Less Ceremony, Ten Groovy One Liners to Impress Your Friends, and Ten More Groovy One-Liners. In this post, I look at another one-liner for determining available character set support.

There are actually two ways to find "every charset for which support is available in the current Java virtual machine" (quote source) using a single line of Groovy script. One single-line approach uses a Groovy-specific class while the other single-line approach uses a standard Java class. I'll demonstrate both examples.

The Java Approach

One approach to determining a JVM's supported character sets with a single line of Groovy is to use the Java NIO class provided for that purpose. The following line of Groovy code uses Charset.availableCharsets() to list all character sets supported on the current JVM. The entire Groovy code looks like this:

java.nio.charset.Charset.availableCharsets().each{println it}

As is the case with most one-liners, this one is short enough that I can easily try it out without an explicit script file by simply passing this command to the groovy launcher with the -e option. This is demonstrated in the next screen snapshot.

The Groovy Approach

Groovy provides a class called CharsetToolkit that can also be used in a single line of Groovy to extract the supported character sets on the current JVM. Here is the code using that class and its getAvailableCharsets() method.

CharsetToolkit.availableCharsets.each{println it}

I again show this as used with the groovy launcher and its -e option.

Although both of the above approaches do the job of displaying the JVM's supported character sets, I slightly prefer the Groovier approach because it uses Groovy's property support on the method named getAvailableCharsets and does not require an explicit import or scoping. These advantages make it slightly more concise with no loss of readability.

Determining the JVM's Default Charset

Both the Java class Charset and the Groovy class CharsetToolkit also make it easy in a single line of Groovy script to determine the default character set for the given JVM. This is done via Charset.defaultCharset() or CharsetToolkit.getDefaultSystemCharset(). The "full" script for each is shown in the next two single-line code listings.

println java.nio.charset.Charset.defaultCharset()

println CharsetToolkit.defaultSystemCharset

The Groovy approach is again shorter, but not by much. The output of running both of these via the groovy launcher and its -e option are shown in the next single screen snapshot.


Acquiring the available character sets supported by a particular JVM as well as determining the default character set supported by a particular JVM are easily accomplished with a single line of Groovy script using either the Java Charset class or the Groovy CharsetToolkit class. This is another example of how Groovy is an ideal scripting language for a Java development environment and working with the JVM.

No comments: