【发布时间】:2014-12-04 19:06:39
【问题描述】:
我有一个文件比较的布尔方法。它是 bb 的一部分并以相等的方式检查。 如果部分相等 - 获取下一个块。如果位置(点)> 文件大小并且所有块都相等 - 返回 true。 处理小文件 (10MB),但处理大文件时会遇到麻烦。
private static boolean getFiles(File file1, File file2) throws IOException {
FileChannel channel1 = new FileInputStream(file1).getChannel();
FileChannel channel2 = new FileInputStream(file2).getChannel();
int SIZE;
MappedByteBuffer buffer1, buffer2;
for (int point = 0; point < channel1.size(); point += SIZE) {
SIZE = (int) Math.min((4096*1024), channel1.size() - point);
buffer1 = channel1.map(FileChannel.MapMode.READ_ONLY, point, SIZE);
buffer2 = channel2.map(FileChannel.MapMode.READ_ONLY, point, SIZE);
if (!buffer1.equals(buffer2)) {
return false;
}
}
return true;
}
如何修改?改变块的大小?
【问题讨论】:
-
我会尝试更小的块,可能在 16-128k 左右的范围内......我想不出更多的尝试:)
-
问题是
MappedByteBuffer没有释放资源的方法,相反,它依赖于可能异步发生并延迟的终结,因此在循环中分配缓冲区时可能会遇到OutOfMemoryError即使旧缓冲区超出范围。我认为这是 Java API 的设计错误,但偶尔调用System.gc()可能会解决问题。
标签: java equals memory-mapped-files bytebuffer