NIO05——通道之间的数据传输

 在 Java NIO 中,如果两个 Channel 中的一个是 FileChannel,那么这两个 Channel 可以直接传送数据。FileChannel 提供了一个 transferTo() 方法和一个 transferFrom() 方法可以做到这些。
 1、transferFrom()
  FileChannel.transferFrom() 方法可以从一个源 Channel 中向这个 FileChannel 中传送数据,示例如下:

RandomAccessFile fromFile = new RandomAccessFile("F:\\temp\\source.txt", "rw");
FileChannel fromChannel = fromFile.getChannel();

RandomAccessFile toFile = new RandomAccessFile("F:\\temp\\file.txt", "rw");
FileChannel toChannel = toFile.getChannel();

long position = 0;
long count = fromChannel.size();

toChannel.transferFrom(fromChannel, position, count);

fromFile.close();
toFile.close();

  入参中的 position 和 count,指定了从目的 Channel 的哪个位置开始(position)以及向这个 FileChannel 中最多传送的字节数(count)。如果目的 Channle 中没有这么多个字节,则不传送那么多个,即只传送 limit - position 个字节。
 此外要注意,在一些 SoketChannel 的实现中,SocketChannel 只会传输此时此刻其 Buffer 中已准备好的数据,此后出现在该 SocketChannel 中的那些有意义的数据则不会被传输。因此,SocketChannel 可能不会将请求的所有数据(count 个字节)全部传输到FileChannel中,而其实这些数据在 SocketChannel 中都有,只不过不是同时出现的。
 2、transferTo()
  transferTo() 方法可以从一个 FileChannel 中向其他类型的 Channel 中传送数据,示例如下:

RandomAccessFile fromFile = new RandomAccessFile("F:\\temp\\source.txt", "rw");
FileChannel fromChannel = fromFile.getChannel();

RandomAccessFile toFile = new RandomAccessFile("F:\\temp\\file.txt", "rw");
FileChannel toChannel = toFile.getChannel();

long position = 0;
long count = fromChannel.size();

fromChannel.transferTo(position, count, toChannel);

fromFile.close();
toFile.close();

  SocketChannel 在 transferTo() 方法中依然有上面提到的那个问题,SocketChannel 只能传送 Send Buffer 中的数据,而一旦 Send Buffer 满了,就会停止,导致后面的数据无法传输。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值