Skip to content

Commit 2451134

Browse files
committed
ReaderInputStream.available() should return 0 after the stream is closed
ReaderInputStream.read() should return -1 (EOF) after the stream is closed
1 parent 305c003 commit 2451134

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ The <action> type attribute can be add,update,fix,remove.
8181
<action dev="ggregory" type="fix" due-to="Gary Gregory">ProxyInputStream.read() should return -1 (EOF) after the stream is closed.</action>
8282
<action dev="ggregory" type="fix" due-to="Gary Gregory">RandomAccessFileInputStream.available() should return 0 after the stream is closed.</action>
8383
<action dev="ggregory" type="fix" due-to="Gary Gregory">RandomAccessFileInputStream.read() should return -1 (EOF) after the stream is closed.</action>
84+
<action dev="ggregory" type="fix" due-to="Gary Gregory">ReaderInputStream.available() should return 0 after the stream is closed.</action>
85+
<action dev="ggregory" type="fix" due-to="Gary Gregory">ReaderInputStream.read() should return -1 (EOF) after the stream is closed.</action>
8486
<!-- UPDATE -->
8587
<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>
8688
<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/ReaderInputStream.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
* @see org.apache.commons.io.output.WriterOutputStream
8686
* @since 2.0
8787
*/
88-
public class ReaderInputStream extends InputStream {
88+
public class ReaderInputStream extends AbstractInputStream {
8989

9090
// @formatter:off
9191
/**
@@ -338,6 +338,14 @@ public ReaderInputStream(final Reader reader, final String charsetName, final in
338338
this(reader, Charsets.toCharset(charsetName), bufferSize);
339339
}
340340

341+
@Override
342+
public int available() throws IOException {
343+
if (encoderOut.hasRemaining()) {
344+
return encoderOut.remaining();
345+
}
346+
return 0;
347+
}
348+
341349
/**
342350
* Closes the stream. This method will cause the underlying {@link Reader} to be closed.
343351
*
@@ -346,6 +354,7 @@ public ReaderInputStream(final Reader reader, final String charsetName, final in
346354
@Override
347355
public void close() throws IOException {
348356
reader.close();
357+
super.close();
349358
}
350359

351360
/**
@@ -399,6 +408,9 @@ CharsetEncoder getCharsetEncoder() {
399408
*/
400409
@Override
401410
public int read() throws IOException {
411+
if (isClosed()) {
412+
return IOUtils.EOF;
413+
}
402414
for (;;) {
403415
if (encoderOut.hasRemaining()) {
404416
return encoderOut.get() & 0xFF;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,37 @@ static Stream<Arguments> charsetData() {
7373

7474
private final Random random = new Random();
7575

76+
private ReaderInputStream createInputStream() throws IOException {
77+
return ReaderInputStream.builder().setReader(new StringReader(TEST_STRING)).get();
78+
}
79+
80+
@Test
81+
public void testAvailableAfterClose() throws IOException {
82+
try (InputStream inputStream = createInputStream()) {
83+
inputStream.close();
84+
assertEquals(0, inputStream.available());
85+
}
86+
}
87+
88+
@Test
89+
public void testReasdAfterClose() throws IOException {
90+
try (InputStream inputStream = createInputStream()) {
91+
inputStream.close();
92+
assertEquals(IOUtils.EOF, inputStream.read());
93+
}
94+
}
95+
96+
@Test
97+
public void testAvailableAfterOpen() throws IOException {
98+
try (InputStream inputStream = createInputStream()) {
99+
// Nothing read, may block
100+
assertEquals(0, inputStream.available());
101+
// Read/block
102+
inputStream.read();
103+
assertEquals(TEST_STRING.length() - 1, inputStream.available());
104+
}
105+
}
106+
76107
@Test
77108
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
78109
public void testBufferSmallest() throws IOException {

0 commit comments

Comments
 (0)