最近在学习NIO 时遇到一个问题,使用transferTo()方法和transferFrom()方法做零拷贝复制文件时数据丢失。
我想要完成这样一个测试,将d盘中一个centos镜像文件(CentOS-8.1.1911-x86_64-dvd1.iso)通过网络通信和NIO传输到d盘的server文件夹下,代码如下
- 客户端
//客户端代码
try {
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9999));
FileChannel fileChannel = FileChannel.open(Paths.get("D:\\vm\\centos\\CentOS-8.1.1911-x86_64-dvd1.iso"), StandardOpenOption.READ);
fileChannel.transferTo(0,fileChannel.size(),socketChannel);
fileChannel.close();
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
- 服务端
SocketChannel acceptChannel = null;
FileChannel fileChannel = null;
try {
ServerSocketChannel socketChannel = ServerSocketChannel.open();
socketChannel.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"),9999));
while (true) {
acceptChannel = socketChannel.accept();
long start = System.currentTimeMillis();
ByteBuffer buffer = ByteBuffer.allocate(1024);
fileChannel = FileChannel.open(Paths.get("d:/server/testt.iso"), StandardOpenOption.CREATE_NEW,StandardOpenOption.WRITE);
while(acceptChannel.read(buffer)!= -1) {
buffer.flip();
fileChannel.write(buffer);
buffer.re();
}
acceptChannel.close();
fileChannel.close();
System.out.println("写入时间是:" + (System.currentTimeMillis() - start));
}
} catch (IOException e) {
e.printStackTrace();
}
镜像文件的大小在7G左右,但是传输后的文件大小只有8M。查看了方法的API注释如下
/* An attempt is made to read up to count bytes starting at
* the given position in this channel's file and write them to the
* target channel. An invocation of this method may or may not transfer
* all of the requested bytes; whether or not it does so depends upon the
* natures and states of the channels. Fewer than the requested number of
* bytes are transferred if this channel's file contains fewer than
* count bytes starting at the given position, or if the
* target channel is non-blocking and it has fewer than count
* bytes free in its output buffer.
*/
public abstract long transferTo(long position, long count,
WritableByteChannel target)
throws IOException;
大概的意思是此方法尝试将参数count长度的字节从参数position的位置开始 从此channel连接的文件中写入目标channel中,
但是引用此方法并不一定会传输所有需要的字节数,取决于这些通道的性质和状态。如果当前管道连接的文件包含的数据小于参数给出的大小或者目标管道是非阻塞的并且目标管道的输出缓冲区的可用空间不足,会导致小于参数count的数据传输。
这个注释是很坑人的,看了一直在思考是否目标管道有什么问题,然后找了好久都没找到,实际上并非如此。
查看transferTo()方法的源码,在传输数据之前会做一系列验证,其中有一个验证是这样的
int var8 = (int)Math.min(var3, 2147483647L);
其中var是我们传入的参数count,而var8是该方法内部实际运输的字节长度,当参数长度大于2147483647时,取这个数,转换完之后大概在2G左右,所有终于找到了问题原因。而解决这个问题的方法就是循环传输数据
代码如下:
try {
long start = System.currentTimeMillis();
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9999));
FileChannel fileChannel = FileChannel.open(Paths.get("D:\\vm\\centos\\CentOS-8.1.1911-x86_64-dvd1.iso"), StandardOpenOption.READ);
System.out.println("文件大小" + fileChannel.size());
long size = fileChannel.size();
long postion = 0 ;
while (size > 0) {
long count = fileChannel.transferTo(postion, size, socketChannel);
postion += count;
size -= count;
}
fileChannel.close();
socketChannel.close();
System.out.println("客户端文件传输时间:" + (System.currentTimeMillis() - start));
------------------------------------------分割线---------------------------------------------------------------
- 追加一个问题,我上面写道我通过SocketChannel传输后的数据只有8M左右,但是上面的源码中最终看到是限制是2G,这是为啥呢?
后来继续看了一下源码,发现我transferTo到SocketChannel这个类的对象中,最终调用的方式是下面这个
private long transferToTrustedChannel(long var1, long var3, WritableByteChannel var5) throws IOException {
boolean var6 = var5 instanceof SelChImpl;
if (!(var5 instanceof FileChannelImpl) && !var6) {
return -4L;
} else {
long var7 = var3;
while(var7 > 0L) {
long var9 = Math.min(var7, 8388608L);
try {
MappedByteBuffer var11 = this.map(MapMode.READ_ONLY, var1, var9);
try {
int var12 = var5.write(var11);
assert var12 >= 0;
var7 -= (long)var12;
if (var6) {
break;
}
assert var12 > 0;
var1 += (long)var12;
} finally {
unmap(var11);
}
} catch (ClosedByInterruptException var20) {
assert !var5.isOpen();
try {
this.close();
} catch (Throwable var18) {
var20.addSuppressed(var18);
}
throw var20;
} catch (IOException var21) {
if (var7 != var3) {
break;
}
throw var21;
}
}
return var3 - var7;
}
}
与上面的代码类似,通过long var9 = Math.min(var7, 8388608L); 这一句代码,将数据大小再一次限制了,所有最终单次传输的数据是8M左右。而解决方案与上面相同,循环写入数据就行了。问题到此完美解决了,终于可以安心的睡个好觉了。。