1
1
mirror of https://github.com/wader/fq.git synced 2024-12-29 16:42:06 +03:00
fq/pkg/bitio/ioreadseeker.go
2022-02-09 01:35:47 +01:00

39 lines
807 B
Go

package bitio
// IOReadSeeker is a io.ReadSeeker that reads from a bitio.ReadSeeker.
// Unaligned byte at EOF will be zero bit padded.
type IOReadSeeker struct {
IOReader
s Seeker
sPos int64
}
// NewIOReadSeeker return a new bitio.IOReadSeeker.
func NewIOReadSeeker(rs ReadSeeker) *IOReadSeeker {
return &IOReadSeeker{
IOReader: IOReader{r: rs},
s: rs,
}
}
func (r *IOReadSeeker) Read(p []byte) (n int, err error) {
n, err = r.IOReader.Read(p)
r.sPos += int64(n)
return n, err
}
func (r *IOReadSeeker) Seek(offset int64, whence int) (int64, error) {
n, err := r.s.SeekBits(offset*8, whence)
// TODO: reset last error on seek. some nicer way?
r.IOReader.rErr = nil
if n != r.sPos {
r.b.Reset()
r.sPos = n / 8
}
if err != nil {
return n / 8, err
}
return n / 8, err
}