1
1
mirror of https://github.com/wader/fq.git synced 2024-12-25 22:34:14 +03:00
fq/pkg/bitio/iobitwriter.go

65 lines
1.0 KiB
Go
Raw Normal View History

package bitio
import (
"io"
)
2022-02-09 03:22:51 +03:00
// IOBitWriter is a bitio.Writer that writes to a io.Writer.
// Use Flush to write possible unaligned byte zero bit padded.
type IOBitWriter struct {
w io.Writer
b Buffer
}
2022-02-09 03:22:51 +03:00
// NewIOBitWriter returns a new bitio.IOBitWriter.
func NewIOBitWriter(w io.Writer) *IOBitWriter {
return &IOBitWriter{w: w}
}
func (w *IOBitWriter) WriteBits(p []byte, nBits int64) (n int64, err error) {
if n, err = w.b.WriteBits(p, nBits); err != nil {
return n, err
}
var sn int64
for {
l := w.b.Len()
if l < 8 {
break
}
var buf [32 * 1024]byte
n, err := w.b.ReadBits(buf[:], l-(l%8))
if err != nil {
return sn, err
}
rn, err := w.w.Write(buf[:n/8])
sn += int64(rn)
if err != nil {
return sn, err
}
sn += n
}
return nBits, nil
}
// Flush write possible unaligned byte zero bit padded.
func (w *IOBitWriter) Flush() error {
if w.b.Len() == 0 {
return nil
}
var buf [1]byte
_, err := w.b.ReadBits(buf[:], w.b.Len())
if err != nil {
return err
}
_, err = w.w.Write(buf[:])
return err
}