You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/11/09 16:03:00 UTC
(commons-io) 10/12: Add support to MessageDigestCalculatingInputStream for setting a consumer for ProxyInputStream.afterRead(int)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit f31b2f3c9b0dba00b2e2a1d0b56885f4ce0a9e9b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Nov 9 11:01:11 2024 -0500
Add support to MessageDigestCalculatingInputStream for setting a
consumer for
ProxyInputStream.afterRead(int)
---
src/changes/changes.xml | 1 +
.../input/MessageDigestCalculatingInputStream.java | 21 ++++++++++-----
.../MessageDigestCalculatingInputStreamTest.java | 30 ++++++++++++++++++++++
3 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6f623c115..a8c8b926f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -72,6 +72,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">Add support to ChecksumInputStream for setting a consumer for ProxyInputStream.afterRead(int).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add support to ThrottledInputStream for setting a consumer for ProxyInputStream.afterRead(int).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add support to ObservableInputStream for setting a consumer for ProxyInputStream.afterRead(int).</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add support to MessageDigestCalculatingInputStream for setting a consumer for ProxyInputStream.afterRead(int).</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 74 to 78 #670, #676, #679, #688.</action>
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons.bytebuddy.version from 1.15.1 to 1.15.10 #672, #673, #685, #686, #694, #696, #698.</action>
diff --git a/src/main/java/org/apache/commons/io/input/MessageDigestCalculatingInputStream.java b/src/main/java/org/apache/commons/io/input/MessageDigestCalculatingInputStream.java
index ba9aa9941..0413b4114 100644
--- a/src/main/java/org/apache/commons/io/input/MessageDigestCalculatingInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/MessageDigestCalculatingInputStream.java
@@ -21,10 +21,9 @@ import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
+import java.util.Arrays;
import java.util.Objects;
-import org.apache.commons.io.build.AbstractStreamBuilder;
-
/**
* Calculates a checksum using a {@link MessageDigest}, for example, a SHA-512 sum.
* <p>
@@ -57,12 +56,15 @@ public class MessageDigestCalculatingInputStream extends ObservableInputStream {
* .setMessageDigest("SHA-512")
* .get();}
* </pre>
+ * <p>
+ * <em>The MD5 cryptographic algorithm is weak and should not be used.</em>
+ * </p>
*
* @see #get()
* @since 2.12.0
*/
// @formatter:on
- public static class Builder extends AbstractStreamBuilder<MessageDigestCalculatingInputStream, Builder> {
+ public static class Builder extends AbstractBuilder<Builder> {
private MessageDigest messageDigest;
@@ -101,16 +103,16 @@ public class MessageDigestCalculatingInputStream extends ObservableInputStream {
* @throws IOException if an I/O error occurs.
* @see #getInputStream()
*/
- @SuppressWarnings("resource")
@Override
public MessageDigestCalculatingInputStream get() throws IOException {
- return new MessageDigestCalculatingInputStream(getInputStream(), messageDigest);
+ setObservers(Arrays.asList(new MessageDigestMaintainingObserver(messageDigest)));
+ return new MessageDigestCalculatingInputStream(this);
}
/**
* Sets the message digest.
* <p>
- * The MD5 cryptographic algorithm is weak and should not be used.
+ * <em>The MD5 cryptographic algorithm is weak and should not be used.</em>
* </p>
*
* @param messageDigest the message digest.
@@ -122,7 +124,7 @@ public class MessageDigestCalculatingInputStream extends ObservableInputStream {
/**
* Sets the name of the name of the message digest algorithm.
* <p>
- * The MD5 cryptographic algorithm is weak and should not be used.
+ * <em>The MD5 cryptographic algorithm is weak and should not be used.</em>
* </p>
*
* @param algorithm the name of the algorithm. See the MessageDigest section in the
@@ -197,6 +199,11 @@ public class MessageDigestCalculatingInputStream extends ObservableInputStream {
private final MessageDigest messageDigest;
+ private MessageDigestCalculatingInputStream(final Builder builder) throws IOException {
+ super(builder);
+ this.messageDigest = builder.messageDigest;
+ }
+
/**
* Constructs a new instance, which calculates a signature on the given stream, using a {@link MessageDigest} with the "MD5" algorithm.
* <p>
diff --git a/src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java b/src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
index 385a08b85..5226980fd 100644
--- a/src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
@@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream;
@@ -28,11 +29,14 @@ import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;
+import org.apache.commons.io.IOExceptionList;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.MessageDigestCalculatingInputStream.Builder;
+import org.apache.commons.io.test.CustomIOException;
import org.junit.jupiter.api.Test;
/**
@@ -50,6 +54,32 @@ public class MessageDigestCalculatingInputStreamTest {
return MessageDigestCalculatingInputStream.builder().setInputStream(origin).get();
}
+ @Test
+ public void testAfterReadConsumer() throws Exception {
+ final AtomicBoolean boolRef = new AtomicBoolean();
+ // @formatter:off
+ try (InputStream bounded = MessageDigestCalculatingInputStream.builder()
+ .setCharSequence("Hi")
+ .setAfterRead(i -> boolRef.set(true))
+ .get()) {
+ IOUtils.consume(bounded);
+ }
+ // @formatter:on
+ assertTrue(boolRef.get());
+ // Throwing
+ final String message = "test exception message";
+ // @formatter:off
+ try (InputStream bounded = MessageDigestCalculatingInputStream.builder()
+ .setCharSequence("Hi")
+ .setAfterRead(i -> {
+ throw new CustomIOException(message);
+ })
+ .get()) {
+ assertTrue(assertThrowsExactly(IOExceptionList.class, () -> IOUtils.consume(bounded)).getMessage().contains(message));
+ }
+ // @formatter:on
+ }
+
@SuppressWarnings("resource")
@Test
public void testAvailableAfterClose() throws Exception {