【发布时间】:2018-12-11 08:02:22
【问题描述】:
我正在编写为给定文件生成校验和的 Java 代码。我正在使用 Gogole 的 Guava 库进行哈希处理。这是代码 -
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
private HashCode doHash(File file) throws IOException {
HashFunction hc = Hashing.murmur3_128();
HashCode hsCode = hc.newHasher().putBytes(com.google.common.io.Files.asByteSource(file).read()).hash();
return hsCode;
}
我为一个大小为 2.8GB 的文件运行此代码。它抛出了以下错误 -
Exception in thread "main" java.lang.OutOfMemoryError: 2945332859 bytes is too large to fit in a byte array
at com.google.common.io.ByteStreams.toByteArray(ByteStreams.java:232)
at com.google.common.io.Files$FileByteSource.read(Files.java:154)
...
我可以在这里使用另一种数据结构吗?或者我应该寻找另一种策略将文件提供给散列函数?
【问题讨论】:
-
感谢@ScaryWombat。但我正在寻找一种解决方案,如果可能的话,我可以继续使用 Guava 哈希 API。
标签: java arrays hash guava checksum