Documentation
¶
Overview ¶
Package iobit provides primitives for reading & writing bits
The main purpose of this library is to remove the need to write custom bit-masks when reading or writing bitstreams, and to ease maintenance. This is true especially when you need to read/write data which is not aligned on bytes.
Errors are sticky so you can check for errors after a chunk of meaningful work rather than after every operation.
For example, with iobit you can read an MPEG-TS PCR like this:
r := iobit.NewReader(buffer) base := r.Uint64(33) // PCR base is 33-bits r.Skip(6) // 6-bits are reserved extension := r.Uint64(9) // PCR extension is 9-bits
instead of:
base = uint64(buffer[0]) << 25 base |= uint64(buffer[1]) << 17 base |= uint64(buffer[2]) << 9 base |= uint64(buffer[3]) << 1 base |= buffer[4] >> 7 extension := uint16(buffer[4] & 0x1) << 8 extension |= buffer[5]
and write it like this:
w := iobit.NewWriter(buffer) w.PutUint64(33, base) w.PutUint32(6, 0) w.PutUint32(9, extension)
Index ¶
- Variables
- type Reader
- func (r *Reader) At() uint
- func (r *Reader) Be16() uint16
- func (r *Reader) Be32() uint32
- func (r *Reader) Be64() uint64
- func (r *Reader) Bit() bool
- func (r *Reader) Byte() uint8
- func (r *Reader) Bytes(size int) []byte
- func (r *Reader) Error() error
- func (r *Reader) Int16(bits uint) int16
- func (r *Reader) Int32(bits uint) int32
- func (r *Reader) Int64(bits uint) int64
- func (r *Reader) Int8(bits uint) int8
- func (r *Reader) Le16() uint16
- func (r *Reader) Le32() uint32
- func (r *Reader) Le64() uint64
- func (r *Reader) LeftBits() uint
- func (r *Reader) LeftBytes() []byte
- func (r *Reader) Peek() *Reader
- func (r *Reader) Reset()
- func (r *Reader) Skip(bits uint)
- func (r *Reader) String(size int) string
- func (r *Reader) Uint16(bits uint) uint16
- func (r *Reader) Uint32(bits uint) uint32
- func (r *Reader) Uint64(bits uint) uint64
- func (r *Reader) Uint8(bits uint) uint8
- type Writer
- func (w *Writer) Bits() int
- func (w *Writer) Bytes() []byte
- func (w *Writer) Flush() error
- func (w *Writer) Index() int
- func (w *Writer) PutBe16(val uint16)
- func (w *Writer) PutBe32(val uint32)
- func (w *Writer) PutBe64(val uint64)
- func (w *Writer) PutBit(val bool)
- func (w *Writer) PutByte(val byte)
- func (w *Writer) PutInt16(bits uint, val int16)
- func (w *Writer) PutInt32(bits uint, val int32)
- func (w *Writer) PutInt64(bits uint, val int64)
- func (w *Writer) PutInt8(bits uint, val int8)
- func (w *Writer) PutLe16(val uint16)
- func (w *Writer) PutLe32(val uint32)
- func (w *Writer) PutLe64(val uint64)
- func (w *Writer) PutUint16(bits uint, val uint16)
- func (w *Writer) PutUint32(bits uint, val uint32)
- func (w *Writer) PutUint64(bits uint, val uint64)
- func (w *Writer) PutUint8(bits uint, val byte)
- func (w *Writer) Reset()
- func (w *Writer) Write(p []byte) (int, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrOverflow happens when trying to write too many bits ErrOverflow = errors.New("bit overflow") // ErrUnderflow happens when flushing unaligned writers ErrUnderflow = errors.New("bit underflow") )
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader wraps a raw byte array and provides multiple methods to read and skip data bit-by-bit. Its methods don't return the usual error as it is too expensive. Instead, read errors can be checked with the Check() method
func (*Reader) LeftBytes ¶
LeftBytes returns a slice of the contents of the unread reader portion. Note that this slice is byte aligned even if the reader is not.
func (*Reader) Peek ¶
Peek returns a reader copy. Useful to read data without advancing the original reader.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer wraps a raw byte array and provides multiple methoods to write data bit-by-bit Its methods don't return the usual error as it is too expensive. Instead, write errors can be checked with the Flush() method.
func (*Writer) Bytes ¶
Bytes returns a byte array of what's left to write. Note that this array is 8-bit aligned even if the writer is not.
func (*Writer) Flush ¶
Flush flushes the writer to its underlying buffer. Returns ErrUnderflow if the output is not byte-aligned. Returns ErrOverflow if the output array is too small.