From f6a6c6e577f34867c5f3c0235787bbe825f80e6b Mon Sep 17 00:00:00 2001 From: Hairong Kuang Date: Thu, 28 Oct 2010 18:49:39 +0000 Subject: [PATCH] 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 --- CHANGES.txt | 3 +++ src/java/org/apache/hadoop/io/MD5Hash.java | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 78f40c82e5..f2616e5db3 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -152,6 +152,9 @@ Trunk (unreleased changes) 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 HADOOP-6884. Add LOG.isDebugEnabled() guard for each LOG.debug(..). diff --git a/src/java/org/apache/hadoop/io/MD5Hash.java b/src/java/org/apache/hadoop/io/MD5Hash.java index 4e358fd9bb..f19f8db12c 100644 --- a/src/java/org/apache/hadoop/io/MD5Hash.java +++ b/src/java/org/apache/hadoop/io/MD5Hash.java @@ -94,11 +94,18 @@ public static MD5Hash digest(byte[] data) { 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. */ public static MD5Hash digest(InputStream in) throws IOException { 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; ) { 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. */ public static MD5Hash digest(byte[] data, int start, int len) { byte[] digest; - MessageDigest digester = DIGESTER_FACTORY.get(); + MessageDigest digester = getDigester(); digester.update(data, start, len); digest = digester.digest(); return new MD5Hash(digest);