Java NIO的通道类似流stream,但又有些不同:
- 既可以从通道中读取数据,又可以写数据到通道。但流的读写通常是单向的
- 通道可以异步地读写
- 通道中的数据总是先读到一个Buffer,或者总是要从一个Buffer中写入
- Channel用于在字节缓冲区和位于Channel另一侧的实体(通常是一个文件或套接字)之间有效的传输数据
java.nio.channels.Channel接口只声明了两个方法:
java.nio.channels
public interface Channel extends Closeable
- close() - 关闭通道
- isOpen() - 判断通道是否打开
通道在创建时被打开,一旦关闭通道,就不能重新打开了。
一些子接口/子类
ReadableByteChannel和WritableByteChannel, 以及ByteChannel
两个重要的子接口。
- ReadabelByteChannel接口声明了read(ByteBuffer dst)方法,把数据源的数据读入参数指定的ByteBuffer缓冲区中
- WritableByteBuffer接口声明了write(ByteBuffer src)方法,该方法把参数指定的ByteBuffer缓冲区中的数据写到数据汇中
ByteChannel接口扩展了这两个接口,因而同时支持读写操作。
分散和聚集 —— GatheringByteChannel和ScatteringByteChannel
- ScatteringByteChannel接口扩展了ReadableByteChannel接口,允许分散地读取数据。分散读取数据是指单个读取操作能填充多个缓冲区。ScatteringByteChannel接口声明了read(ByteBuffer[] dsts)方法,该方法把从数据源读取的数据依次填充到参数指定的ByteBuffer数值中
- GatheringByteChannel接口扩展了WritableByteChannel接口,允许集中地写入数据。集中写入数据是指单个写操作能把多个缓冲区的数据写到数据汇。GatheringByteChannel接口声明了write(ByteBuffer[] srcs)方法,该方法依次把参数指定的ByteBuffer数组的每个ByteBuffer中的数据写入数据汇。
- 分散读取和集中写数据能够进一步提高输入和输出操作的速度。
Scattering Reads:
ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray = {header, body};
channel.read(bufferArray);
注意buffer首先被插入到数组,然后再将数组作为channel.read()的输入参数。read()方法按照buffer在数组中的顺序将从channel中读取的数据写到buffer中,当一个buffer被写满后,channel紧接着向另一个buffer中写。
Scattering Reads在移动一下buffer前,必须填满当前的buffer。这意味着它不适用于动态消息。
Gathering Writes
ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray = {header, body};
channel.write(bufferArray);
buffer数组是write()方法的入参,write()方法会按照buffer在数组中的顺序,将数据写入到channel,注意只有position和limit之间的数据才会被写入。与scattering reads相反,gathering writes能较好地处理动态消息。
FileChannel类
FileChannel类代表与文件相连的通道。该类实现了ByteChannel, ScatteringByteChannel和GatheringByteChannel接口,支持读写操作,分散读操作和集中写操作。
FileChannel类没有提供公开的构造方法,因此用户不能使用new构造;不过,在FIleInputStream, FileOutputStream和RandomAccessFile类中提供了getChannel(0方法,该方法返回相应的FileChannel对象。
例如:
RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(48);
int byteRead = inChannel.read(buf);
while (byteRead != -1){
System.out.println("Read" + bytesRead);
buf.flip();
while(buf.hasRemaining()){
System.out.println((char)buf.get());
}
buf.clear();
bytesRead = inChannel.read(buf);
}
aFile.close();
SelectableChannel类
java.nio.channels
public abstract class SelectableChannel extends AbstractInterruptibleChannel implements Channel
SelectableChannel是一种支持阻塞I/O和非阻塞I/O的通道。
在非阻塞模式下,读写数据不会阻塞,并且SelectableChannel可以向Selector注册读就绪和写就绪等事件。
Selector负责监控这些事件,等到事件发生时,比如发生了读就绪事件,SelectableChannel就可以执行读操作了。
SelectableChannel的主要方法如下:
- confugureBlocking(boolean block) —— 当参数block为true时,表示设为阻塞模式;如果为false,表示设为非阻塞模式。默认情况下,采用阻塞模式,该方法返回对象本身的引用
- isBlocking —— 判断是否处于阻塞模式
- register(Selector sel, int ops) —— 向Selector注册事件
- register(Selector sel, int ops, Object attachment) —— 向Selector注册事件
- 上两个方法返回一个SelectionKey对象,用来跟踪被注册的事件。register()方法(第二个)还有一个Object类型的参数attachment,用于为SelectionKey关联一个附件,当被注册事件发生后,需要处理该事件时,可以从SelectionKey中获得这个附件,该附件可用来包含与处理这个事件相关的信息
SelectionKey key = socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
或者
MyHandler handler = new MyHandler();
SelectionKey key = socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, handler);
或者
SelectionKey key = socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
MyHandler handler = new MyHandler();
key.attach(handler);