最近在學(xué)習(xí)NIO 時(shí)遇到一個(gè)問題藕赞,使用transferTo()方法和transferFrom()方法做零拷貝復(fù)制文件時(shí)數(shù)據(jù)丟失缝左。
我想要完成這樣一個(gè)測(cè)試亿遂,將d盤中一個(gè)centos鏡像文件(CentOS-8.1.1911-x86_64-dvd1.iso)通過網(wǎng)絡(luò)通信和NIO傳輸?shù)絛盤的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();
}
- 服務(wù)端
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("寫入時(shí)間是:" + (System.currentTimeMillis() - start));
}
} catch (IOException e) {
e.printStackTrace();
}
鏡像文件的大小在7G左右渺杉,但是傳輸后的文件大小只有8M蛇数。查看了方法的API注釋如下
/* An attempt is made to read up to <tt>count</tt> bytes starting at
* the given <tt>position</tt> 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
* <tt>count</tt> bytes starting at the given <tt>position</tt>, or if the
* target channel is non-blocking and it has fewer than <tt>count</tt>
* bytes free in its output buffer.
*/
public abstract long transferTo(long position, long count,
WritableByteChannel target)
throws IOException;
大概的意思是此方法嘗試將參數(shù)count長(zhǎng)度的字節(jié)從參數(shù)position的位置開始 從此channel連接的文件中寫入目標(biāo)channel中,
但是引用此方法并不一定會(huì)傳輸所有需要的字節(jié)數(shù)是越,取決于這些通道的性質(zhì)和狀態(tài)耳舅。如果當(dāng)前管道連接的文件包含的數(shù)據(jù)小于參數(shù)給出的大小或者目標(biāo)管道是非阻塞的并且目標(biāo)管道的輸出緩沖區(qū)的可用空間不足,會(huì)導(dǎo)致小于參數(shù)count的數(shù)據(jù)傳輸倚评。
這個(gè)注釋是很坑人的浦徊,看了一直在思考是否目標(biāo)管道有什么問題,然后找了好久都沒找到天梧,實(shí)際上并非如此盔性。
查看transferTo()方法的源碼,在傳輸數(shù)據(jù)之前會(huì)做一系列驗(yàn)證呢岗,其中有一個(gè)驗(yàn)證是這樣的
int var8 = (int)Math.min(var3, 2147483647L);
其中var是我們傳入的參數(shù)count冕香,而var8是該方法內(nèi)部實(shí)際運(yùn)輸?shù)淖止?jié)長(zhǎng)度,當(dāng)參數(shù)長(zhǎng)度大于2147483647時(shí)后豫,取這個(gè)數(shù)悉尾,轉(zhuǎn)換完之后大概在2G左右,所有終于找到了問題原因硬贯。而解決這個(gè)問題的方法就是循環(huán)傳輸數(shù)據(jù)
代碼如下:
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("客戶端文件傳輸時(shí)間:" + (System.currentTimeMillis() - start));
------------------------------------------分割線---------------------------------------------------------------
- 追加一個(gè)問題焕襟,我上面寫道我通過SocketChannel傳輸后的數(shù)據(jù)只有8M左右,但是上面的源碼中最終看到是限制是2G饭豹,這是為啥呢鸵赖?
后來繼續(xù)看了一下源碼,發(fā)現(xiàn)我transferTo到SocketChannel這個(gè)類的對(duì)象中拄衰,最終調(diào)用的方式是下面這個(gè)
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); 這一句代碼,將數(shù)據(jù)大小再一次限制了翘悉,所有最終單次傳輸?shù)臄?shù)據(jù)是8M左右茫打。而解決方案與上面相同,循環(huán)寫入數(shù)據(jù)就行了。問題到此完美解決了老赤,終于可以安心的睡個(gè)好覺了轮洋。。