|
23 | 23 | import org.apache.commons.io.function.IOFunction; |
24 | 24 |
|
25 | 25 | /** |
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 | + * |
29 | 32 | * <p> |
30 | 33 | * This class overrides all {@link OutputStream} methods. However, these overrides ultimately call the corresponding |
31 | 34 | * methods in the underlying output stream implementation. |
@@ -89,6 +92,9 @@ public ThresholdingOutputStream(final int threshold, final IOConsumer<Thresholdi |
89 | 92 | this.threshold = threshold; |
90 | 93 | this.thresholdConsumer = thresholdConsumer == null ? IOConsumer.noop() : thresholdConsumer; |
91 | 94 | this.outputStreamGetter = outputStreamGetter == null ? NOOP_OS_GETTER : outputStreamGetter; |
| 95 | + if (threshold < 0) { |
| 96 | + thresholdExceeded = true; |
| 97 | + } |
92 | 98 | } |
93 | 99 |
|
94 | 100 | /** |
@@ -244,6 +250,8 @@ public void write(final byte[] b) throws IOException { |
244 | 250 | @SuppressWarnings("resource") // the underlying stream is managed by a subclass. |
245 | 251 | @Override |
246 | 252 | 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. |
247 | 255 | checkThreshold(len); |
248 | 256 | // TODO for 4.0: Replace with getOutputStream() |
249 | 257 | getStream().write(b, off, len); |
|
0 commit comments