mirror of
https://github.com/wader/fq.git
synced 2024-11-27 06:04:47 +03:00
684a083813
Will be used for probing via -d <string> and filename
40 lines
766 B
Go
40 lines
766 B
Go
package bitio
|
|
|
|
import (
|
|
"io"
|
|
)
|
|
|
|
// LimitReader is a bitio.Reader that reads a limited amount of bits from a bitio.Reader.
|
|
// Similar to bytes.LimitedReader.
|
|
type LimitReader struct {
|
|
r Reader
|
|
n int64
|
|
}
|
|
|
|
// NewLimitReader returns a new bitio.LimitReader.
|
|
func NewLimitReader(r Reader, n int64) *LimitReader { return &LimitReader{r, n} }
|
|
|
|
func (l *LimitReader) ReadBits(p []byte, nBits int64) (n int64, err error) {
|
|
if l.n <= 0 {
|
|
return 0, io.EOF
|
|
}
|
|
if nBits > l.n {
|
|
nBits = l.n
|
|
}
|
|
n, err = l.r.ReadBits(p, nBits)
|
|
l.n -= n
|
|
return n, err
|
|
}
|
|
|
|
func (l *LimitReader) CloneReader() (Reader, error) {
|
|
rc, err := CloneReader(l.r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &LimitReader{r: rc, n: l.n}, nil
|
|
}
|
|
|
|
func (r *LimitReader) Unwrap() any {
|
|
return r.r
|
|
}
|