如果使用 FileChannel 调用,transferFrom 和 transferTo 是否传输所有字节?
Are transferFrom and transferTo transfer all bytes if invoked with FileChannel?
一些互联网文档向我展示了示例,
FileChannel target;
FileChannel source;
target.trasferFrom(source, 0, source.size()); // done!
或
FileChannel source;
FileChannel target;
source.transferTo(0, source.size(), target); // done!
但是文档说这些方法实际上可以传输的比给定的要少。
是transferFrom
和transferTo
,如果source
或target
是FileChannel
的实例,转移所有给定的计数?
我不应该像这样循环吗?
for (long count = Files.size(source); count > 0L;) {
final long transferred = readable.transferTo(
readable.position(), count, writable);
readable.position(readable.position() + transferred);
count -= transferred;
}
for (long count = Files.size(source); count > 0L;) {
final long transferred = writable.transferFrom(
readable, writable.position(), count);
writable.position(writable.position() + transferred);
count -= transferred;
}
是的,文档是正确的,你必须循环。
一些互联网文档向我展示了示例,
FileChannel target;
FileChannel source;
target.trasferFrom(source, 0, source.size()); // done!
或
FileChannel source;
FileChannel target;
source.transferTo(0, source.size(), target); // done!
但是文档说这些方法实际上可以传输的比给定的要少。
是transferFrom
和transferTo
,如果source
或target
是FileChannel
的实例,转移所有给定的计数?
我不应该像这样循环吗?
for (long count = Files.size(source); count > 0L;) {
final long transferred = readable.transferTo(
readable.position(), count, writable);
readable.position(readable.position() + transferred);
count -= transferred;
}
for (long count = Files.size(source); count > 0L;) {
final long transferred = writable.transferFrom(
readable, writable.position(), count);
writable.position(writable.position() + transferred);
count -= transferred;
}
是的,文档是正确的,你必须循环。