1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 21:31:33 +03:00
fq/internal/bitioextra/bitioextra.go
Mattias Wadman 7c5215347d bitio,decode: Refactor bitio usage and make buffer slicing more correct
Remove bitio.Buffer layer. bitio.Buffer was a kitchen sink layer with helpers
now it's just a buffer and most functions have been moved to decode instead.

bitio package now only have primitive types and functions simialar to standard
library io and bytes packages.

Make nearly eveything internally use bitio.Bit* interfaces so that slicing work
correctly this will also make it possible to start experimenting with more
complicated silcing helpers, ex things like:
breplace(.header.bitrate; 123) to get a new buffer with bitrate changed.
2022-02-04 21:41:53 +01:00

57 lines
1.2 KiB
Go

package bitioextra
// bitio helpers that don't really belong in bitio
import (
"errors"
"io"
"github.com/wader/fq/pkg/bitio"
)
func CopyBitsBuffer(dst io.Writer, src bitio.Reader, buf []byte) (int64, error) {
return io.CopyBuffer(dst, bitio.NewIOReader(src), buf)
}
func CopyBits(dst io.Writer, src bitio.Reader) (int64, error) {
return CopyBitsBuffer(dst, src, nil)
}
func Clone(br bitio.ReaderAtSeeker) (bitio.ReaderAtSeeker, error) {
l, err := Len(br)
if err != nil {
return nil, err
}
return bitio.NewSectionReader(br, 0, l), nil
}
func Len(br bitio.ReadAtSeeker) (int64, error) {
bPos, err := br.SeekBits(0, io.SeekCurrent)
if err != nil {
return 0, err
}
bEnd, err := br.SeekBits(0, io.SeekEnd)
if err != nil {
return 0, err
}
if _, err := br.SeekBits(bPos, io.SeekStart); err != nil {
return 0, err
}
return bEnd, nil
}
func Range(br bitio.ReadAtSeeker, firstBitOffset int64, nBits int64) (bitio.ReaderAtSeeker, error) {
l, err := Len(br)
if err != nil {
return nil, err
}
// TODO: move error check?
if nBits < 0 {
return nil, errors.New("negative nBits")
}
if firstBitOffset+nBits > l {
return nil, errors.New("outside buffer")
}
return bitio.NewSectionReader(br, firstBitOffset, nBits), nil
}