RandomAccessFileInputStream.available() should return 0 after the stream
is closed
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5971bcc..635fc8b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -78,6 +78,7 @@
<action dev="ggregory" type="fix" due-to="Gary Gregory">MemoryMappedFileInputStream.available() should return 0 after the stream is closed.</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">MemoryMappedFileInputStream.read() should return -1 after the stream is closed.</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">ProxyInputStream.read() should return -1 after the stream is closed.</action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">RandomAccessFileInputStream.available() should return 0 after the stream is closed.</action>
<!-- UPDATE -->
<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>
<action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0.</action>
diff --git a/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java b/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
index c23b62b..77aea3f 100644
--- a/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
@@ -128,6 +128,7 @@ public static Builder builder() {
return new Builder();
}
+ private boolean closed;
private final boolean propagateClose;
private final RandomAccessFile randomAccessFile;
@@ -179,7 +180,7 @@ public int available() throws IOException {
* @throws IOException If an I/O error occurs.
*/
public long availableLong() throws IOException {
- return randomAccessFile.length() - randomAccessFile.getFilePointer();
+ return closed ? 0 : randomAccessFile.length() - randomAccessFile.getFilePointer();
}
@Override
@@ -188,6 +189,7 @@ public void close() throws IOException {
if (propagateClose) {
randomAccessFile.close();
}
+ closed = true;
}
/**
diff --git a/src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java b/src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java
index 0892d3a..3d1ee14 100644
--- a/src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java
@@ -47,7 +47,7 @@ private RandomAccessFile createRandomAccessFile() throws FileNotFoundException {
}
@Test
- public void testAvailable() throws IOException {
+ public void testAvailableAfterOpen() throws IOException {
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
true)) {
assertEquals(DATA_FILE_LEN, inputStream.available());
@@ -55,6 +55,15 @@ public void testAvailable() throws IOException {
}
@Test
+ public void testAvailableAfterClose() throws IOException {
+ try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
+ true)) {
+ inputStream.close();
+ assertEquals(0, inputStream.available());
+ }
+ }
+
+ @Test
public void testAvailableLong() throws IOException {
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
true)) {