From 07ad1b3cb5d16fbc49e4d46927c93b102f71815c Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 2 Jan 2025 08:36:51 -0500 Subject: [PATCH] Add ProxyOutputStream.setReference(OutputStream) --- src/changes/changes.xml | 1 + .../commons/io/output/ProxyOutputStream.java | 12 +++++++++++ .../io/output/ProxyOutputStreamTest.java | 20 +++++++++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index a5f4ae83f51..c3b2a3027de 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -59,6 +59,7 @@ The type attribute can be add,update,fix,remove. Add AbstractByteArrayOutputStream.write(byte[]). Add RandomAccessFileOutputStream.getRandomAccessFile(). Add ProxyInputStream.setReference(InputStream), was package-private setIn(InputStream). + Add ProxyOutputStream.setReference(OutputStream). Bump commons.bytebuddy.version from 1.15.10 to 1.15.11 #710. diff --git a/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java b/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java index 303d029439a..766f5f83a3d 100644 --- a/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java @@ -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 diff --git a/src/test/java/org/apache/commons/io/output/ProxyOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ProxyOutputStreamTest.java index 5f46a98a802..7420b41d555 100644 --- a/src/test/java/org/apache/commons/io/output/ProxyOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/ProxyOutputStreamTest.java @@ -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; @@ -34,7 +35,7 @@ public class ProxyOutputStreamTest { private ByteArrayOutputStream original; - private OutputStream proxied; + private ProxyOutputStream proxied; private final AtomicBoolean hit = new AtomicBoolean(); @@ -43,13 +44,13 @@ 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); } @@ -57,6 +58,17 @@ public void write(final byte[] 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());