改进哈希例程
Improving a hashing routine
我有以下(非常讨厌的)代码片段,它在项目的内容上生成 md5 哈希:
protected String createHashFromContentNew() throws CrawlerException {
final StringBuilder builder = new StringBuilder();
if (getContent() != null) {
builder.append(new String(getContent()));
}
if (builder.length() == 0) {
throw new CrawlerException(hashErrorMessage("the content of this item is empty!"));
} else {
return MD5Utils.generateMD5Hash(builder.toString());
}
}
MD5Utils.generateMD5Hash(builder.toString());
函数也可以与 InputStream
一起使用。
getContent()
returns一个byte[]
.
这实际上工作正常,直到我得到包含大量内容的项目。由于这是在多线程环境中使用的,因此多次保存内容会占用大量 RAM。
我现在想将 generateMD5Hash()
与 InputStream 一起使用,以停止将所有内容加载到 RAM 中。问题是,对于所有先前生成的哈希值,输出哈希值必须与当前函数中的相同。
关于如何以适当的方式实现这一点有什么想法吗?
也许你想要 ByteArrayInputStream?
看看here.
我有以下(非常讨厌的)代码片段,它在项目的内容上生成 md5 哈希:
protected String createHashFromContentNew() throws CrawlerException {
final StringBuilder builder = new StringBuilder();
if (getContent() != null) {
builder.append(new String(getContent()));
}
if (builder.length() == 0) {
throw new CrawlerException(hashErrorMessage("the content of this item is empty!"));
} else {
return MD5Utils.generateMD5Hash(builder.toString());
}
}
MD5Utils.generateMD5Hash(builder.toString());
函数也可以与 InputStream
一起使用。
getContent()
returns一个byte[]
.
这实际上工作正常,直到我得到包含大量内容的项目。由于这是在多线程环境中使用的,因此多次保存内容会占用大量 RAM。
我现在想将 generateMD5Hash()
与 InputStream 一起使用,以停止将所有内容加载到 RAM 中。问题是,对于所有先前生成的哈希值,输出哈希值必须与当前函数中的相同。
关于如何以适当的方式实现这一点有什么想法吗?
也许你想要 ByteArrayInputStream?
看看here.