Skip to content

Commit 7ef6378

Browse files
committed
RandomAccessFileInputStream.read() should return -1 (EOF) after the
stream is closed
1 parent a987fa8 commit 7ef6378

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ The <action> type attribute can be add,update,fix,remove.
7979
<action dev="ggregory" type="fix" due-to="Gary Gregory">MemoryMappedFileInputStream.read() should return -1 after the stream is closed.</action>
8080
<action dev="ggregory" type="fix" due-to="Gary Gregory">ProxyInputStream.read() should return -1 after the stream is closed.</action>
8181
<action dev="ggregory" type="fix" due-to="Gary Gregory">RandomAccessFileInputStream.available() should return 0 after the stream is closed.</action>
82+
<action dev="ggregory" type="fix" due-to="Gary Gregory">RandomAccessFileInputStream.read() should return -1 (EOF) after the stream is closed.</action>
8283
<!-- UPDATE -->
8384
<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>
8485
<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/RandomAccessFileInputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.RandomAccessFile;
2424
import java.util.Objects;
2525

26+
import org.apache.commons.io.IOUtils;
2627
import org.apache.commons.io.RandomAccessFileMode;
2728
import org.apache.commons.io.build.AbstractOrigin;
2829
import org.apache.commons.io.build.AbstractStreamBuilder;
@@ -212,7 +213,7 @@ public boolean isCloseOnClose() {
212213

213214
@Override
214215
public int read() throws IOException {
215-
return randomAccessFile.read();
216+
return closed ? IOUtils.EOF : randomAccessFile.read();
216217
}
217218

218219
@Override

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.file.Paths;
3434
import java.nio.file.StandardOpenOption;
3535

36+
import org.apache.commons.io.IOUtils;
3637
import org.apache.commons.io.RandomAccessFileMode;
3738
import org.junit.jupiter.api.Test;
3839

@@ -199,6 +200,15 @@ public void testRead() throws IOException {
199200
}
200201
}
201202

203+
@Test
204+
public void testReadAfterClose() throws IOException {
205+
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
206+
true)) {
207+
inputStream.close();
208+
assertEquals(IOUtils.EOF, inputStream.read());
209+
}
210+
}
211+
202212
@Test
203213
public void testReadByteArray() throws IOException {
204214
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),

0 commit comments

Comments
 (0)