diff --git a/bgzf/index/index.go b/bgzf/index/index.go index ee936c8..c8ef342 100644 --- a/bgzf/index/index.go +++ b/bgzf/index/index.go @@ -70,12 +70,9 @@ func (r *ChunkReader) Read(p []byte) (int, error) { // blocked mode and so will stop there anyway. want := int(r.chunks[0].End.Block) if r.chunks[0].End.Block == 0 && r.chunks[0].End.File > last.End.File { - // Special case for when the current end block offset is zero. - // We pick an arbitrary length (the maximum progression). - // Because we must move past the current bgzf block to get - // to the current chunk end this is safe since the bgzf - // Reader is in blocked mode. - want = len(p) + // Special case for when the current end block offset + // is zero. + want = r.r.BlockLen() } var cursor int if last.End.File == r.chunks[0].End.File { diff --git a/bgzf/index/index_test.go b/bgzf/index/index_test.go index 185eaff..903a36b 100644 --- a/bgzf/index/index_test.go +++ b/bgzf/index/index_test.go @@ -265,13 +265,9 @@ func (s *S) TestIssue10(c *check.C) { var got bytes.Buffer io.Copy(&got, cr) - gotString := got.String() - c.Check(strings.Contains(gotString, want), check.Equals, true, + c.Check(got.String(), check.Equals, want, check.Commentf("clean=%t merge=%t trunc=%t chunks=%+v", clean, strategy != nil, truncFinal, chunks), ) - if gotString != want { - c.Logf("read over-run clean=%t merge=%t trunc=%t:\n\tgot: %q\n\twant:%q", clean, strategy != nil, truncFinal, gotString, want) - } } } } diff --git a/bgzf/reader.go b/bgzf/reader.go index 7223e01..b513a14 100644 --- a/bgzf/reader.go +++ b/bgzf/reader.go @@ -487,6 +487,10 @@ func (bg *Reader) Seek(off Offset) error { // seek operation. func (bg *Reader) LastChunk() Chunk { return bg.lastChunk } +// BlockLen returns the number of bytes remaining to be read from the +// current BGZF block. +func (bg *Reader) BlockLen() int { return bg.current.len() } + // Close closes the reader and releases resources. func (bg *Reader) Close() error { if bg.control != nil { @@ -521,9 +525,6 @@ func (bg *Reader) Read(p []byte) (int, error) { for n < len(p) && bg.err == nil { var _n int _n, bg.err = bg.current.Read(p[n:]) - if _n > 0 { - bg.lastChunk.End = bg.current.txOffset() - } n += _n if bg.err == io.EOF { if n == len(p) { @@ -533,6 +534,7 @@ func (bg *Reader) Read(p []byte) (int, error) { if bg.Blocked { bg.err = nil + bg.lastChunk.End = bg.current.txOffset() return n, io.EOF } @@ -543,6 +545,7 @@ func (bg *Reader) Read(p []byte) (int, error) { } } + bg.lastChunk.End = bg.current.txOffset() return n, bg.err }