Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading