0

我正在尝试通过使用 MappedByteBuffer 对文件进行内存映射来为不同大小的位图设置交换文件。我希望能够在这个文件中移动内存,所以可能有两个子问题:

  • 有没有办法告诉 ByteBuffer 将内存块移动到另一个索引,或者
  • 是否可以直接访问原始映射内存来自己移动。
4

1 回答 1

0

好的,我找到了一种方法,方法是使用 MappedByteBuffer 映射的文件通道在其内部传输内存。

RandomAccessFile raFile = new RandomAccessFile(MyFile, "rw");
FileChannel fileChannel = raFile.getChannel();
fileChannel.position(srcPosition);
fileChannel.transferFrom(fileChannel, dstPosition, count);

这比从 MappedByteBuffer 中按 int 读取 int 并将其写入复制指向同一文件中的另一个位置的速度大约快 200 倍(准确地说:读取数据部分的 1/100 时快两倍)。

于 2011-10-06T13:54:42.420 回答