func (w *Writer) VOffset() uint64 {
	return w.coffset<<16 | uint64(w.original.Len())
}
VOffset returns the virtual-offset of the next byte to be written.
package bgzf
import (
	"bytes"
	"fmt"
	"io"
	"github.com/grailbio/base/compress/libdeflate"
	"v.io/x/lib/vlog"
)
const (
	DefaultUncompressedBlockSize = 0x0ff00
	MaxUncompressedBlockSize = 0x10000
	compressedBlockSize = 0x10000
)
var (
	bgzfExtra       = [...]byte{66, 67, 2, 0, 0, 0}
	bgzfExtraPrefix = [...]byte{66, 67, 2, 0}
	terminator = []byte{
		0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x42, 0x43,
		0x02, 0x00, 0x1b, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	}
)
type compressFactory interface {
	create(io.Writer) (io.WriteCloser, error)
}
type deflateFactory struct {
	level    int
	dfWriter *libdeflate.Writer
}
func (c *deflateFactory) create(w io.Writer) (io.WriteCloser, error) {
	if c.dfWriter == nil {
		var err error
		c.dfWriter, err = libdeflate.NewWriterLevel(w, c.level)
		if err != nil {
			return nil, err
		}
	} else {
		c.dfWriter.Reset(w)
	}
	c.dfWriter.Header.Extra = make([]byte, len(bgzfExtra))
	copy(c.dfWriter.Header.Extra[:], bgzfExtra[:])
	c.dfWriter.Header.OS = 0xff  
	return c.dfWriter, nil
}
type Writer struct {
	factory          compressFactory
	uncompressedSize int
	xfl              int
	w                io.Writer
	original         bytes.Buffer
	compressed       bytes.Buffer
	writer           io.WriteCloser
	coffset          uint64  
}
func NewWriter(w io.Writer, level int) (*Writer, error) {
	return &Writer{
		factory:          &deflateFactory{level, nil},
		uncompressedSize: DefaultUncompressedBlockSize,
		xfl:              -1,
		w:                w,
	}, nil
}
func (w *Writer) Write(buf []byte) (int, error) {
	for i := 0; i < len(buf); {
		end := len(buf)
		limit := i + w.uncompressedSize - w.original.Len()
		if limit < end {
			end = limit
		}
		n, _ := w.original.Write(buf[i:end])
		i += n
		if err := w.tryCompress(false); err != nil {
			return i, err
		}
	}
	return len(buf), nil
}
func (w *Writer) CloseWithoutTerminator() error {
	return w.tryCompress(true)
}
func (w *Writer) Close() error {
	if err := w.CloseWithoutTerminator(); err != nil {
		return err
	}
	_, err := w.w.Write([]byte(terminator))
	return err
}
func (w *Writer) tryCompress(compressRemainder bool) error {
	for w.original.Len() >= w.uncompressedSize || (compressRemainder && w.original.Len() > 0) {
		var err error
		w.writer, err = w.factory.create(&w.compressed)
		if err != nil {
			return err
		}
		if w.original.Len() > 0 {
			_, err := w.writer.Write(w.original.Next(w.uncompressedSize))
			if err != nil {
				return err
			}
		}
		if err := w.writer.Close(); err != nil {
			return err
		}
		b := w.compressed.Bytes()
		if w.xfl >= 0 {
			offset := 8  
			b[offset] = byte(w.xfl)
		}
		offset := 12  
		bsize := w.compressed.Len() - 1
		if bsize >= compressedBlockSize {
			return fmt.Errorf("bgzf compressed block is too big: %d > %d", bsize,
				compressedBlockSize)
		}
		if w.compressed.Len() < (offset + len(bgzfExtra)) {
			vlog.Fatalf("compressed length is too short: %d < %d", w.compressed.Len(),
				offset+len(bgzfExtra))
		}
		if !bytes.Equal(b[offset:offset+len(bgzfExtraPrefix)], bgzfExtraPrefix[:]) {
			vlog.Fatalf("could not find bgzf extra prefix")
		}
		b[offset+4] = byte(bsize)
		b[offset+5] = byte(bsize >> 8)
		sz := w.compressed.Len()
		if _, err := w.compressed.WriteTo(w.w); err != nil {
			return err
		}
		w.coffset += uint64(sz)
	}
	return nil
}