Skip to content

Commit 5035e97

Browse files
committed
Add ThresholdingOutputStream.getOutputStream() and deprecate getStream()
1 parent ba967e3 commit 5035e97

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ The <action> type attribute can be add,update,fix,remove.
9898
<action dev="ggregory" type="add" due-to="Gary Gregory">
9999
Add PathMatcherFileFilter to adapt java.nio.file.PathMatcher.
100100
</action>
101+
<action dev="ggregory" type="add" due-to="Gary Gregory">
102+
Add ThresholdingOutputStream.getOutputStream() and deprecate getStream().
103+
</action>
101104
<!-- UPDATE -->
102105
<action dev="ggregory" type="update" due-to="Dependabot">
103106
Bump jimfs from 1.2 to 1.3.0 #465 (tests).

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public void close() throws IOException {
118118
} catch (final IOException ignored) {
119119
// ignore
120120
}
121+
// TODO for 4.0: Replace with getOutputStream()
121122
getStream().close();
122123
}
123124

@@ -129,6 +130,7 @@ public void close() throws IOException {
129130
@SuppressWarnings("resource") // the underlying stream is managed by a subclass.
130131
@Override
131132
public void flush() throws IOException {
133+
// TODO for 4.0: Replace with getOutputStream()
132134
getStream().flush();
133135
}
134136

@@ -142,14 +144,27 @@ public long getByteCount() {
142144
}
143145

144146
/**
145-
* Returns the underlying output stream, to which the corresponding {@link OutputStream} methods in this class will
147+
* Gets the underlying output stream, to which the corresponding {@link OutputStream} methods in this class will
146148
* ultimately delegate.
147149
*
148150
* @return The underlying output stream.
149-
*
150151
* @throws IOException if an error occurs.
152+
* @deprecated Use {@link #getOutputStream()}.
151153
*/
154+
@Deprecated
152155
protected OutputStream getStream() throws IOException {
156+
return getOutputStream();
157+
}
158+
159+
/**
160+
* Gets the underlying output stream, to which the corresponding {@link OutputStream} methods in this class will
161+
* ultimately delegate.
162+
*
163+
* @return The underlying output stream.
164+
* @throws IOException if an error occurs.
165+
* @since 2.14.0
166+
*/
167+
protected OutputStream getOutputStream() throws IOException {
153168
return outputStreamGetter.apply(this);
154169
}
155170

@@ -212,6 +227,7 @@ protected void thresholdReached() throws IOException {
212227
@Override
213228
public void write(final byte[] b) throws IOException {
214229
checkThreshold(b.length);
230+
// TODO for 4.0: Replace with getOutputStream()
215231
getStream().write(b);
216232
written += b.length;
217233
}
@@ -229,6 +245,7 @@ public void write(final byte[] b) throws IOException {
229245
@Override
230246
public void write(final byte[] b, final int off, final int len) throws IOException {
231247
checkThreshold(len);
248+
// TODO for 4.0: Replace with getOutputStream()
232249
getStream().write(b, off, len);
233250
written += len;
234251
}
@@ -244,6 +261,7 @@ public void write(final byte[] b, final int off, final int len) throws IOExcepti
244261
@Override
245262
public void write(final int b) throws IOException {
246263
checkThreshold(1);
264+
// TODO for 4.0: Replace with getOutputStream()
247265
getStream().write(b);
248266
written++;
249267
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
public class ThresholdingOutputStreamTest {
3434

3535
@Test
36-
public void testSetByteCount() throws Exception {
36+
public void testSetByteCount_Stream() throws Exception {
3737
final AtomicBoolean reached = new AtomicBoolean(false);
3838
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
3939
{
@@ -57,6 +57,31 @@ protected void thresholdReached() throws IOException {
5757
}
5858
}
5959

60+
@Test
61+
public void testSetByteCount_OutputStream() throws Exception {
62+
final AtomicBoolean reached = new AtomicBoolean(false);
63+
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
64+
{
65+
setByteCount(2);
66+
}
67+
68+
@Override
69+
protected OutputStream getOutputStream() throws IOException {
70+
return new ByteArrayOutputStream(4);
71+
}
72+
73+
@Override
74+
protected void thresholdReached() throws IOException {
75+
reached.set(true);
76+
}
77+
}) {
78+
tos.write('a');
79+
assertFalse(reached.get());
80+
tos.write('a');
81+
assertTrue(reached.get());
82+
}
83+
}
84+
6085
@Test
6186
public void testThresholdIOConsumer() throws Exception {
6287
final AtomicBoolean reached = new AtomicBoolean();

0 commit comments

Comments
 (0)