Skip to content

Commit 025850a

Browse files
committed
Add BrokenOutputStream.BrokenOutputStream(Function<String>, Throwable>)
Deprecate <String> constructor
1 parent 00a39a9 commit 025850a

3 files changed

Lines changed: 30 additions & 15 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The <action> type attribute can be add,update,fix,remove.
6767
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileInputStream.copy(long, long, OutputStream).</action>
6868
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyOutputStream.Builder.</action>
6969
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ByteOrderMark.matches(int[]).</action>
70+
<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>
7071
<!-- UPDATE -->
7172
<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>
7273
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.1 to 1.18.0. #717.</action>

src/main/java/org/apache/commons/io/output/BrokenOutputStream.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.OutputStream;
21+
import java.util.function.Function;
2122
import java.util.function.Supplier;
2223

2324
import org.apache.commons.io.function.Erase;
@@ -42,13 +43,13 @@ public class BrokenOutputStream extends OutputStream {
4243
/**
4344
* Supplies the exception that is thrown by all methods of this class.
4445
*/
45-
private final Supplier<Throwable> exceptionSupplier;
46+
private final Function<String, Throwable> exceptionFunction;
4647

4748
/**
4849
* Constructs a new stream that always throws an {@link IOException}.
4950
*/
5051
public BrokenOutputStream() {
51-
this(() -> new IOException("Broken output stream"));
52+
this(m -> new IOException("Broken output stream: " + m));
5253
}
5354

5455
/**
@@ -59,17 +60,32 @@ public BrokenOutputStream() {
5960
*/
6061
@Deprecated
6162
public BrokenOutputStream(final IOException exception) {
62-
this(() -> exception);
63+
this(m -> exception);
64+
}
65+
66+
/**
67+
* Constructs a new stream that always throws the supplied exception.
68+
* <p>
69+
* This class uses the invoked method name as the function input.
70+
* </p>
71+
*
72+
* @param exceptionFunction a supplier for the IOException or RuntimeException to be thrown.
73+
* @since 2.19.0
74+
*/
75+
public BrokenOutputStream(final Function<String, Throwable> exceptionFunction) {
76+
this.exceptionFunction = exceptionFunction;
6377
}
6478

6579
/**
6680
* Constructs a new stream that always throws the supplied exception.
6781
*
6882
* @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
6983
* @since 2.12.0
84+
* @deprecated Use {@link #BrokenOutputStream(Function)}.
7085
*/
86+
@Deprecated
7187
public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
72-
this.exceptionSupplier = exceptionSupplier;
88+
this.exceptionFunction = m -> exceptionSupplier.get();
7389
}
7490

7591
/**
@@ -79,7 +95,7 @@ public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
7995
* @since 2.16.0
8096
*/
8197
public BrokenOutputStream(final Throwable exception) {
82-
this(() -> exception);
98+
this(m -> exception);
8399
}
84100

85101
/**
@@ -89,7 +105,7 @@ public BrokenOutputStream(final Throwable exception) {
89105
*/
90106
@Override
91107
public void close() throws IOException {
92-
throw rethrow();
108+
throw rethrow("close()");
93109
}
94110

95111
/**
@@ -99,16 +115,17 @@ public void close() throws IOException {
99115
*/
100116
@Override
101117
public void flush() throws IOException {
102-
throw rethrow();
118+
throw rethrow("flush()");
103119
}
104120

105121
/**
106122
* Throws the configured exception from its supplier.
123+
* @param method TODO
107124
*
108125
* @return Throws the configured exception from its supplier.
109126
*/
110-
private RuntimeException rethrow() {
111-
return Erase.rethrow(exceptionSupplier.get());
127+
private RuntimeException rethrow(final String method) {
128+
return Erase.rethrow(exceptionFunction.apply(method));
112129
}
113130

114131
/**
@@ -119,7 +136,7 @@ private RuntimeException rethrow() {
119136
*/
120137
@Override
121138
public void write(final int b) throws IOException {
122-
throw rethrow();
139+
throw rethrow("write(int)");
123140
}
124141

125142
}

src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
* Tests {@link BrokenOutputStream}.
3232
*/
3333
public class BrokenOutputStreamTest {
34-
3534
private static BrokenOutputStream createBrokenOutputStream(final Throwable exception) {
3635
if (exception instanceof IOException) {
3736
return new BrokenOutputStream((IOException) exception);
@@ -69,12 +68,11 @@ public void testTryWithResources() {
6968
newStream.write(1);
7069
}
7170
});
72-
assertEquals("Broken output stream", thrown.getMessage());
73-
71+
assertEquals("Broken output stream: write(int)", thrown.getMessage());
7472
final Throwable[] suppressed = thrown.getSuppressed();
7573
assertEquals(1, suppressed.length);
7674
assertEquals(IOException.class, suppressed[0].getClass());
77-
assertEquals("Broken output stream", suppressed[0].getMessage());
75+
assertEquals("Broken output stream: close()", suppressed[0].getMessage());
7876
}
7977

8078
@ParameterizedTest
@@ -103,5 +101,4 @@ public void testWriteInt(final Class<Throwable> clazz) throws Exception {
103101
final BrokenOutputStream stream = createBrokenOutputStream(exception);
104102
assertEquals(exception, assertThrows(clazz, () -> stream.write(1)));
105103
}
106-
107104
}

0 commit comments

Comments
 (0)