Skip to content

Commit 2ab3e9c

Browse files
committed
Add ProxyWriter.setReference(Writer)
Like roxyOutputStream.setReference(OutputStream)
1 parent 56ab304 commit 2ab3e9c

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ file comparators, endian transformation classes, and much more.
132132
</commons.osgi.import>
133133
<commons.scmPubUrl>https://svn.apache.org/repos/infra/websites/production/commons/content/proper/commons-io/</commons.scmPubUrl>
134134
<commons.scmPubCheckoutDirectory>site-content</commons.scmPubCheckoutDirectory>
135-
<commons.bytebuddy.version>1.18.4</commons.bytebuddy.version>
135+
<commons.bytebuddy.version>1.18.7</commons.bytebuddy.version>
136136
<japicmp.skip>false</japicmp.skip>
137137
<commons.release.isDistModule>true</commons.release.isDistModule>
138138
<!-- JaCoCo: Don't make code coverage worse than: -->

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ The <action> type attribute can be add,update,fix,remove.
6363
<action type="fix" dev="ggregory" due-to="Martin Wiesner">Fix typos in Javadoc of FileUtils and related test classes #833.</action>
6464
<!-- ADD -->
6565
<action type="add" dev="ggregory" due-to="Gary Gregory, Piotr P. Karwasz">Add and use IOUtils.closeQuietlySuppress(Closeable, Throwable) #818.</action>
66+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ProxyWriter.setReference(Writer).</action>
6667
<!-- UPDATE -->
6768
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 91 to 97 #816.</action>
6869
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump commons-codec:commons-codec from 1.19.0 to 1.21.0 #812.</action>

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

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

184+
/**
185+
* Sets the underlying writer.
186+
*
187+
* @param out the underlying output writer.
188+
* @return {@code this} instance.
189+
* @since 2.22.0
190+
*/
191+
public ProxyWriter setReference(final Writer out) {
192+
this.out = out;
193+
return this;
194+
}
195+
184196
/**
185197
* Invokes the delegate's {@code write(char[])} method.
186198
*

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,57 @@
1717
package org.apache.commons.io.output;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
2021
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

2223
import java.io.IOException;
2324
import java.io.OutputStreamWriter;
25+
import java.io.StringWriter;
2426
import java.io.UnsupportedEncodingException;
2527
import java.io.Writer;
28+
import java.util.concurrent.atomic.AtomicBoolean;
2629

30+
import org.junit.jupiter.api.BeforeEach;
2731
import org.junit.jupiter.api.Test;
2832

2933
/**
3034
* Test {@link ProxyWriter}.
3135
*/
3236
class ProxyWriterTest {
3337

38+
private StringWriter target;
39+
40+
private ProxyWriter proxied;
41+
42+
private final AtomicBoolean hitArray = new AtomicBoolean();
43+
private final AtomicBoolean hitArrayAt = new AtomicBoolean();
44+
private final AtomicBoolean hitInt = new AtomicBoolean();
45+
46+
@BeforeEach
47+
public void setUp() {
48+
target = new StringWriter() {
49+
50+
@Override
51+
public void write(final char[] ba) throws IOException {
52+
hitArray.set(true);
53+
super.write(ba);
54+
}
55+
56+
@Override
57+
public void write(final char[] b, final int off, final int len) {
58+
hitArrayAt.set(true);
59+
super.write(b, off, len);
60+
}
61+
62+
@Override
63+
public synchronized void write(final int ba) {
64+
hitInt.set(true);
65+
super.write(ba);
66+
}
67+
};
68+
proxied = new ProxyWriter(target);
69+
}
70+
3471
@Test
3572
void testAppendChar() throws Exception {
3673
try (StringBuilderWriter writer = new StringBuilderWriter();
@@ -226,6 +263,16 @@ void testNullString() throws Exception {
226263
}
227264
}
228265

266+
@Test
267+
void testSetReference() throws Exception {
268+
assertFalse(hitArray.get());
269+
proxied.setReference(new StringWriter());
270+
proxied.write('y');
271+
assertFalse(hitArray.get());
272+
assertEquals(0, target.toString().length());
273+
assertEquals("", target.toString());
274+
}
275+
229276
@Test
230277
void testWriteCharArray() throws Exception {
231278
try (StringBuilderWriter writer = new StringBuilderWriter();

0 commit comments

Comments
 (0)