Skip to content

Commit fd48c7c

Browse files
committed
[CODEC-168] Reported by Daniel Cassidy: Add DigestUtils.updateDigest(MessageDigest, InputStream). No need for additional tests as the method is used by Codec itself.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1462710 13f79535-47bb-0310-9956-ffa450edef68
1 parent 17f61c8 commit fd48c7c

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The <action> type attribute can be add,update,fix,remove.
4848
</release>
4949
-->
5050
<release version="1.8" date="TBA" description="Feature and fix release.">
51+
<action dev="ggregory" type="add" issue="CODEC-168" due-to="Daniel Cassidy">Add DigestUtils.updateDigest(MessageDigest, InputStream).</action>
5152
<action dev="julius" type="add" issue="CODEC-167">Add JUnit to test our decode with pad character in the middle.</action>
5253
<action dev="ggregory" type="add" issue="CODEC-161" due-to="crice">Add Match Rating Approach (MRA) phonetic algorithm encoder.</action>
5354
<action dev="ggregory" type="fix" issue="CODEC-163" due-to="leo141">ColognePhonetic encoder unnecessarily creates many char arrays on every loop run.</action>

src/main/java/org/apache/commons/codec/digest/DigestUtils.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,7 @@ public class DigestUtils {
4646
* On error reading from the stream
4747
*/
4848
private static byte[] digest(final MessageDigest digest, final InputStream data) throws IOException {
49-
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
50-
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
51-
52-
while (read > -1) {
53-
digest.update(buffer, 0, read);
54-
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
55-
}
56-
57-
return digest.digest();
49+
return updateDigest(digest, data).digest();
5850
}
5951

6052
/**
@@ -784,6 +776,30 @@ public static MessageDigest updateDigest(final MessageDigest messageDigest, fina
784776
return messageDigest;
785777
}
786778

779+
/**
780+
* Reads through an InputStream and updates the digest for the data
781+
*
782+
* @param digest
783+
* The MessageDigest to use (e.g. MD5)
784+
* @param data
785+
* Data to digest
786+
* @return MD5 digest
787+
* @throws IOException
788+
* On error reading from the stream
789+
* @since 1.8
790+
*/
791+
public static MessageDigest updateDigest(final MessageDigest digest, final InputStream data) throws IOException {
792+
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
793+
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
794+
795+
while (read > -1) {
796+
digest.update(buffer, 0, read);
797+
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
798+
}
799+
800+
return digest;
801+
}
802+
787803
/**
788804
* Updates the given {@link MessageDigest}.
789805
*

0 commit comments

Comments
 (0)