Skip to content

Commit e4d8537

Browse files
authored
CloseShieldInputStream now supports a custom close shield as a function (#836)
* [IO-856] Try test on all OSs for GitHub CI * CloseShieldInputStreamTest now supports a custom close shield as a function
1 parent 248e10e commit e4d8537

2 files changed

Lines changed: 91 additions & 5 deletions

File tree

src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19+
import java.io.IOException;
1920
import java.io.InputStream;
2021

22+
import org.apache.commons.io.function.IOUnaryOperator;
23+
2124
/**
2225
* Proxy stream that prevents the underlying input stream from being closed.
2326
* <p>
@@ -30,6 +33,50 @@
3033
*/
3134
public class CloseShieldInputStream extends ProxyInputStream {
3235

36+
/**
37+
* Constructs a new builder for {@link CloseShieldInputStream}.
38+
*
39+
* @since 2.22.0
40+
*/
41+
public static class Builder extends AbstractBuilder<CloseShieldInputStream, Builder> {
42+
43+
private IOUnaryOperator<InputStream> onClose = is -> ClosedInputStream.INSTANCE;
44+
45+
/**
46+
* Constructs a new instance.
47+
*/
48+
public Builder() {
49+
// empty
50+
}
51+
52+
@Override
53+
public CloseShieldInputStream get() throws IOException {
54+
return new CloseShieldInputStream(this);
55+
}
56+
57+
/**
58+
* Sets the {@code onClose} function. By default, replaces the underlying input stream when {@link #close()} is called.
59+
*
60+
* @param onClose the onClose function.
61+
* @return {@code this} instance.
62+
*/
63+
public Builder setOnClose(final IOUnaryOperator<InputStream> onClose) {
64+
this.onClose = onClose;
65+
return asThis();
66+
}
67+
68+
}
69+
70+
/**
71+
* Constructs a new builder for {@link CloseShieldInputStream}.
72+
*
73+
* @return the new builder.
74+
* @since 2.22.0
75+
*/
76+
public static Builder builder() {
77+
return new Builder();
78+
}
79+
3380
/**
3481
* Constructs a proxy that only shields {@link System#in} from closing.
3582
*
@@ -52,6 +99,13 @@ public static CloseShieldInputStream wrap(final InputStream inputStream) {
5299
return new CloseShieldInputStream(inputStream);
53100
}
54101

102+
private final IOUnaryOperator<InputStream> onClose;
103+
104+
private CloseShieldInputStream(final Builder builder) throws IOException {
105+
super(builder.getInputStream());
106+
this.onClose = builder.onClose;
107+
}
108+
55109
/**
56110
* Constructs a proxy that shields the given input stream from being closed.
57111
*
@@ -63,16 +117,21 @@ public static CloseShieldInputStream wrap(final InputStream inputStream) {
63117
@Deprecated
64118
public CloseShieldInputStream(final InputStream inputStream) {
65119
super(inputStream);
120+
this.onClose = builder().onClose;
66121
}
67122

68123
/**
69-
* Replaces the underlying input stream with a {@link ClosedInputStream}
70-
* sentinel. The original input stream will remain open, but this proxy will
71-
* appear closed.
124+
* Applies the {@code onClose} function to the underlying input stream, replacing it with the result.
125+
* <p>
126+
* By default, replaces the underlying input stream with a {@link ClosedInputStream} sentinel. The original input stream will remain open, but this proxy
127+
* will appear closed.
128+
* </p>
129+
*
130+
* @throws IOException Thrown by the {@code onClose} function.
72131
*/
73132
@Override
74-
public void close() {
75-
in = ClosedInputStream.INSTANCE;
133+
public void close() throws IOException {
134+
in = onClose.apply(in);
76135
}
77136

78137
}

src/test/java/org/apache/commons/io/input/CloseShieldInputStreamTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
2223

2324
import java.io.ByteArrayInputStream;
@@ -79,6 +80,32 @@ void testClose() throws IOException {
7980
assertEquals(data[0], byteArrayInputStream.read(), "read()");
8081
}
8182

83+
@Test
84+
void testOnClose() throws Exception {
85+
try (InputStream in = CloseShieldInputStream.builder().setInputStream(byteArrayInputStream).setOnClose(is -> {
86+
assertFalse(closed);
87+
closed = true;
88+
return ClosedInputStream.INSTANCE;
89+
}).get()) {
90+
assertEquals(3, in.available());
91+
}
92+
assertTrue(closed);
93+
}
94+
95+
@Test
96+
void testOnCloseException() throws Exception {
97+
final String message = "test";
98+
assertEquals("test", assertThrowsExactly(IOException.class, () -> {
99+
try (InputStream in = CloseShieldInputStream.builder().setInputStream(byteArrayInputStream).setOnClose(is -> {
100+
assertFalse(closed);
101+
throw new IOException(message);
102+
}).get()) {
103+
assertEquals("test", assertThrowsExactly(IOException.class, in::close).getMessage());
104+
}
105+
}).getMessage());
106+
assertFalse(closed);
107+
}
108+
82109
@Test
83110
void testReadAfterCose() throws Exception {
84111
final InputStream shadow;

0 commit comments

Comments
 (0)