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 2025/02/16 22:01:25 UTC

(commons-io) 02/03: Add BrokenOutputStream.BrokenOutputStream(Function, Throwable>)

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 025850a680eab989dcfc0536cfdecc00c57d566e
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 16 16:57:54 2025 -0500

    Add BrokenOutputStream.BrokenOutputStream(Function<String>, Throwable>)
    
    Deprecate <String> constructor
---
 src/changes/changes.xml                            |  1 +
 .../commons/io/output/BrokenOutputStream.java      | 37 ++++++++++++++++------
 .../commons/io/output/BrokenOutputStreamTest.java  |  7 ++--
 3 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2589d47d7..4390a3c18 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -67,6 +67,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add"                due-to="Gary Gregory">Add RandomAccessFileInputStream.copy(long, long, OutputStream).</action>
       <action dev="ggregory" type="add"                due-to="Gary Gregory">Add ProxyOutputStream.Builder.</action>
       <action dev="ggregory" type="add"                due-to="Gary Gregory">Add ByteOrderMark.matches(int[]).</action>
+      <action dev="ggregory" type="add"                due-to="Gary Gregory">Add BrokenOutputStream.BrokenOutputStream(Function&lt;String&gt;, Throwable>) and deprecate Supplier&lt;String&gt; constructor.</action>
       <!-- UPDATE -->
       <action dev="ggregory" type="update"             due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.17.0 #710, #715, #720.</action>
       <action dev="ggregory" type="update"             due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.1 to 1.18.0. #717.</action>
diff --git a/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java b/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java
index 5b1dad042..726cce7a8 100644
--- a/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java
@@ -18,6 +18,7 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.function.Function;
 import java.util.function.Supplier;
 
 import org.apache.commons.io.function.Erase;
@@ -42,13 +43,13 @@ public class BrokenOutputStream extends OutputStream {
     /**
      * Supplies the exception that is thrown by all methods of this class.
      */
-    private final Supplier<Throwable> exceptionSupplier;
+    private final Function<String, Throwable> exceptionFunction;
 
     /**
      * Constructs a new stream that always throws an {@link IOException}.
      */
     public BrokenOutputStream() {
-        this(() -> new IOException("Broken output stream"));
+        this(m -> new IOException("Broken output stream: " + m));
     }
 
     /**
@@ -59,7 +60,20 @@ public BrokenOutputStream() {
      */
     @Deprecated
     public BrokenOutputStream(final IOException exception) {
-        this(() -> exception);
+        this(m -> exception);
+    }
+
+    /**
+     * Constructs a new stream that always throws the supplied exception.
+     * <p>
+     * This class uses the invoked method name as the function input.
+     * </p>
+     *
+     * @param exceptionFunction a supplier for the IOException or RuntimeException to be thrown.
+     * @since 2.19.0
+     */
+    public BrokenOutputStream(final Function<String, Throwable> exceptionFunction) {
+        this.exceptionFunction = exceptionFunction;
     }
 
     /**
@@ -67,9 +81,11 @@ public BrokenOutputStream(final IOException exception) {
      *
      * @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
      * @since 2.12.0
+     * @deprecated Use {@link #BrokenOutputStream(Function)}.
      */
+    @Deprecated
     public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
-        this.exceptionSupplier = exceptionSupplier;
+        this.exceptionFunction = m -> exceptionSupplier.get();
     }
 
     /**
@@ -79,7 +95,7 @@ public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
      * @since 2.16.0
      */
     public BrokenOutputStream(final Throwable exception) {
-        this(() -> exception);
+        this(m -> exception);
     }
 
     /**
@@ -89,7 +105,7 @@ public BrokenOutputStream(final Throwable exception) {
      */
     @Override
     public void close() throws IOException {
-        throw rethrow();
+        throw rethrow("close()");
     }
 
     /**
@@ -99,16 +115,17 @@ public void close() throws IOException {
      */
     @Override
     public void flush() throws IOException {
-        throw rethrow();
+        throw rethrow("flush()");
     }
 
     /**
      * Throws the configured exception from its supplier.
+     * @param method TODO
      *
      * @return Throws the configured exception from its supplier.
      */
-    private RuntimeException rethrow() {
-        return Erase.rethrow(exceptionSupplier.get());
+    private RuntimeException rethrow(final String method) {
+        return Erase.rethrow(exceptionFunction.apply(method));
     }
 
     /**
@@ -119,7 +136,7 @@ private RuntimeException rethrow() {
      */
     @Override
     public void write(final int b) throws IOException {
-        throw rethrow();
+        throw rethrow("write(int)");
     }
 
 }
diff --git a/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
index a3a127baf..b507321f0 100644
--- a/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
@@ -31,7 +31,6 @@
  * Tests {@link BrokenOutputStream}.
  */
 public class BrokenOutputStreamTest {
-
     private static BrokenOutputStream createBrokenOutputStream(final Throwable exception) {
         if (exception instanceof IOException) {
             return new BrokenOutputStream((IOException) exception);
@@ -69,12 +68,11 @@ public void testTryWithResources() {
                 newStream.write(1);
             }
         });
-        assertEquals("Broken output stream", thrown.getMessage());
-
+        assertEquals("Broken output stream: write(int)", thrown.getMessage());
         final Throwable[] suppressed = thrown.getSuppressed();
         assertEquals(1, suppressed.length);
         assertEquals(IOException.class, suppressed[0].getClass());
-        assertEquals("Broken output stream", suppressed[0].getMessage());
+        assertEquals("Broken output stream: close()", suppressed[0].getMessage());
     }
 
     @ParameterizedTest
@@ -103,5 +101,4 @@ public void testWriteInt(final Class<Throwable> clazz) throws Exception {
         final BrokenOutputStream stream = createBrokenOutputStream(exception);
         assertEquals(exception, assertThrows(clazz, () -> stream.write(1)));
     }
-
 }