1818
1919import java .io .IOException ;
2020import java .io .OutputStream ;
21+ import java .util .function .Function ;
2122import java .util .function .Supplier ;
2223
2324import 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}
0 commit comments