The examples in the last chapter all read and wrote file channels.
However, just as streams aren’t limited to files, neither are channels. Like streams, channels can read and write network sockets, byte arrays, piped data from other threads, and more. The basic methods and patterns for reading and writing channels don’t change from one data source to the next. You drain data from buffers when writing and fill buffers with data when reading. You can also transfer data directly from one channel to another. However, some things do change from one channel to the next. For instance, some channels are read-only and some are write-only. Some scatter data to multiple targets while some gather data from multiple sources. In this chapter, we take up the details of the various channel classes found in the
Much of the channel functionality is abstracted into a series of different interfaces. Interfaces are used rather than abstract classes because there’s frequently a need to mix and match different components. Some channels can be read, some can be written, and some can be both read and written. Some channels are interruptible and some are not. Some channels scatter and some gather. In practice, these capabilities appear in almost any combination.
The key superinterface is
java.nio.channels.Channel. This interface defines the only two methods all channels implement,
isOpen( ) and
public boolean isOpen( ) ...