Skip to content

Commit

Permalink
Add ProxyOutputStream.setReference(OutputStream)
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jan 2, 2025
1 parent f78f7a2 commit 07ad1b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(byte[]).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileOutputStream.getRandomAccessFile().</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyInputStream.setReference(InputStream), was package-private setIn(InputStream).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyOutputStream.setReference(OutputStream).</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.15.11 #710.</action>
</release>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ protected void handleIOException(final IOException e) throws IOException {
throw e;
}

/**
* Sets the underlying output stream.
*
* @param out the underlying output stream.
* @return this instance.
* @since 2.19.0
*/
public ProxyOutputStream setReference(final OutputStream out) {
this.out = out;
return this;
}

/**
* Invokes the delegate's {@code write(byte[])} method.
* @param bts the bytes to write
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
*/
package org.apache.commons.io.output;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -34,7 +35,7 @@ public class ProxyOutputStreamTest {

private ByteArrayOutputStream original;

private OutputStream proxied;
private ProxyOutputStream proxied;

private final AtomicBoolean hit = new AtomicBoolean();

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

@Override
public synchronized void write(final int ba) {
public void write(final byte[] ba) {
hit.set(true);
super.write(ba);
}

@Override
public void write(final byte[] ba) {
public synchronized void write(final int ba) {
hit.set(true);
super.write(ba);
}
};
proxied = new ProxyOutputStream(original);
}

@SuppressWarnings("resource")
@Test
public void testSetReference() throws Exception {
assertFalse(hit.get());
proxied.setReference(new ByteArrayOutputStream());
proxied.write('y');
assertFalse(hit.get());
assertEquals(0, original.size());
assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, original.toByteArray());
}

@Test
public void testWrite() throws Exception {
assertFalse(hit.get());
Expand Down

0 comments on commit 07ad1b3

Please sign in to comment.