Download (direct link):
Charsets. A charset is a named mapping between bytes and 16-bit Unicode characters. The names of these character sets are listed in the IANA charset registry (http://www.iana.org/assignments/character-sets).
Selectors. A Selector is a class that multiplexes a SelectableChannel. A SelectableChannel has a single subclass called AbstractSelec-tableChannel. The AbstractSelectableChannel has four direct subclasses: DatagramChannel, Pipe.SinkChannel, Pipe.SourceChannel, ServerSocketChannel, and SocketChannel. In network communications, a multiplexer is a device that can send several signals over a single line. The Selector class manages a set of one or more "selectable" channels and provides a single point of access to input from any of them.
Table 2.1 lists all the key classes and interfaces in the NIO packages.
Table 2.1 Key NIO Classes and Interfaces
PACKAGE CLASS/INTERFACE DESCRIPTION
java.nio Buffer Random access data container for temporary data storage and manipulation.
java.nio ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer Buffer that stores the specific data type represented in its name.
java.nio MappedByteBuffer A ByteBuffer whose content is a memory-mapped region of a file. These are created via the FileChannel.map() method.
java.nio ByteOrder A typesafe enumeration of byte orders: little_endian or BIG_ENDIAN.
java.nio.channels Channel An interface that represents an open or closed connection to an IO device.
java.nio.channels Channels A class that contains utility methods for working with channels.
NIO Performance and Pitfalls 19
Table 2.1 (Continued)
PACKAGE CLASS/INTERFACE DESCRIPTION
java.nio.channels FileChannel, Pipe, DatagramChannel, SocketChannel, ServerSocketChannel, SelectableChannel Specific channel types to work with specific devices like files and sockets. A DatagramChannel is a channel for working with DatagramSockets (sockets that use User Datagram Protocol, or UDP). A pipe performs communication between threads.
java.nio.channels FileLock A class that represents a lock on a region of a file. An object of this type is created by calling either lock() or tryLock() on a FileChannel.
java.nio.channels Selector, SelectionKey As stated before, a Selector is a class that multiplexes one or more SelectableChannels. A SelectionKey is used to specify specific operations to listen for (or select) on a specific SelectableChannel.
java.nio.charset Charset An abstract class that provides a mapping between 16-bit Unicode characters and sequences of bytes. The standard character sets are US-ASCII (7-bit ASCII), ISO-8859-1 (ISO Latin), UTF-8, UTF-16BE (big endian order), UTF-16LE (little endian order), UTF-16 (uses a byte order mark).
java.nio.charset CharsetEncoder, CharsetDecoder A CharsetEncoder is an engine that encodes (or transforms) a set of 16-bit Unicode characters into a specific sequence of bytes specified by the specific character set. A CharsetDecoder performs the opposite operation.
java.nio.charset CoderResult A class that reports the state of an encoder or decoder. It reports any of five states: error, malformed, underflow, overflow, or unmappable.
java.nio.charset CodingActionError A class that is a typesafe enumeration that specifies how an encoder or decoder should handle errors. It can be set to ignore, REPLACE, or REPORT.
20 Item 2
Unfortunately, we cannot demonstrate all of the functionality in the NIO package. Instead, we examine three examples: a canonical file copy, little endian byte operations, and non-blocking server IO.
Canonical File Copy
Our first example is a canonical file copy operation. The old way to implement the copy would be to loop while reading and writing from a fixed-size byte buffer until we had exhausted the bytes in the file. Listing 2.1 demonstrates that method.
01 02 package org.javapitfalls.item2;
03 import java.io.*;
04 import java.nio.*;
05 06 import java.nio.channels.*;
07 public class SlowFileCopy
09 public static void main(String args)
// 16 ...command line check omitted for brevity ...
19 long start = System.currentTimeMillis();
20 // open files
21 FilelnputStream fis =
22 new FileInputStream(args);
23 FileOutputStream fos =
24 25 new FileOutputStream(args);
26 int bufSize = 4096;
27 28 byte  buf = new byte[bufSize];
29 int cnt = 0;
30 while ((cnt = fis.read(buf)) >= 0)
31 32 fos.write(buf, 0, (int) cnt);
35 long end = System.currentTimeMillis();
36 System.out.println("Elapsed Time: " 2
+ end - start) + " milliseconds.");
37 } catch (Throwable t)
Listing 2.1 SlowFileCopy.java
NIO Performance and Pitfalls 21
In line 26 of Listing 2.1 we set the buffer size to be 4 KB. Obviously, given the simple space/time trade-off, we can increase the speed of this for large files by increasing the buffer size. A run of Listing 2.1 on a 27-MB file produces: