-->

FileChannel transferFrom的评论解释(FileChannel transfer

2019-10-18 07:04发布

我读过的评论FileChanneltransferFrom

 * <p> This method is potentially much more efficient than a simple loop
 * that reads from the source channel and writes to this channel.  Many
 * operating systems can transfer bytes directly from the source channel
 * into the filesystem cache without actually copying them.  </p>

这是什么意思

Many operating systems can transfer bytes directly from the source channel
into the filesystem cache without actually copying them.

如果我从一个通道读取,然后将其写入到另一个通道,不会将其转移字节到缓存?

Answer 1:

是,如果使用一个循环,并从源信道读入字节缓冲区,然后写字节缓冲区到FileChannel字节/数据将在在写结束时的文件系统高速缓存中。 他们也被复制到Java字节缓冲区,并可能已经从内核拷贝,应用程序内存(或“C堆”),然后到JVM堆(在最坏的情况下)。

如果源通道是兼容的,则OS 可能能够避免进入副本JVM堆,甚至传出内核的全部,而是直接,比如说,从源文件页面做复制到目标文件页面。

如果你会看到任何性能真正的改善将是非常JVM版本,操作系统和文件系统的依赖。 我不希望它永远表现较差的是一个Java编码的循环。

抢。



文章来源: FileChannel transferFrom's comments explanation