Skip to content

Commit a68a9f1

Browse files
authored
[IO-867] Fix ThresholdingOutputStream.isThresholdExceeded and event retry (#847)
1 parent c9f54fc commit a68a9f1

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<release version="2.23.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4949
<!-- FIX -->
50+
<action issue="IO-867" type="fix" dev="ggregory" due-to="Sai Asish Y">ThresholdingOutputStream.isThresholdExceeded() now reflects the threshold-reached state, and a failed thresholdReached() no longer leaves the stream unable to fire the event again.</action>
5051
<!-- ADD -->
5152
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IOConsumer.accept(IOConsumer, T) (#846).</action>
5253
<!-- UPDATE -->

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ public ThresholdingOutputStream(final int threshold, final IOConsumer<Thresholdi
111111
protected void checkThreshold(final int count) throws IOException {
112112
if (!thresholdExceeded && written + count > threshold) {
113113
thresholdExceeded = true;
114-
thresholdReached();
114+
try {
115+
thresholdReached();
116+
} catch (final IOException | RuntimeException e) {
117+
thresholdExceeded = false;
118+
throw e;
119+
}
115120
}
116121
}
117122

@@ -192,7 +197,7 @@ public int getThreshold() {
192197
* @return {@code true} if the threshold has been reached; {@code false} otherwise.
193198
*/
194199
public boolean isThresholdExceeded() {
195-
return written > threshold;
200+
return thresholdExceeded;
196201
}
197202

198203
/**

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,26 @@ void testThresholdIOConsumerIOException() throws IOException {
208208
}
209209
}
210210

211+
@Test
212+
void testThresholdReachedRetriedAfterException() throws IOException {
213+
final int threshold = 1;
214+
final AtomicInteger counter = new AtomicInteger();
215+
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, tos -> {
216+
if (counter.incrementAndGet() == 1) {
217+
throw new IOException("Threshold reached.");
218+
}
219+
}, os -> new ByteArrayOutputStream(4))) {
220+
assertThresholdingInitialState(out, threshold, 0);
221+
out.write('a');
222+
assertFalse(out.isThresholdExceeded());
223+
assertThrows(IOException.class, () -> out.write('a'));
224+
assertFalse(out.isThresholdExceeded());
225+
out.write('a');
226+
assertEquals(2, counter.get());
227+
assertTrue(out.isThresholdExceeded());
228+
}
229+
}
230+
211231
@Test
212232
void testThresholdIOConsumerUncheckedException() throws IOException {
213233
final int threshold = 1;

0 commit comments

Comments
 (0)