Java NIO
12
1.3 Getting to the Good Stuff
Most of the development effort that goes into operating systems is targeted at improving I/O
performance. Lots of very smart people toil very long hours perfecting techniques for
schlepping data back and forth. Operating-system vendors expend vast amounts of time and
money seeking a competitive advantage by beating the other guys in this or that published
benchmark.
Today's operating systems are modern marvels of software engineering (OK, some are more
marvelous than others), but how can the Java programmer take advantage of all this wizardry
and still remain platform-independent? Ah, yet another example of the TANSTAAFL
principle.
1
The JVM is a double-edged sword. It provides a uniform operating environment that shelters
the Java programmer from most of the annoying differences between operating-system
environments. This makes it faster and easier to write code because platform-specific
idiosyncrasies are mostly hidden. But cloaking the specifics of the operating system means
that the jazzy, wiz-bang stuff is invisible too.
What to do? If you're a developer, you could write some native code using the Java Native
Interface (JNI) to access the operating-system features directly. Doing so ties you to a specific
operating system (and maybe a specific version of that operating system) and exposes the
JVM to corruption or crashes if your native code is not 100% bug free. If you're an operating-
system vendor, you could write native code and ship it with your JVM implementation to
provide these features as a Java API. But doing so might violate the license you signed to
provide a conforming JVM. Sun took Microsoft to court about this over the JDirect package
which, of course, worked only on Microsoft systems. Or, as a last resort, you could turn to
another language to implement performance-critical applications.
The
java.nio
package provides new abstractions to address this problem. The Channel and
Selector classes in particular provide generic APIs to I/O services that were not reachable
prior to JDK 1.4. The TANSTAAFL principle still applies: you won't be able to access every
feature of every operating system, but these new classes provide a powerful new framework
that encompasses the high-performance I/O features commonly available on commercial
operating systems today. Additionally, a new Service Provider Interface (SPI) is provided in
java.nio.channels.spi
that allows you to plug in new types of channels and selectors
without violating compliance with the specifications.
With the addition of NIO, Java is ready for serious business, entertainment, scientific and
academic applications in which high-performance I/O is essential.
The JDK 1.4 release contains many other significant improvements in addition to NIO. As of
1.4, the Java platform has reached a high level of maturity, and there are few application areas
remaining that Java cannot tackle. A great guide to the full spectrum of JDK features in 1.4 is
Java In A Nutshell, Fourth Edition by David Flanagan (O'Reilly).
1
There Ain't No Such Thing As A Free Lunch.