You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/01/02 13:37:01 UTC
(commons-io) 02/02: Add ProxyOutputStream.setReference(OutputStream)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit 07ad1b3cb5d16fbc49e4d46927c93b102f71815c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jan 2 08:36:51 2025 -0500
Add ProxyOutputStream.setReference(OutputStream)
---
src/changes/changes.xml | 1 +
.../apache/commons/io/output/ProxyOutputStream.java | 12 ++++++++++++
.../commons/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 a5f4ae83f..c3b2a3027 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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>
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 303d02943..766f5f83a 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 @@ public class ProxyOutputStream extends FilterOutputStream {
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 5f46a98a8..7420b41d5 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 class ProxyOutputStreamTest {
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 class ProxyOutputStreamTest {
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());