在Android下没有可用的黑客。
但是有一些辅助程序和片段使得C-Java绑定mmap文件变得更加容易:
看一下util-mmap的实际应用,非常简单:
public class MMapTesting {
public static void main(String[] args) throws IOException {
File file = new File("test");
MMapBuffer buffer = new MMapBuffer(file, 0, 1000, FileChannel.MapMode.READ_WRITE, ByteOrder.BIG_ENDIAN)) {
buffer.memory().intArray(0, 100).set(2, 234);
// calls unmap under the hood
buffer.close();
// here we call unmap automatically at the end of this try-resource block
try (MMapBuffer buffer = new MMapBuffer(file, FileChannel.MapMode.READ_WRITE, ByteOrder.BIG_ENDIAN)) {
System.out.println("length: " + buffer.memory().length());
IntArray arr = buffer.memory().intArray(0, buffer.memory().length() / 8);
// prints 234
System.out.println(arr.get(2));
}
}
}
来自Android开发者网站:
直接字节缓冲区,其内容是文件的内存映射区域。
通过FileChannel.map方法创建映射字节缓冲区。该类使用特定于内存映射文件区域的操作扩展了ByteBuffer类。
映射字节缓冲区及其表示的文件映射将保持有效,直到垃圾回收器回收缓冲区本身。
映射字节缓冲区的内容可以随时更改,例如如果程序或其他程序更改了映射文件的相应区域的内容。是否发生此类更改以及何时发生更改取决于操作系统,因此未指定。
就我从这段文字中所理解的而言,使用Android Java SDK无法取消映射MappedByteBuffer。只能使用NDK,正如你所说的那样。