Java NIO Channel

A Channel is a conduit that tansport data efficiently between byte buffers and the entity on the other end of the channel (usually a file or socket).

Interfaces

  • Channel
    • InterruptibleChannel: most channels are interruptible
    • WritableByteChannel: Channel operate only on byte buffers.
      • GetheringByteChannel
    • ReadableByteChannel
      • ReadableByteChannel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public interface Channel {
	public boolean isOpen();
	public void close() throws IOException;
}

public interface ReadableByteChannel extends Channel {
	public int read(ByteBuffer dst) throws IOException;
}

public interface WritableByteChannel extends Channel {
	public int write(ByteBuffer src) throws IOException;
}

public interface ByteChannel
	extends ReadableByteChannel, WritableByteChannel{
}

public interface ScatteringByteChannel
	extends ReadableByteChannel {
	public long read(ByteBuffer[] dsts) throws IOException;
	public long read(ByteBuffer[] dsts, int offset, int length) throws IOException;
}

public interface GatheringByteChannel
	extends WritableByteChannel {
	public long write(ByteBuffer[] srcs) throws IOException;
	public long write(ByteBuffer[] scrs, int offset, int length) throws IOException;
}

Scatter/Gather refers to performing a single I/O operate across multiple buffers.

Channels

There are two types of channels:

  • file I/O: file
    • FileChannel
  • stream I/O: socket
    • SocketChannel
    • ServerSocketChannel
    • DatagramChannel

Channels can operate in blocking or nonblocking modes. Only stream-oriented channels, such as sockets and pipes , can be placed in nonblocking mode.