Java NIO PDF Print E-mail

by Ron Hitchens
Published by O'Reilly

Review by Ben Galbraith

When I first heard about the java.nio package and its neat high-performance I/O features, I was pretty excited. For the unfamiliar among our readers, J2SE 1.4 introduced new classes for file, network, and memory I/O that are much more efficient (and fully featured) than the "classic" Java classes for these purposes (e.g., File, Socket, etc). The new classes are stored in the java.nio package. The java.nio classes are not intended to replace java.io; they involve advanced concepts that can introduce considerable complexity and potentially serious memory leaks into your application. Rather, think of them as valuable tools for those instances where you are writing an unusually I/O intense application that hits Java-related I/O bottlenecks.

These new java.nio classes are closer to the metal than any standard Java I/O ever before, and as such, require greater familiarity with how modern operating systems deal with I/O. I admit that I didn't have enough of a computer science background to readily understand the concepts involved with java.nio by simply reading the JavaDocs. I therefore had high hopes that Java NIO would educate a back-water technology bumpkin such as myself on both the API and its underlying concepts. I was not disappointed!

In fact, I believe that Java NIO is an excellent example of how technical books should be written. The author begins with what he calls a "whirlwind review" of the I/O concepts involved. In reality, the review was quite good and prepared me more than adequately for what followed. What most stuck in my mind was the author's explanation of the memory "paging" paradigm that modern operating systems use. Having existed in interpreter hell with VB, scripter land with Perl, and high-level bliss with Java for the bulk of my professional career, I had no idea how memory management worked behind-the-scenes, and learning about paging was quite informative.

After the concept review, the author began to crack open the API. He started with the NIO Buffer classes, explaining how the are manipulated, and then described the primitive-based buffers as well as the ByteBuffer and MappedByteBuffer. All of the explanations were completely lucid and fun to read. There were also plenty of code examples; never too short, and rarely long enough to be obnoxious. The buffer section also included discussions of direct and non-direct buffers, and included some thought-provoking advice on byte-ordering (i.e., be aware of the native ordering and plan accordingly for efficiency gains).

The author then built on buffers to cover the other major I/O classes: file channels, socket channels, and pipes. Each of these subjects was treated with the same quality as buffers. I was especially impressed that the author went to the trouble to explain why pipes might be useful, rather than just discussing how they are used. The author also included a comprehensive section on selectors, which are the key mechanism for implementing scalable, multiplexed applications as they deal with the concurrency issues.

The java.nio classes also include the J2SE standard regular expression engine, and although a bit of a non-sequitar for the other chapters, the author opted to cover it. While the author does not include a primer on regular expressions, I can't really fault him for it because this intended book's audience is highly likely to have been exposed to them before. Rather, he focuses on the Java implementation of regex. I should note, however, that the other does cover advanced regex topics that he did not need to, such as expression groups. Having recently read a lot of authors that seem to look for excuses to dodge complex material, I was impressed that this author didn't waste space on the intuitive regex features but dove straight into the complex regex stuff.

The book wraps up with the new J2SE 1.4 way to deal with encoding: character sets. I haven't read this section yet, so I can't comment on it.

In conclusion, Java NIO is a superb reference for intermediate- to advanced-level developers who are seeking to learn more about the java.nio classes. The author is clearly qualified, the writing style is clear and fun, the examples and explanations clear, and enough techie trivia to keep the geekiest among us content. Clearly, a good buy.

Ben Galbraith
ben [shift-2] galbraiths [point] org

 
< Prev   Next >
Colocation provided by Tier Four