Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="2.23.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
<!-- FIX -->
<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>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IOConsumer.accept(IOConsumer, T) (#846).</action>
<!-- UPDATE -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ public ThresholdingOutputStream(final int threshold, final IOConsumer<Thresholdi
protected void checkThreshold(final int count) throws IOException {
if (!thresholdExceeded && written + count > threshold) {
thresholdExceeded = true;
thresholdReached();
try {
thresholdReached();
} catch (final IOException | RuntimeException e) {
thresholdExceeded = false;
throw e;
}
}
}

Expand Down Expand Up @@ -192,7 +197,7 @@ public int getThreshold() {
* @return {@code true} if the threshold has been reached; {@code false} otherwise.
*/
public boolean isThresholdExceeded() {
return written > threshold;
return thresholdExceeded;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ void testThresholdIOConsumerIOException() throws IOException {
}
}

@Test
void testThresholdReachedRetriedAfterException() throws IOException {
final int threshold = 1;
final AtomicInteger counter = new AtomicInteger();
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, tos -> {
if (counter.incrementAndGet() == 1) {
throw new IOException("Threshold reached.");
}
}, os -> new ByteArrayOutputStream(4))) {
assertThresholdingInitialState(out, threshold, 0);
out.write('a');
assertFalse(out.isThresholdExceeded());
assertThrows(IOException.class, () -> out.write('a'));
assertFalse(out.isThresholdExceeded());
out.write('a');
assertEquals(2, counter.get());
assertTrue(out.isThresholdExceeded());
}
}

@Test
void testThresholdIOConsumerUncheckedException() throws IOException {
final int threshold = 1;
Expand Down
Loading