Skip to content

Commit

Permalink
fixing bug in SeekableHttpStream.read (#1182)
Browse files Browse the repository at this point in the history
* fixing bug in SeekableHttpStream.read

* a bug in SeekableHttpStream could cause clients to go into an infinite loop because read would return 0 when trying to read one byte past the end of the file
* fixes #1181
  • Loading branch information
lbergelson authored Sep 26, 2018
1 parent a762262 commit 49c70e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ public int read(byte[] buffer, int offset, int len) throws IOException {
if (offset < 0 || len < 0 || (offset + len) > buffer.length) {
throw new IndexOutOfBoundsException("Offset="+offset+",len="+len+",buflen="+buffer.length);
}
if (len == 0 || position == contentLength) {
if (len == 0 ) {
return 0;
}
if (position == contentLength) {
return -1;
}

HttpURLConnection connection = null;
InputStream is = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public void testRandomRead() throws IOException {
assertEquals(buffer1, buffer2);
}

@Test
public void testReadExactlyOneByteAtEndOfFile() throws IOException {
try (final SeekableStream stream = new SeekableHTTPStream(new URL(BAM_URL_STRING))){
byte[] buff = new byte[1];
long length = stream.length();
stream.seek(length - 1);
Assert.assertFalse(stream.eof());
Assert.assertEquals(stream.read(buff), 1);
Assert.assertTrue(stream.eof());
Assert.assertEquals(stream.read(buff), -1);
}
}


/**
* Test an attempt to read past the end of the file. The test file is 594,149 bytes in length. The test
* attempts to read a 1000 byte block starting at position 594000. A correct result would return 149 bytes.
Expand Down

0 comments on commit 49c70e5

Please sign in to comment.