Start saddling up for Mustang

Explore some of the new features in Java SE 6

Sun will release its newest Java platform -- also called Mustang -- later this year. Officially known as Java Platform, Standard Edition 6 (Java SE 6 -- read "Building and Strengthening the Java Brand" for the reason behind the name change), Mustang focuses on several major themes, such as compatibility and stability. For a complete list of themes, see Java Specification Request 270.

New features expected to be part of Mustang include (among others):

  • An API for accessing the compiler
  • Console input/output (I/O)
  • An API for working with splash screens
  • Numerous Java 2D performance improvements
  • XML Digital Signatures API
  • An API for interacting with the system tray
  • Partition-space methods in the java.io.File class
  • Java Database Connectivity (JDBC) 4.0
  • Common annotations
  • Scripting API
  • A streaming API for XML
  • The ability to sort and filter the contents of javax.swing.JTables
  • Programmatic access to network parameters (such as the broadcast address and the network mark)
  • The ability to conveniently print the contents of javax.swing.text.JTextComponents

Mustang has far too many new features to explore in one article, so, instead, I focus on a small subset of features. Specifically, I discuss console I/O, partition-space methods, and the APIs for working with splash screens and for interacting with the system tray. I also illustrate the new JDBC 4.0 automatic driver-loading feature.

Note: I originally created and tested this article's code using Java SE 6 version 1.6.0-rc (build 62). The underlying platform was Microsoft Windows ME. I subsequently updated this article to reflect version 1.6.0 (build 105), the December 11, 2006 release version of Java SE 6.

Console I/O

In May 1997, Sun received a request for enhancement (RFE) to improve console I/O. Specifically, the requester asked for a way to prompt users for a pass-phrase and to let users type that pass-phrase (of arbitrary length) without the characters being echoed to the console. The requester pointed out that the Abstract Window Toolkit's (AWT) setEchoChar() method is not usable because it relies on a GUI's availability, and many server-based operating systems don't use GUIs.

In late 2005, Sun responded to RFE #4050435 by adding the java.io.Console class to Mustang (build 57). That class provides methods for accessing the character-based console device associated with the current virtual machine. But before you can call those methods, you must obtain a Console object by invoking System's public static Console console() method. That method returns either a Console object for interacting with the console device or null if the console device does not exist—the console device does not exist if you redirect standard input or standard output (or both), for example. After invoking System.console() to return a Console object, the following code fragment determines whether the console device exists by testing the returned Console reference for null:

Console console = System.console ();
if (console == null)
{
    System.err.println ("Console not available");
    return;
}

Assuming that the console device is available, you can read passwords and lines of characters from the console input stream, and write characters to the console output stream. To read a password (without echoing password characters to the console output stream), you must invoke either one of Console's two readPassword() methods. These methods do not include any line-termination characters as part of the password; they will return null if the end of the console input stream is reached.

For example, you can call public char [] readPassword(String fmt, Object... args) to prompt the user to type a password, where the prompt is described by the java.util.Formatter-style format string (fmt) and its variable list of arguments (args), and then return the user's chosen password in a character array. The code fragment below repeatedly invokes readPassword(String fmt, Object... args) to prompt the user to type a password until the user enters a password that's at least MIN_PWD_LEN characters in length:

char [] pwd; do {

pwd = console.readPassword ("Enter password (%d characters min):", MIN_PWD_LEN); } while (pwd.length < MIN_PWD_LEN);

After the password is stored in pwd, it can be used as required. However, for security reasons, pwd should be zeroed out when the password is no longer needed.

In addition to the readPassword() methods, Console provides a pair of readLine() methods for conveniently reading an entire line of characters from the console and storing those characters (minus line-termination characters) in a String. These methods will return null if the end of the console input stream is reached.

1 2 3 4 5 Page
Join the discussion
Be the first to comment on this article. Our Commenting Policies