2
我正在爲FileChannel s寫實用課程。我如何從FileChannel#transferFrom循環中斷?
以下方法看起來可能有效。
// tries to transfer as many bytes as specified
public static long transferTo(final FileChannel src, long position,
long count, final WritableByteChannel dst)
throws IOException {
long accumulated = 0L;
while (position < src.size()) {
final long transferred = src.transferTo(position, count, dst);
position += transferred;
count -= transferred;
accumulated += transferred;
}
return accumulated;
}
但是transferFrom
的版本有問題。
如果src
在count之前達到EOF,則循環可能無限生活。
有沒有可能的解決方案呢?