diff --git a/core/src/main/java/io/roastedroot/zerofs/ZeroFsInputStream.java b/core/src/main/java/io/roastedroot/zerofs/ZeroFsInputStream.java index 8754fbee9..f567f35f6 100644 --- a/core/src/main/java/io/roastedroot/zerofs/ZeroFsInputStream.java +++ b/core/src/main/java/io/roastedroot/zerofs/ZeroFsInputStream.java @@ -66,6 +66,9 @@ public int read(byte[] b, int off, int len) throws IOException { private synchronized int readInternal(byte[] b, int off, int len) throws IOException { checkNotClosed(); + if (len == 0) { + return 0; + } if (finished) { return -1; } diff --git a/core/src/test/java/io/roastedroot/zerofs/ZeroFsInputStreamTest.java b/core/src/test/java/io/roastedroot/zerofs/ZeroFsInputStreamTest.java index 002c4aab4..5e78aa091 100644 --- a/core/src/test/java/io/roastedroot/zerofs/ZeroFsInputStreamTest.java +++ b/core/src/test/java/io/roastedroot/zerofs/ZeroFsInputStreamTest.java @@ -106,6 +106,25 @@ public void testRead_partialArray_invalidInput() throws IOException { } } + @Test + public void testRead_zeroLength_returnsZero() throws IOException { + ZeroFsInputStream in = newInputStream(1, 2, 3); + byte[] bytes = new byte[10]; + assertEquals(0, in.read(bytes, 0, 0)); + assertArrayEquals(new byte[10], bytes); // nothing should be read + assertEquals(1, in.read()); // next read should still be the first byte + } + + @Test + public void testRead_zeroLengthAtEof_returnsZero() throws IOException { + ZeroFsInputStream in = newInputStream(1, 2, 3); + assertEquals(3, in.read(new byte[3])); + assertEmpty(in); // we're at the end of the stream + + // now try reading 0 bytes at the end of the stream + assertEquals(0, in.read(new byte[10], 0, 0)); + } + @Test public void testAvailable() throws IOException { ZeroFsInputStream in = newInputStream(1, 2, 3, 4, 5, 6, 7, 8); diff --git a/core/src/test/java/io/roastedroot/zerofs/ZeroFsUnixLikeFileSystemTest.java b/core/src/test/java/io/roastedroot/zerofs/ZeroFsUnixLikeFileSystemTest.java index 1ba48d1f9..ca0196d21 100644 --- a/core/src/test/java/io/roastedroot/zerofs/ZeroFsUnixLikeFileSystemTest.java +++ b/core/src/test/java/io/roastedroot/zerofs/ZeroFsUnixLikeFileSystemTest.java @@ -1077,7 +1077,7 @@ public void testDelete_file_whenOpenReferencesRemain() throws IOException { byte[] allBytes = new byte[105]; int off = 0; int read; - while ((read = in.read(allBytes, off, allBytes.length - off)) != -1) { + while ((read = in.read(allBytes, off, allBytes.length - off)) > 0) { off += read; } assertArrayEquals(concat(bytes, moreBytes), allBytes);