Skip to content

Commit a016740

Browse files
committed
Add ProxyOutputStream.Builder
1 parent 549a034 commit a016740

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ The <action> type attribute can be add,update,fix,remove.
6161
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyInputStream.setReference(InputStream), was package-private setIn(InputStream).</action>
6262
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyOutputStream.setReference(OutputStream).</action>
6363
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileInputStream.copy(long, long, OutputStream).</action>
64+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyOutputStream.Builder.</action>
6465
<!-- UPDATE -->
6566
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.16.1 #710, #715.</action>
6667
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.1 to 1.17.2.</action>

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.OutputStream;
2222

2323
import org.apache.commons.io.IOUtils;
24+
import org.apache.commons.io.build.AbstractStreamBuilder;
2425

2526
/**
2627
* A Proxy stream which acts as expected, that is it passes the method
@@ -34,6 +35,46 @@
3435
*/
3536
public class ProxyOutputStream extends FilterOutputStream {
3637

38+
/**
39+
* Builds instances of {@link ProxyOutputStream}.
40+
* <p>
41+
* This class does not provide a convenience static {@code builder()} method so that subclasses can.
42+
* </p>
43+
*
44+
* @since 2.19.0
45+
*/
46+
public static class Builder extends AbstractStreamBuilder<ProxyOutputStream, Builder> {
47+
48+
/**
49+
* Constructs a new builder of {@link ProxyOutputStream}.
50+
*/
51+
public Builder() {
52+
// empty
53+
}
54+
55+
/**
56+
* Builds a new {@link ProxyOutputStream}.
57+
* <p>
58+
* This builder use the following aspects:
59+
* </p>
60+
* <ul>
61+
* <li>{@link #getOutputStream()}</li>
62+
* </ul>
63+
*
64+
* @return a new instance.
65+
* @throws IllegalStateException if the {@code origin} is {@code null}.
66+
* @throws UnsupportedOperationException if the origin cannot be converted to an {@link OutputStream}.
67+
* @throws IOException if an I/O error occurs.
68+
* @see #getOutputStream()
69+
*/
70+
@SuppressWarnings("resource") // Caller closes.
71+
@Override
72+
public ProxyOutputStream get() throws IOException {
73+
return new ProxyOutputStream(getOutputStream());
74+
}
75+
76+
}
77+
3778
/**
3879
* Constructs a new ProxyOutputStream.
3980
*
@@ -128,6 +169,18 @@ public ProxyOutputStream setReference(final OutputStream out) {
128169
return this;
129170
}
130171

172+
/**
173+
* Unwraps this instance by returning the underlying {@link OutputStream}.
174+
* <p>
175+
* Use with caution; useful to query the underlying {@link OutputStream}.
176+
* </p>
177+
*
178+
* @return the underlying {@link OutputStream}.
179+
*/
180+
OutputStream unwrap() {
181+
return out;
182+
}
183+
131184
/**
132185
* Invokes the delegate's {@code write(byte[])} method.
133186
* @param bts the bytes to write

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertSame;
2223
import static org.junit.jupiter.api.Assertions.assertThrows;
2324
import static org.junit.jupiter.api.Assertions.assertTrue;
2425

@@ -58,6 +59,11 @@ public synchronized void write(final int ba) {
5859
proxied = new ProxyOutputStream(original);
5960
}
6061

62+
@Test
63+
public void testBuilder() throws Exception {
64+
assertSame(original, new ProxyOutputStream.Builder().setOutputStream(original).get().unwrap());
65+
}
66+
6167
@SuppressWarnings("resource")
6268
@Test
6369
public void testSetReference() throws Exception {

0 commit comments

Comments
 (0)