Skip to content

Commit dd63fa3

Browse files
[IO-826] Reimplemented add runtime exception support to broken streams (#530)
* Introduce ability to throw RuntimeExceptions from BrokenReader / BrokenOutputStream / BrokenWriter following example of BrokenInputStream. --------- Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
1 parent eee328b commit dd63fa3

9 files changed

Lines changed: 399 additions & 221 deletions

File tree

src/main/java/org/apache/commons/io/input/BrokenInputStream.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.apache.commons.io.function.Erase;
2424

2525
/**
26-
* Always throws an {@link IOException} from all the {@link InputStream} methods where the exception is declared.
26+
* Always throws an exception from all {@link InputStream} methods where {@link IOException} is declared.
2727
* <p>
2828
* This class is mostly useful for testing error handling.
2929
* </p>
@@ -33,7 +33,7 @@
3333
public class BrokenInputStream extends InputStream {
3434

3535
/**
36-
* The singleton instance.
36+
* The singleton instance using a default IOException.
3737
*
3838
* @since 2.12.0
3939
*/
@@ -73,7 +73,7 @@ public BrokenInputStream(final Throwable exception) {
7373
}
7474

7575
/**
76-
* Constructs a new stream that always throws an {@link IOException}.
76+
* Constructs a new stream that always throws the supplied exception.
7777
*
7878
* @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
7979
* @since 2.12.0
@@ -85,8 +85,8 @@ public BrokenInputStream(final Supplier<Throwable> exceptionSupplier) {
8585
/**
8686
* Throws the configured exception.
8787
*
88-
* @return nothing
89-
* @throws IOException always thrown
88+
* @return nothing.
89+
* @throws IOException always throws the exception configured in a constructor.
9090
*/
9191
@Override
9292
public int available() throws IOException {
@@ -96,7 +96,7 @@ public int available() throws IOException {
9696
/**
9797
* Throws the configured exception.
9898
*
99-
* @throws IOException always thrown
99+
* @throws IOException always throws the exception configured in a constructor.
100100
*/
101101
@Override
102102
public void close() throws IOException {
@@ -106,8 +106,8 @@ public void close() throws IOException {
106106
/**
107107
* Throws the configured exception.
108108
*
109-
* @return nothing
110-
* @throws IOException always thrown
109+
* @return nothing.
110+
* @throws IOException always throws the exception configured in a constructor.
111111
*/
112112
@Override
113113
public int read() throws IOException {
@@ -117,7 +117,7 @@ public int read() throws IOException {
117117
/**
118118
* Throws the configured exception.
119119
*
120-
* @throws IOException always thrown
120+
* @throws IOException always throws the exception configured in a constructor.
121121
*/
122122
@Override
123123
public synchronized void reset() throws IOException {
@@ -136,9 +136,9 @@ private RuntimeException rethrow() {
136136
/**
137137
* Throws the configured exception.
138138
*
139-
* @param n ignored
140-
* @return nothing
141-
* @throws IOException always thrown
139+
* @param n ignored.
140+
* @return nothing.
141+
* @throws IOException always throws the exception configured in a constructor.
142142
*/
143143
@Override
144144
public long skip(final long n) throws IOException {

src/main/java/org/apache/commons/io/input/BrokenReader.java

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.io.Reader;
2121
import java.util.function.Supplier;
2222

23+
import org.apache.commons.io.function.Erase;
24+
2325
/**
24-
* Always throws an {@link IOException} from all the {@link Reader} methods where the exception is declared.
26+
* Always throws an exception from all {@link Reader} methods where {@link IOException} is declared.
2527
* <p>
2628
* This class is mostly useful for testing error handling.
2729
* </p>
@@ -40,7 +42,7 @@ public class BrokenReader extends Reader {
4042
/**
4143
* A supplier for the exception that is thrown by all methods of this class.
4244
*/
43-
private final Supplier<IOException> exceptionSupplier;
45+
private final Supplier<Throwable> exceptionSupplier;
4446

4547
/**
4648
* Constructs a new reader that always throws an {@link IOException}.
@@ -53,87 +55,108 @@ public BrokenReader() {
5355
* Constructs a new reader that always throws the given exception.
5456
*
5557
* @param exception the exception to be thrown.
58+
* @deprecated Use {@link #BrokenReader(Throwable)}.
5659
*/
60+
@Deprecated
5761
public BrokenReader(final IOException exception) {
5862
this(() -> exception);
5963
}
6064

6165
/**
62-
* Constructs a new reader that always throws an {@link IOException}
66+
* Constructs a new reader that always throws the given exception.
67+
*
68+
* @param exception the exception to be thrown.
69+
* @since 2.16.0
70+
*/
71+
public BrokenReader(final Throwable exception) {
72+
this(() -> exception);
73+
}
74+
75+
/**
76+
* Constructs a new reader that always throws the supplied exception.
6377
*
64-
* @param exceptionSupplier a supplier for the exception to be thrown.
78+
* @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
6579
* @since 2.12.0
6680
*/
67-
public BrokenReader(final Supplier<IOException> exceptionSupplier) {
81+
public BrokenReader(final Supplier<Throwable> exceptionSupplier) {
6882
this.exceptionSupplier = exceptionSupplier;
6983
}
7084

7185
/**
7286
* Throws the configured exception.
7387
*
74-
* @throws IOException always thrown
88+
* @throws IOException always throws the exception configured in a constructor.
7589
*/
7690
@Override
7791
public void close() throws IOException {
78-
throw exceptionSupplier.get();
92+
throw rethrow();
7993
}
8094

8195
/**
8296
* Throws the configured exception.
8397
*
84-
* @param readAheadLimit ignored
85-
* @throws IOException always thrown
98+
* @param readAheadLimit ignored.
99+
* @throws IOException always throws the exception configured in a constructor.
86100
*/
87101
@Override
88102
public void mark(final int readAheadLimit) throws IOException {
89-
throw exceptionSupplier.get();
103+
throw rethrow();
90104
}
91105

92106
/**
93107
* Throws the configured exception.
94108
*
95-
* @param cbuf ignored
96-
* @param off ignored
97-
* @param len ignored
98-
* @return nothing
99-
* @throws IOException always thrown
109+
* @param cbuf ignored.
110+
* @param off ignored.
111+
* @param len ignored.
112+
* @return nothing.
113+
* @throws IOException always throws the exception configured in a constructor.
100114
*/
101115
@Override
102116
public int read(final char[] cbuf, final int off, final int len) throws IOException {
103-
throw exceptionSupplier.get();
117+
throw rethrow();
104118
}
105119

106120
/**
107121
* Throws the configured exception.
108122
*
109-
* @return nothing
110-
* @throws IOException always thrown
123+
* @return nothing.
124+
* @throws IOException always throws the exception configured in a constructor.
111125
*/
112126
@Override
113127
public boolean ready() throws IOException {
114-
throw exceptionSupplier.get();
128+
throw rethrow();
115129
}
116130

117131
/**
118132
* Throws the configured exception.
119133
*
120-
* @throws IOException always thrown
134+
* @throws IOException always throws the exception configured in a constructor.
121135
*/
122136
@Override
123-
public synchronized void reset() throws IOException {
124-
throw exceptionSupplier.get();
137+
public void reset() throws IOException {
138+
throw rethrow();
139+
}
140+
141+
/**
142+
* Throws the configured exception from its supplier.
143+
*
144+
* @return Throws the configured exception from its supplier.
145+
*/
146+
private RuntimeException rethrow() {
147+
return Erase.rethrow(exceptionSupplier.get());
125148
}
126149

127150
/**
128151
* Throws the configured exception.
129152
*
130-
* @param n ignored
131-
* @return nothing
132-
* @throws IOException always thrown
153+
* @param n ignored.
154+
* @return nothing.
155+
* @throws IOException always throws the exception configured in a constructor.
133156
*/
134157
@Override
135158
public long skip(final long n) throws IOException {
136-
throw exceptionSupplier.get();
159+
throw rethrow();
137160
}
138161

139162
}

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,29 @@
2020
import java.io.OutputStream;
2121
import java.util.function.Supplier;
2222

23+
import org.apache.commons.io.function.Erase;
24+
2325
/**
24-
* Broken output stream. This stream always throws an {@link IOException} from
25-
* all {@link OutputStream} methods.
26+
* Always throws an exception from all {@link OutputStream} methods where {@link IOException} is declared.
2627
* <p>
27-
* This class is mostly useful for testing error handling in code that uses an
28-
* output stream.
28+
* This class is mostly useful for testing error handling.
2929
* </p>
3030
*
3131
* @since 2.0
3232
*/
3333
public class BrokenOutputStream extends OutputStream {
3434

3535
/**
36-
* A singleton instance.
36+
* The singleton instance using a default IOException.
3737
*
3838
* @since 2.12.0
3939
*/
4040
public static final BrokenOutputStream INSTANCE = new BrokenOutputStream();
4141

4242
/**
43-
* A supplier for the exception that is thrown by all methods of this class.
43+
* Supplies the exception that is thrown by all methods of this class.
4444
*/
45-
private final Supplier<IOException> exceptionSupplier;
45+
private final Supplier<Throwable> exceptionSupplier;
4646

4747
/**
4848
* Constructs a new stream that always throws an {@link IOException}.
@@ -55,50 +55,71 @@ public BrokenOutputStream() {
5555
* Constructs a new stream that always throws the given exception.
5656
*
5757
* @param exception the exception to be thrown.
58+
* @deprecated Use {@link #BrokenOutputStream(Throwable)}.
5859
*/
60+
@Deprecated
5961
public BrokenOutputStream(final IOException exception) {
6062
this(() -> exception);
6163
}
6264

6365
/**
64-
* Constructs a new stream that always throws an {@link IOException}.
66+
* Constructs a new stream that always throws the given exception.
6567
*
66-
* @param exceptionSupplier a supplier for the exception to be thrown.
68+
* @param exception the exception to be thrown.
69+
* @since 2.16.0
70+
*/
71+
public BrokenOutputStream(final Throwable exception) {
72+
this(() -> exception);
73+
}
74+
75+
/**
76+
* Constructs a new stream that always throws the supplied exception.
77+
*
78+
* @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
6779
* @since 2.12.0
6880
*/
69-
public BrokenOutputStream(final Supplier<IOException> exceptionSupplier) {
81+
public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
7082
this.exceptionSupplier = exceptionSupplier;
7183
}
7284

7385
/**
7486
* Throws the configured exception.
7587
*
76-
* @throws IOException always thrown
88+
* @throws IOException always throws the exception configured in a constructor.
7789
*/
7890
@Override
7991
public void close() throws IOException {
80-
throw exceptionSupplier.get();
92+
throw rethrow();
8193
}
8294

8395
/**
8496
* Throws the configured exception.
8597
*
86-
* @throws IOException always thrown
98+
* @throws IOException always throws the exception configured in a constructor.
8799
*/
88100
@Override
89101
public void flush() throws IOException {
90-
throw exceptionSupplier.get();
102+
throw rethrow();
103+
}
104+
105+
/**
106+
* Throws the configured exception from its supplier.
107+
*
108+
* @return Throws the configured exception from its supplier.
109+
*/
110+
private RuntimeException rethrow() {
111+
return Erase.rethrow(exceptionSupplier.get());
91112
}
92113

93114
/**
94115
* Throws the configured exception.
95116
*
96-
* @param b ignored
97-
* @throws IOException always thrown
117+
* @param b ignored.
118+
* @throws IOException always throws the exception configured in a constructor.
98119
*/
99120
@Override
100121
public void write(final int b) throws IOException {
101-
throw exceptionSupplier.get();
122+
throw rethrow();
102123
}
103124

104125
}

0 commit comments

Comments
 (0)