HADOOP-7009. MD5Hash provides a public factory method that creates an instance of MessageDigest. Contributed by Hairong Kuang.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1028427 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hairong Kuang 2010-10-28 18:49:39 +00:00
parent 4be1688c53
commit f6a6c6e577
2 changed files with 12 additions and 2 deletions

View File

@ -152,6 +152,9 @@ Trunk (unreleased changes)
HADOOP-7010. Typo in FileSystem.java. (Jingguo Yao via eli) HADOOP-7010. Typo in FileSystem.java. (Jingguo Yao via eli)
HADOOP-7009. MD5Hash provides a public factory method that creates an
instance of thread local MessageDigest. (hairong)
OPTIMIZATIONS OPTIMIZATIONS
HADOOP-6884. Add LOG.isDebugEnabled() guard for each LOG.debug(..). HADOOP-6884. Add LOG.isDebugEnabled() guard for each LOG.debug(..).

View File

@ -94,11 +94,18 @@ public static MD5Hash digest(byte[] data) {
return digest(data, 0, data.length); return digest(data, 0, data.length);
} }
/**
* Create a thread local MD5 digester
*/
public static MessageDigest getDigester() {
return DIGESTER_FACTORY.get();
}
/** Construct a hash value for the content from the InputStream. */ /** Construct a hash value for the content from the InputStream. */
public static MD5Hash digest(InputStream in) throws IOException { public static MD5Hash digest(InputStream in) throws IOException {
final byte[] buffer = new byte[4*1024]; final byte[] buffer = new byte[4*1024];
final MessageDigest digester = DIGESTER_FACTORY.get(); final MessageDigest digester = getDigester();
for(int n; (n = in.read(buffer)) != -1; ) { for(int n; (n = in.read(buffer)) != -1; ) {
digester.update(buffer, 0, n); digester.update(buffer, 0, n);
} }
@ -109,7 +116,7 @@ public static MD5Hash digest(InputStream in) throws IOException {
/** Construct a hash value for a byte array. */ /** Construct a hash value for a byte array. */
public static MD5Hash digest(byte[] data, int start, int len) { public static MD5Hash digest(byte[] data, int start, int len) {
byte[] digest; byte[] digest;
MessageDigest digester = DIGESTER_FACTORY.get(); MessageDigest digester = getDigester();
digester.update(data, start, len); digester.update(data, start, len);
digest = digester.digest(); digest = digester.digest();
return new MD5Hash(digest); return new MD5Hash(digest);