Skip to content

Commit 058de05

Browse files
committed
[IO-799] ReaderInputStream.read() throws an exception instead of
returning -1 when called again after returning -1.
1 parent 60392c9 commit 058de05

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ The <action> type attribute can be add,update,fix,remove.
4747
</properties>
4848

4949
<body>
50+
<release version="2.13.1" date="2023-MM-DD" description="Java 8 required.">
51+
<action dev="ggregory" type="fix" issue="IO-799" due-to="Jeroen van der Vegt, Gary Gregory">
52+
ReaderInputStream.read() throws an exception instead of returning -1 when called again after returning -1.
53+
</action>
54+
</release>
5055
<release version="2.13.0" date="2023-06-03" description="Java 8 required.">
5156
<!-- FIX -->
5257
<action issue="IO-791" dev="ggregory" type="fix" due-to="Chad Wilson, Gary Gregory">

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ public void close() throws IOException {
339339
* @throws IOException If an I/O error occurs
340340
*/
341341
private void fillBuffer() throws IOException {
342+
if (endOfInput) {
343+
return;
344+
}
342345
if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) {
343346
encoderIn.compact();
344347
final int position = encoderIn.position();

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ public void testLargeUTF8WithSingleByteRead() throws IOException {
209209
testWithSingleByteRead(LARGE_TEST_STRING, UTF_8);
210210
}
211211

212+
@Test
213+
public void testReadEofTwice() throws IOException {
214+
try (ReaderInputStream reader = ReaderInputStream.builder().setCharset(StandardCharsets.UTF_8).setReader(new StringReader("123")).get()) {
215+
assertEquals('1', reader.read());
216+
assertEquals('2', reader.read());
217+
assertEquals('3', reader.read());
218+
assertEquals(-1, reader.read());
219+
assertEquals(-1, reader.read());
220+
}
221+
}
222+
212223
@SuppressWarnings("deprecation")
213224
@Test
214225
public void testReadZero() throws Exception {
@@ -220,7 +231,6 @@ public void testReadZero() throws Exception {
220231
testReadZero(inStr, inputStream);
221232
}
222233
}
223-
224234
private void testReadZero(final String inStr, final ReaderInputStream inputStream) throws IOException {
225235
final byte[] bytes = new byte[30];
226236
assertEquals(0, inputStream.read(bytes, 0, 0));

0 commit comments

Comments
 (0)