Skip to content

Commit

Permalink
bgzf/index: use correct end of chunk for identification
Browse files Browse the repository at this point in the history
Fixes #10.
  • Loading branch information
kortschak committed Sep 15, 2015
1 parent 05760fd commit 9e97d38
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions bgzf/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,25 @@ func NewChunkReader(r *bgzf.Reader, chunks []bgzf.Chunk) (*ChunkReader, error) {

// Read satisfies the io.Reader interface.
func (r *ChunkReader) Read(p []byte) (int, error) {
if len(r.chunks) == 0 || vOffset(r.r.LastChunk().End) >= vOffset(r.chunks[0].End) {
if len(r.chunks) == 0 {
return 0, io.EOF
}
last := r.r.LastChunk()
if vOffset(last.End) >= vOffset(r.chunks[0].End) {
return 0, io.EOF
}

// Ensure the byte slice does not extend beyond the end of
// the current chunk. We do not need to consider reading
// beyond the end of the block because the bgzf.Reader is in
// blocked mode and so will stop there anyway.
if r.r.LastChunk().End.File == r.chunks[0].End.File {
p = p[:min(len(p), int(r.chunks[0].End.Block-r.r.LastChunk().End.Block))]
// If we are in the same block as the last read, set the
// cursor to the end of that read.
var cursor uint16
if last.Begin.File == r.chunks[0].Begin.File {
cursor = last.End.Block
}
p = p[:min(len(p), int(r.chunks[0].End.Block-cursor))]

n, err := r.r.Read(p)
if err != nil {
Expand All @@ -69,13 +77,17 @@ func (r *ChunkReader) Read(p []byte) (int, error) {
}
return n, err
}
if len(r.chunks) != 0 && vOffset(r.r.LastChunk().End) >= vOffset(r.chunks[0].End) {

// Check for non-progress and terminate when this happens.
this := r.r.LastChunk()
if this == last || vOffset(this.End) >= vOffset(r.chunks[0].End) {
r.chunks = r.chunks[1:]
if len(r.chunks) == 0 {
return n, io.EOF
}
err = r.r.Seek(r.chunks[0].Begin)
}

return n, err
}

Expand Down

0 comments on commit 9e97d38

Please sign in to comment.