Skip to content

Commit 07ad1b3

Browse files
committed
Add ProxyOutputStream.setReference(OutputStream)
1 parent f78f7a2 commit 07ad1b3

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
5959
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(byte[]).</action>
6060
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileOutputStream.getRandomAccessFile().</action>
6161
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyInputStream.setReference(InputStream), was package-private setIn(InputStream).</action>
62+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyOutputStream.setReference(OutputStream).</action>
6263
<!-- UPDATE -->
6364
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.15.11 #710.</action>
6465
</release>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ protected void handleIOException(final IOException e) throws IOException {
116116
throw e;
117117
}
118118

119+
/**
120+
* Sets the underlying output stream.
121+
*
122+
* @param out the underlying output stream.
123+
* @return this instance.
124+
* @since 2.19.0
125+
*/
126+
public ProxyOutputStream setReference(final OutputStream out) {
127+
this.out = out;
128+
return this;
129+
}
130+
119131
/**
120132
* Invokes the delegate's {@code write(byte[])} method.
121133
* @param bts the bytes to write

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
*/
1717
package org.apache.commons.io.output;
1818

19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
2324

24-
import java.io.OutputStream;
2525
import java.util.concurrent.atomic.AtomicBoolean;
2626

27+
import org.apache.commons.lang3.ArrayUtils;
2728
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
2930

@@ -34,7 +35,7 @@ public class ProxyOutputStreamTest {
3435

3536
private ByteArrayOutputStream original;
3637

37-
private OutputStream proxied;
38+
private ProxyOutputStream proxied;
3839

3940
private final AtomicBoolean hit = new AtomicBoolean();
4041

@@ -43,20 +44,31 @@ public void setUp() {
4344
original = new ByteArrayOutputStream() {
4445

4546
@Override
46-
public synchronized void write(final int ba) {
47+
public void write(final byte[] ba) {
4748
hit.set(true);
4849
super.write(ba);
4950
}
5051

5152
@Override
52-
public void write(final byte[] ba) {
53+
public synchronized void write(final int ba) {
5354
hit.set(true);
5455
super.write(ba);
5556
}
5657
};
5758
proxied = new ProxyOutputStream(original);
5859
}
5960

61+
@SuppressWarnings("resource")
62+
@Test
63+
public void testSetReference() throws Exception {
64+
assertFalse(hit.get());
65+
proxied.setReference(new ByteArrayOutputStream());
66+
proxied.write('y');
67+
assertFalse(hit.get());
68+
assertEquals(0, original.size());
69+
assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, original.toByteArray());
70+
}
71+
6072
@Test
6173
public void testWrite() throws Exception {
6274
assertFalse(hit.get());

0 commit comments

Comments
 (0)