Skip to content

Commit 4f144cf

Browse files
authored
handle zero and negative thresholds (#587)
1 parent dc3dd7c commit 4f144cf

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

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

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

2525
/**
26-
* An output stream which triggers an event when a specified number of bytes of data have been written to it. The event
27-
* can be used, for example, to throw an exception if a maximum has been reached, or to switch the underlying stream
28-
* type when the threshold is exceeded.
26+
* An output stream which triggers an event on the first write that causes
27+
* the total number of bytes written to the stream to exceed a configured threshold,
28+
* and every subsequent write. The event
29+
* can be used, for example, to throw an exception if a maximum has been reached,
30+
* or to switch the underlying stream when the threshold is exceeded.
31+
*
2932
* <p>
3033
* This class overrides all {@link OutputStream} methods. However, these overrides ultimately call the corresponding
3134
* methods in the underlying output stream implementation.
@@ -89,6 +92,9 @@ public ThresholdingOutputStream(final int threshold, final IOConsumer<Thresholdi
8992
this.threshold = threshold;
9093
this.thresholdConsumer = thresholdConsumer == null ? IOConsumer.noop() : thresholdConsumer;
9194
this.outputStreamGetter = outputStreamGetter == null ? NOOP_OS_GETTER : outputStreamGetter;
95+
if (threshold < 0) {
96+
thresholdExceeded = true;
97+
}
9298
}
9399

94100
/**
@@ -244,6 +250,8 @@ public void write(final byte[] b) throws IOException {
244250
@SuppressWarnings("resource") // the underlying stream is managed by a subclass.
245251
@Override
246252
public void write(final byte[] b, final int off, final int len) throws IOException {
253+
// TODO we could write the sub-array up the threshold, fire the event,
254+
// and then write the rest so the event is always fired at the precise point.
247255
checkThreshold(len);
248256
// TODO for 4.0: Replace with getOutputStream()
249257
getStream().write(b, off, len);

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@
3232
*/
3333
public class ThresholdingOutputStreamTest {
3434

35+
@Test
36+
public void testThresholdLessThanZero() throws IOException {
37+
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(-1)) {
38+
assertTrue(out.isThresholdExceeded());
39+
}
40+
}
41+
42+
@Test
43+
public void testThresholdZero() throws IOException {
44+
final AtomicBoolean reached = new AtomicBoolean(false);
45+
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(0) {
46+
@Override
47+
protected void thresholdReached() throws IOException {
48+
reached.set(true);
49+
}
50+
}) {
51+
assertFalse(out.isThresholdExceeded());
52+
out.write(89);
53+
assertTrue(reached.get());
54+
assertTrue(out.isThresholdExceeded());
55+
}
56+
}
57+
3558
@Test
3659
public void testSetByteCount_OutputStream() throws Exception {
3760
final AtomicBoolean reached = new AtomicBoolean(false);

0 commit comments

Comments
 (0)