Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 6 of 7

Figure 9. Channel interface hierarchy
Additional interfaces depicted in Figure 9 add methods for reading (java.nio.channels.ReadableByteChannel), writing (java.nio.channels.WritableByteChannel), and scatter/gather operations. A gathering write can write data from several buffers to the channel in one contiguous operation. Conversely,
a scattering read can read data from the channel and deposit it into several buffers, filling each one in turn to its limit.
Scatter/gather operations have been used for years in high-performance I/O managers in Unix and Windows NT. SCSI controllers
also employ scatter/gather to improve overall performance. In Java, the channels quickly pass scatter/gather operations down
to the native operating system functions for vectored I/O. Scatter/gather operations also ease protocol or file handling,
particularly when you create fixed headers in some buffers and change only one or two variable data buffers. You can configure
channels for blocking or nonblocking operations. When blocking, calls to read, write, or other operations do not return until
the operation completes. Large writes over a slow socket can take a long time. In nonblocking mode, a call to write a large
buffer over a slow socket would just queue up the data (probably in an operating system buffer, though it could even queue
it up in a buffer on the network card) and return immediately. The thread can move on to other tasks while the operating system's
I/O manager finishes the job. Similarly, the operating system always buffers incoming data until the application asks for
it. When blocking, if the application asks for more data than the operating system has received, the call blocks until more
data comes in. In nonblocking mode, the application just gets whatever data is immediately available. The sample code included with this article uses each of the following three channels at various times:
ServerSocketChannelSocketChannelFileChanneljava.nio.channels.ServerSocketChannel plays the same role as java.net.ServerSocket. It creates a listening socket that accepts incoming connections. It cannot read or write. ServerSocketChannel.socket() provides access to the underlying ServerSocket, so you can still set socket options that way. As is the case with all the specific channels, you do not construct ServerSocketChannel instances directly. Instead, use the ServerSocketChannel.open() factory method.
ServerSocketChannel.accept() returns a java.nio.channel.SocketChannel for a newly connected client. (Note: Before Beta 3, accept() returned a java.net.Socket. Now the method returns a SocketChannel, which is less confusing for developers.) If the ServerSocketChannel is in blocking mode, accept() won't return until a connection request arrives. (There is an exception: you can set a socket timeout on the ServerSocket. In that case, accept() eventually throws a TimeoutException.) If the ServerSocketChannel is in nonblocking mode, accept() always returns immediately with either a Socket or null. In the sample code, AcceptThread constructs a ServerSocketChannel called ssc and binds it to a local TCP port: