Skip to content

Commit 61db8e9

Browse files
committed
DigestUtils processes InputStream with buffered read instead of full copy
Issue: SPR-14427
1 parent 2bf9bc3 commit 61db8e9

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

spring-core/src/main/java/org/springframework/util/DigestUtils.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323

2424
/**
2525
* Miscellaneous methods for calculating digests.
26+
*
2627
* <p>Mainly for internal use within the framework; consider
2728
* <a href="http://commons.apache.org/codec/">Apache Commons Codec</a>
2829
* for a more comprehensive suite of digest utilities.
2930
*
3031
* @author Arjen Poutsma
32+
* @author Juergen Hoeller
3133
* @author Craig Andrews
3234
* @since 3.0
3335
*/
@@ -49,8 +51,8 @@ public static byte[] md5Digest(byte[] bytes) {
4951
}
5052

5153
/**
52-
* Calculate the MD5 digest of the given InputStream.
53-
* @param inputStream the inputStream to calculate the digest over
54+
* Calculate the MD5 digest of the given stream.
55+
* @param inputStream the InputStream to calculate the digest over
5456
* @return the digest
5557
* @since 4.2
5658
*/
@@ -59,8 +61,7 @@ public static byte[] md5Digest(InputStream inputStream) throws IOException {
5961
}
6062

6163
/**
62-
* Return a hexadecimal string representation of the MD5 digest of the given
63-
* bytes.
64+
* Return a hexadecimal string representation of the MD5 digest of the given bytes.
6465
* @param bytes the bytes to calculate the digest over
6566
* @return a hexadecimal digest string
6667
*/
@@ -69,9 +70,8 @@ public static String md5DigestAsHex(byte[] bytes) {
6970
}
7071

7172
/**
72-
* Return a hexadecimal string representation of the MD5 digest of the given
73-
* inputStream.
74-
* @param inputStream the inputStream to calculate the digest over
73+
* Return a hexadecimal string representation of the MD5 digest of the given stream.
74+
* @param inputStream the InputStream to calculate the digest over
7575
* @return a hexadecimal digest string
7676
* @since 4.2
7777
*/
@@ -127,7 +127,12 @@ private static byte[] digest(String algorithm, InputStream inputStream) throws I
127127
return messageDigest.digest();
128128
}
129129
else {
130-
return messageDigest.digest(StreamUtils.copyToByteArray(inputStream));
130+
final byte[] buffer = new byte[StreamUtils.BUFFER_SIZE];
131+
int bytesRead = -1;
132+
while ((bytesRead = inputStream.read(buffer)) != -1) {
133+
messageDigest.update(buffer, 0, bytesRead);
134+
}
135+
return messageDigest.digest();
131136
}
132137
}
133138

spring-core/src/test/java/org/springframework/util/DigestUtilsTests.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.util;
1818

19+
import java.io.ByteArrayInputStream;
20+
import java.io.IOException;
1921
import java.io.UnsupportedEncodingException;
2022

2123
import org.junit.Before;
@@ -25,6 +27,7 @@
2527

2628
/**
2729
* @author Arjen Poutsma
30+
* @author Juergen Hoeller
2831
*/
2932
public class DigestUtilsTests {
3033

@@ -38,24 +41,39 @@ public void createBytes() throws UnsupportedEncodingException {
3841

3942

4043
@Test
41-
public void md5() {
42-
byte[] result = DigestUtils.md5Digest(bytes);
44+
public void md5() throws IOException {
4345
byte[] expected = new byte[]
4446
{-0x4f, 0xa, -0x73, -0x4f, 0x64, -0x20, 0x75, 0x41, 0x5, -0x49, -0x57, -0x65, -0x19, 0x2e, 0x3f, -0x1b};
47+
48+
byte[] result = DigestUtils.md5Digest(bytes);
49+
assertArrayEquals("Invalid hash", expected, result);
50+
51+
result = DigestUtils.md5Digest(new ByteArrayInputStream(bytes));
4552
assertArrayEquals("Invalid hash", expected, result);
4653
}
4754

4855
@Test
49-
public void md5Hex() throws UnsupportedEncodingException {
56+
public void md5Hex() throws IOException {
57+
String expected = "b10a8db164e0754105b7a99be72e3fe5";
58+
5059
String hash = DigestUtils.md5DigestAsHex(bytes);
51-
assertEquals("Invalid hash", "b10a8db164e0754105b7a99be72e3fe5", hash);
60+
assertEquals("Invalid hash", expected, hash);
61+
62+
hash = DigestUtils.md5DigestAsHex(new ByteArrayInputStream(bytes));
63+
assertEquals("Invalid hash", expected, hash);
5264
}
5365

5466
@Test
55-
public void md5StringBuilder() throws UnsupportedEncodingException {
67+
public void md5StringBuilder() throws IOException {
68+
String expected = "b10a8db164e0754105b7a99be72e3fe5";
69+
5670
StringBuilder builder = new StringBuilder();
5771
DigestUtils.appendMd5DigestAsHex(bytes, builder);
58-
assertEquals("Invalid hash", "b10a8db164e0754105b7a99be72e3fe5", builder.toString());
72+
assertEquals("Invalid hash", expected, builder.toString());
73+
74+
builder = new StringBuilder();
75+
DigestUtils.appendMd5DigestAsHex(new ByteArrayInputStream(bytes), builder);
76+
assertEquals("Invalid hash", expected, builder.toString());
5977
}
6078

6179
}

0 commit comments

Comments
 (0)