Skip to content

Commit c3a92cb

Browse files
committed
RandomAccessFileInputStream.available() should return 0 after the stream
is closed
1 parent d13acbd commit c3a92cb

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ The <action> type attribute can be add,update,fix,remove.
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>
8080
<action dev="ggregory" type="fix" due-to="Gary Gregory">ProxyInputStream.read() should return -1 after the stream is closed.</action>
81+
<action dev="ggregory" type="fix" due-to="Gary Gregory">RandomAccessFileInputStream.available() should return 0 after the stream is closed.</action>
8182
<!-- UPDATE -->
8283
<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>
8384
<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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public static Builder builder() {
128128
return new Builder();
129129
}
130130

131+
private boolean closed;
131132
private final boolean propagateClose;
132133
private final RandomAccessFile randomAccessFile;
133134

@@ -179,7 +180,7 @@ public int available() throws IOException {
179180
* @throws IOException If an I/O error occurs.
180181
*/
181182
public long availableLong() throws IOException {
182-
return randomAccessFile.length() - randomAccessFile.getFilePointer();
183+
return closed ? 0 : randomAccessFile.length() - randomAccessFile.getFilePointer();
183184
}
184185

185186
@Override
@@ -188,6 +189,7 @@ public void close() throws IOException {
188189
if (propagateClose) {
189190
randomAccessFile.close();
190191
}
192+
closed = true;
191193
}
192194

193195
/**

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,22 @@ private RandomAccessFile createRandomAccessFile() throws FileNotFoundException {
4747
}
4848

4949
@Test
50-
public void testAvailable() throws IOException {
50+
public void testAvailableAfterOpen() throws IOException {
5151
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
5252
true)) {
5353
assertEquals(DATA_FILE_LEN, inputStream.available());
5454
}
5555
}
5656

57+
@Test
58+
public void testAvailableAfterClose() throws IOException {
59+
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
60+
true)) {
61+
inputStream.close();
62+
assertEquals(0, inputStream.available());
63+
}
64+
}
65+
5766
@Test
5867
public void testAvailableLong() throws IOException {
5968
try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),

0 commit comments

Comments
 (0)