Skip to content

Commit 1378842

Browse files
committed
ProxyInputStream.read() should return -1 after the stream is closed
1 parent f448f97 commit 1378842

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ The <action> type attribute can be add,update,fix,remove.
7777
<action dev="ggregory" type="fix" due-to="Gary Gregory">NullInputStream.read() should return -1 after the stream is closed.</action>
7878
<action dev="ggregory" type="fix" due-to="Gary Gregory">MemoryMappedFileInputStream.available() should return 0 after the stream is closed.</action>
7979
<action dev="ggregory" type="fix" due-to="Gary Gregory">MemoryMappedFileInputStream.read() should return -1 after the stream is closed.</action>
80+
<action dev="ggregory" type="fix" due-to="Gary Gregory">ProxyInputStream.read() should return -1 after the stream is closed.</action>
8081
<!-- UPDATE -->
8182
<action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons.bytebuddy.version from 1.14.13 to 1.14.17 #615, #621, #631, #635.</action>
8283
<action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0.</action>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,16 @@ public boolean markSupported() {
180180
}
181181

182182
/**
183-
* Invokes the delegate's {@code read()} method.
183+
* Invokes the delegate's {@code read()} method unless the stream is closed.
184184
*
185185
* @return the byte read or -1 if the end of stream
186186
* @throws IOException if an I/O error occurs.
187187
*/
188188
@Override
189189
public int read() throws IOException {
190+
if (isClosed()) {
191+
return EOF;
192+
}
190193
try {
191194
beforeRead(1);
192195
final int b = in.read();

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.ByteArrayInputStream;
2525
import java.io.IOException;
2626
import java.io.InputStream;
27+
import java.nio.charset.StandardCharsets;
2728
import java.util.Arrays;
2829

2930
import org.apache.commons.io.IOUtils;
@@ -66,6 +67,13 @@ public void testAvailableAfterClose() throws IOException {
6667
assertEquals(0, shadow.available());
6768
}
6869

70+
@Test
71+
public void testAvailableAfterOpen() throws IOException {
72+
try (T inputStream = createFixture()) {
73+
assertEquals(3, inputStream.available());
74+
}
75+
}
76+
6977
@Test
7078
public void testAvailableAll() throws IOException {
7179
try (T inputStream = createFixture()) {
@@ -110,8 +118,12 @@ public void testRead() throws IOException {
110118
@SuppressWarnings("resource")
111119
@Test
112120
public void testReadAfterClose() throws IOException {
113-
final T shadow;
114-
try (T inputStream = createFixture()) {
121+
InputStream shadow;
122+
try (InputStream inputStream = createFixture()) {
123+
shadow = inputStream;
124+
}
125+
assertEquals(IOUtils.EOF, shadow.read());
126+
try (InputStream inputStream = new ProxyInputStreamFixture(new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)))) {
115127
shadow = inputStream;
116128
}
117129
assertEquals(IOUtils.EOF, shadow.read());

0 commit comments

Comments
 (0)