改进哈希例程

时间:2018-10-16 10:12:26

标签: java hash inputstream md5

我有以下(非常讨厌)代码段,该代码段在项目内容上生成了md5-hash:

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()返回一个byte[]

在我收到内容很大的物品之前,这实际上行得通。由于它是在多线程环境中使用的,因此通过多次保存内容会占用大量RAM。

我现在想将generateMD5Hash()与InputStream一起使用,以停止将所有内容加载到RAM中。问题在于,对于所有先前生成的哈希,即将到来的哈希必须与当前函数中的哈希相同。

有什么想法如何以适当的方式实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

也许您想要ByteArrayInputStream吗?

看看here

相关问题