Free Programming E-Books
Free download ebooks on computer and programming

Free Java ebook "The Sun Certified Java Developer Exam with J2SE 1.4" Sample Chapter

The SCJD Exam with J2SE 1.4
Free download Chapter 7: NIO (Java's new I/O package)
Download chapter

The Sun Certified Java Developer Exam with J2SE 1.4 takes a two-pronged approach, introducing the new features of J2SE 1.4 in the context of projects structured to mirror the Sun Certified Java Developer (SCJD) examination. Upon reading this book, you'll master the new features of J2SE 1.4 while preparing yourself for the exam.

To begin, you'll find discussion of the SCJD exam and the expectations of the programming assignment. You'll begin a sample project that develops throughout the book, each component building upon your knowledge of new J2SE 1.4 features.

Assuming a certain level of Java skill, this book excludes introductory Java concepts and gets right to what you need to know for the exam. You'll dig into the important J2SE 1.4 concepts that are vital for passing. If you intend to earn Java certification, The Sun Certified Java Developer Exam with J2SE 1.4 offers a supremely practical means for getting the jump on the latest Java features.

< < prev next > >

NIO

THROUGHOUT MANY REVISIONs to the Java platform, Java I/O has remained relatively unchanged. Since Java 1.0, I/O has been stream-based, which allowed for the input and output of byte data. Java 1.2 introduced the concept of Readers and Writers, introduced in JDK 1.1 allowed for easy output of more complex data types, such as Strings. Readers and Writers also introduced the ability to use various character encodings, thus allowing internationalization.

Java's I/O implementation has grown into a very robust method of data input and output. It does, however, have many limitations. First and foremost, Java has a relatively slow I/O framework. Even though most of the JVM's I/O implementation has migrated to a native code implementation, serious speed issues still plague the framework. On average, Java's I/O capabilities are much slower than those achieved with other natively compiled languages, such as C and C++.

Traditional Java I/O has also always blocked. As you recall from Chapter 4, to block means to wait for an event to occur and to retain any held resources until that event does occur. For example, say you have a thread that locks an object and opens a blocking network connection. This thread will be incapable of releasing the locked object until the network operation has completed. When a Java application attempts to access network or file data, blocking will occur until the data transfer is complete. Thus, even simple I/O operations must be accounted for in order to prevent slow performance or "hanging" while they read or write data. This can be particularly important when you are striving to maintain a responsive UI for an application.

NIO: The Future of Java I/O

The release of J2SE 1.4 introduces the first fundamental change to the Java I/O framework: NIO. NIO is Java's new I/O package, and it is a strong supplement to traditional I/O. The changes found in NIO are quite extensive. This chapter offers an introduction to the new features in the NIO implementation as they apply to the book's sample project.

As you would expect, NIO is an incredibly vast subject. This chapter offers an introduction to the new features and changes, but it only scratches the surface. For a complete listing of all the changes and new additions to the Java I/O framework, please consult Sun's API documentation at http://java.sun.com/j2se/1.4/docs/guide/nio/index.phpl.

Advantages of NIO

NIO offers several advantages over traditional I/O, the most dramatic of which is nonblocking I/O for network connections. In addition, there is direct memory mapping, ByteBuffer support, platform-specific optimizations, smooth integration between the various channels, the capability to read and write to a channel at the same time (as opposed to streams), and I/O and threads that can be interrupted. Finally, NIO is fully compatible with previous I/O implementations. Thus, using NIO will not break existing code.