Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinaryCodec.readBytesOrFewer fails if no data is requested AND no date is available in the underlying input stream #1188

Merged
merged 3 commits into from
Oct 5, 2018
Merged
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
5 changes: 4 additions & 1 deletion src/main/java/htsjdk/samtools/util/BinaryCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,10 @@ public int readBytesOrFewer(final byte[] buffer, final int offset, final int len
throw new IllegalStateException("Calling read method on BinaryCodec open for write.");
}
try {
return inputStream.read(buffer, offset, length);
// Some implementations of InputStream do not behave well when the buffer is empty and length is zero, for
// example ByteArrayInputStream, so we must check for length equal to zero.
// See: https://bugs.java.com/view_bug.do?bug_id=6766844
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good find, thanks for adding this note.

return (length == 0) ? 0 : inputStream.read(buffer, offset, length);
} catch (IOException e) {
throw new RuntimeIOException(constructErrorMessage("Read error"), e);
}
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/htsjdk/samtools/util/BinaryCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;


/*
Expand Down Expand Up @@ -284,5 +279,11 @@ public void timeTest() throws IOException{
readCodec.close();
}


@Test
public void testReadBytesOrFewerNoneAvailable() {
final byte[] value = new byte[0];
final ByteArrayInputStream instream = new ByteArrayInputStream(value);
final BinaryCodec readCodec = new BinaryCodec(instream);
Assert.assertEquals(readCodec.readBytesOrFewer(value, 0, 0), 0);
}
}