Skip to content

Commit

Permalink
Add CloseShieldInputStream.systemIn(InputStream)
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jul 5, 2024
1 parent 12dbeaf commit fe6d3c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterator.adapt(Iterable).</action>
<action dev="ggregory" type="add" issue="IO-831" due-to="Elliotte Rusty Harold, Thach Le, Gary Gregory">Add getInputStream() for 'https' and 'http' in URIOrigin #630.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOSupplier.getUnchecked().</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add CloseShieldInputStream.systemIn(InputStream).</action>
<!-- FIX -->
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add missing unit tests.</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">FileUtils.lastModifiedFileTime(File) calls Objects.requireNonNull() on the wrong object.</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
*/
public class CloseShieldInputStream extends ProxyInputStream {

/**
* Constructs a proxy that only shields {@code System.in} from being closed.
*
* @param inputStream the input stream to wrap
* @return the created proxy
* @since 2.17.0
*/
public static InputStream systemIn(final InputStream inputStream) {
return inputStream == System.in ? wrap(inputStream) : inputStream;
}

/**
* Constructs a proxy that shields the given input stream from being closed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -33,7 +34,7 @@ public class CloseShieldInputStreamTest {

private byte[] data;

private InputStream original;
private InputStream byteArrayInputStream;

private InputStream shielded;

Expand All @@ -42,22 +43,40 @@ public class CloseShieldInputStreamTest {
@BeforeEach
public void setUp() {
data = new byte[] { 'x', 'y', 'z' };
original = new ByteArrayInputStream(data) {
byteArrayInputStream = new ByteArrayInputStream(data) {
@Override
public void close() {
closed = true;
}
};
shielded = CloseShieldInputStream.wrap(original);
closed = false;
}

@Test
public void testClose() throws IOException {
shielded = CloseShieldInputStream.wrap(byteArrayInputStream);
shielded.close();
assertFalse(closed, "closed");
assertEquals(-1, shielded.read(), "read()");
assertEquals(data[0], original.read(), "read()");
assertEquals(data[0], byteArrayInputStream.read(), "read()");
}

@Test
public void testSystemInOnSystemInNo() throws IOException {
shielded = CloseShieldInputStream.systemIn(byteArrayInputStream);
shielded.close();
assertTrue(closed, "closed");
assertEquals(data[0], shielded.read(), "read()");
assertEquals(data[1], byteArrayInputStream.read(), "read()");
}

@Test
public void testSystemInOnSystemInYes() throws IOException {
shielded = CloseShieldInputStream.systemIn(System.in);
shielded.close();
assertFalse(closed, "closed");
assertEquals(-1, shielded.read(), "read()");
assertEquals(data[0], byteArrayInputStream.read(), "read()");
}

}

0 comments on commit fe6d3c9

Please sign in to comment.