From 6ed9eb3da9eed02e54dd893a4f4fb60b4caa514b Mon Sep 17 00:00:00 2001 From: Yossi Farjoun Date: Tue, 18 Dec 2012 17:22:34 -0500 Subject: [PATCH] GATKBAMIndex now passes unit test! Problem was that SeekableBufferedStream seems to have a bug: it will read beyond the end of a file if asked to. --- .../sting/gatk/datasources/reads/GATKBAMIndex.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/public/java/src/org/broadinstitute/sting/gatk/datasources/reads/GATKBAMIndex.java b/public/java/src/org/broadinstitute/sting/gatk/datasources/reads/GATKBAMIndex.java index abaa0b226..8f5f420fd 100644 --- a/public/java/src/org/broadinstitute/sting/gatk/datasources/reads/GATKBAMIndex.java +++ b/public/java/src/org/broadinstitute/sting/gatk/datasources/reads/GATKBAMIndex.java @@ -359,7 +359,10 @@ public class GATKBAMIndex { int bytesExpected = buffer.limit(); //BufferedInputStream cannot read directly into a byte buffer, so we read into an array //and put the result into the bytebuffer after the if statement. - int bytesRead = bufferedStream.read(byteArray,0,bytesExpected); + + //SeekableBufferedStream is evil, it will "read" beyond the end of the file if you let it! + final int bytesToRead = (int) Math.min(bufferedStream.length() - bufferedStream.position(), bytesExpected); //min of int and long will definitely be castable to an int. + int bytesRead = bufferedStream.read(byteArray,0,bytesToRead); // We have a rigid expectation here to read in exactly the number of bytes we've limited // our buffer to -- if we read in fewer bytes than this, or encounter EOF (-1), the index