channel java_Java NIO 的前生今世 之二 NIO Channel 小结

Java NIO Channel

通常来说, 所有的 NIO 的 I/O 操作都是从 Channel 开始的. 一个 channel 类似于一个 stream.

java Stream 和 NIO Channel 对比

我们可以在同一个 Channel 中执行读和写操作, 然而同一个 Stream 仅仅支持读或写.

Channel 可以异步地读写, 而 Stream 是阻塞的同步读写.

Channel 总是从 Buffer 中读取数据, 或将数据写入到 Buffer 中.

Channel 类型有:

FileChannel, 文件操作

DatagramChannel, UDP 操作

SocketChannel, TCP 操作

ServerSocketChannel, TCP 操作, 使用在服务器端.

这些通道涵盖了 UDP 和 TCP网络 IO以及文件 IO.

基本的 Channel 使用例子:

public static void main( String[] args ) throws Exception

{

RandomAccessFile aFile = new RandomAccessFile("/Users/xiongyongshun/settings.xml", "rw");

FileChannel inChannel = aFile.getChannel();

ByteBuffer buf = ByteBuffer.allocate(48);

int bytesRead = inChannel.read(buf);

while (bytesRead != -1) {

buf.flip();

while(buf.hasRemaining()){

System.out.print((char) buf.get());

}

buf.clear();

bytesRead = inChannel.read(buf);

}

aFile.close();

}

FileChannel

FileChannel 是操作文件的Channel, 我们可以通过 FileChannel 从一个文件中读取数据, 也可以将数据写入到文件中.

注意, FileChannel 不能设置为非阻塞模式.

打开 FileChannel

RandomAccessFile aFile = new RandomAccessFile("test.txt", "rw");

FileChannel inChannel = aFile.getChannel();

从 FileChannel 中读取数据

ByteBuffer buf = ByteBuffer.allocate(48);

int bytesRead = inChannel.read(buf);

写入数据

String newData = "New String to write to file..." + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);

buf.clear();

buf.put(newData.getBytes());

buf.flip();

while(buf.hasRemaining()) {

channel.write(buf);

}

关闭

当我们对 FileChannel 的操作完成后, 必须将其关闭

channel.close();

设置 position

long pos channel.position();

channel.position(pos +123);

文件大小

我们可以通过 channel.size()获取关联到这个 Channel 中的文件的大小. 注意, 这里返回的是文件的大小, 而不是 Channel 中剩余的元素个数.

截断文件

channel.truncate(1024);

将文件的大小截断为1024字节.

强制写入

我们可以强制将缓存的未写入的数据写入到文件中:

channel.force(true);

SocketChannel

SocketChannel 是一个客户端用来进行 TCP 连接的 Channel.

创建一个 SocketChannel 的方法有两种:

打开一个 SocketChannel, 然后将其连接到某个服务器中

当一个 ServerSocketChannel 接受到连接请求时, 会返回一个 SocketChannel 对象.

打开 SocketChannel

SocketChannel socketChannel = SocketChannel.open();

socketChannel.connect(new InetSocketAddress("http://example.com", 80));

关闭

socketChannel.close();

读取数据

ByteBuffer buf = ByteBuffer.allocate(48);

int bytesRead = socketChannel.read(buf);

如果 read()返回 -1, 那么表示连接中断了.

写入数据

String newData = "New String to write to file..." + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);

buf.clear();

buf.put(newData.getBytes());

buf.flip();

while(buf.hasRemaining()) {

channel.write(buf);

}

非阻塞模式

我们可以设置 SocketChannel 为异步模式, 这样我们的 connect, read, write 都是异步的了.

连接

socketChannel.configureBlocking(false);

socketChannel.connect(new InetSocketAddress("http://example.com", 80));

while(! socketChannel.finishConnect() ){

//wait, or do something else...

}

在异步模式中, 或许连接还没有建立, connect 方法就返回了, 因此我们需要检查当前是否是连接到了主机, 因此通过一个 while 循环来判断.

读写

在异步模式下, 读写的方式是一样的.

在读取时, 因为是异步的, 因此我们必须检查 read 的返回值, 来判断当前是否读取到了数据.

ServerSocketChannel

ServerSocketChannel 顾名思义, 是用在服务器为端的, 可以监听客户端的 TCP 连接, 例如:

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

serverSocketChannel.socket().bind(new InetSocketAddress(9999));

while(true){

SocketChannel socketChannel =

serverSocketChannel.accept();

//do something with socketChannel...

}

打开 关闭

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

serverSocketChannel.close();

监听连接

我们可以使用ServerSocketChannel.accept()方法来监听客户端的 TCP 连接请求, accept()方法会阻塞, 直到有连接到来, 当有连接时, 这个方法会返回一个 SocketChannel 对象:

while(true){

SocketChannel socketChannel =

serverSocketChannel.accept();

//do something with socketChannel...

}

非阻塞模式

在非阻塞模式下, accept()是非阻塞的, 因此如果此时没有连接到来, 那么 accept()方法会返回null:

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

serverSocketChannel.socket().bind(new InetSocketAddress(9999));

serverSocketChannel.configureBlocking(false);

while(true){

SocketChannel socketChannel =

serverSocketChannel.accept();

if(socketChannel != null){

//do something with socketChannel...

}

}

DatagramChannel

DatagramChannel 是用来处理 UDP 连接的.

打开

DatagramChannel channel = DatagramChannel.open();

channel.socket().bind(new InetSocketAddress(9999));

读取数据

ByteBuffer buf = ByteBuffer.allocate(48);

buf.clear();

channel.receive(buf);

发送数据

String newData = "New String to write to file..."

+ System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);

buf.clear();

buf.put(newData.getBytes());

buf.flip();

int bytesSent = channel.send(buf, new InetSocketAddress("example.com", 80));

连接到指定地址

因为 UDP 是非连接的, 因此这个的 connect 并不是向 TCP 一样真正意义上的连接, 而是它会讲 DatagramChannel 锁住, 因此我们仅仅可以从指定的地址中读取或写入数据.

channel.connect(new InetSocketAddress("example.com", 80));

本文由 yongshun 发表于个人博客, 采用署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议.

非商业转载请注明作者及出处. 商业转载请联系作者本人

Email: yongshun1228@gmail.com

本文标题为: Java NIO 的前生今世 之二 NIO Channel 小结

本文链接为: segmentfault.com/a/1190000006824107

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值