iobit

package module
v0.0.0-...-498159a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2017 License: MIT Imports: 2 Imported by: 9

README

iobit GoDoc Build Status

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.

Download:

go get github.com/bamiaux/iobit

Full documentation at http://godoc.org/github.com/bamiaux/iobit


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)

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

Constants

This section is empty.

Variables

View Source
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 NewReader

func NewReader(src []byte) Reader

NewReader returns a new reader reading from <src> byte array.

func (*Reader) At

func (r *Reader) At() uint

At returns the current reader position in bits.

func (*Reader) Be16

func (r *Reader) Be16() uint16

Be16 reads 16 unsigned bits in big-endian order.

func (*Reader) Be32

func (r *Reader) Be32() uint32

Be32 reads 32 unsigned bits in big-endian order.

func (*Reader) Be64

func (r *Reader) Be64() uint64

Be64 reads 64 unsigned bits in big-endian order.

func (*Reader) Bit

func (r *Reader) Bit() bool

Bit reads the next bit as a boolean.

func (*Reader) Byte

func (r *Reader) Byte() uint8

Byte reads one byte.

func (*Reader) Bytes

func (r *Reader) Bytes(size int) []byte

Bytes returns a byte-array of input size.

func (*Reader) Error

func (r *Reader) Error() error

Error returns whether the reader encountered an error.

func (*Reader) Int16

func (r *Reader) Int16(bits uint) int16

Int16 reads up to 16 signed bits in big-endian order.

func (*Reader) Int32

func (r *Reader) Int32(bits uint) int32

Int32 reads up to 32 signed bits in big-endian order.

func (*Reader) Int64

func (r *Reader) Int64(bits uint) int64

Int64 reads up to 64 signed bits in big-endian order.

func (*Reader) Int8

func (r *Reader) Int8(bits uint) int8

Int8 reads up to 8 signed bits in big-endian order.

func (*Reader) Le16

func (r *Reader) Le16() uint16

Le16 reads 16 unsigned bits in litle-endian order.

func (*Reader) Le32

func (r *Reader) Le32() uint32

Le32 reads 32 unsigned bits in little-endian order.

func (*Reader) Le64

func (r *Reader) Le64() uint64

Le64 reads 64 unsigned bits in little-endian order.

func (*Reader) LeftBits

func (r *Reader) LeftBits() uint

LeftBits returns the number of bits left to read.

func (*Reader) LeftBytes

func (r *Reader) LeftBytes() []byte

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

func (r *Reader) Peek() *Reader

Peek returns a reader copy. Useful to read data without advancing the original reader.

func (*Reader) Reset

func (r *Reader) Reset()

Reset resets the reader to its initial position.

func (*Reader) Skip

func (r *Reader) Skip(bits uint)

Skip skips n bits.

func (*Reader) String

func (r *Reader) String(size int) string

String returns a string of input size.

func (*Reader) Uint16

func (r *Reader) Uint16(bits uint) uint16

Uint16 reads up to 16 unsigned bits in big-endian order.

func (*Reader) Uint32

func (r *Reader) Uint32(bits uint) uint32

Uint32 reads up to 32 unsigned bits in big-endian order.

func (*Reader) Uint64

func (r *Reader) Uint64(bits uint) uint64

Uint64 reads up to 64 unsigned bits in big-endian order.

func (*Reader) Uint8

func (r *Reader) Uint8(bits uint) uint8

Uint8 reads up to 8 unsigned bits in big-endian order.

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 NewWriter

func NewWriter(dst []byte) Writer

NewWriter returns a new writer writing to output byte array.

func (*Writer) Bits

func (w *Writer) Bits() int

Bits returns the number of bits available to write.

func (*Writer) Bytes

func (w *Writer) Bytes() []byte

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

func (w *Writer) Flush() error

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.

func (*Writer) Index

func (w *Writer) Index() int

Index returns the current writer position in bits.

func (*Writer) PutBe16

func (w *Writer) PutBe16(val uint16)

PutBe16 writes 16 bits in big-endian order.

func (*Writer) PutBe32

func (w *Writer) PutBe32(val uint32)

PutBe32 writes 32 bits in big-endian order.

func (*Writer) PutBe64

func (w *Writer) PutBe64(val uint64)

PutBe64 writes 64 bits in big-endian order.

func (*Writer) PutBit

func (w *Writer) PutBit(val bool)

PutBit writes one bit to output.

func (*Writer) PutByte

func (w *Writer) PutByte(val byte)

PutByte writes one byte.

func (*Writer) PutInt16

func (w *Writer) PutInt16(bits uint, val int16)

PutInt16 writes up to 16 signed bits in big-endian order.

func (*Writer) PutInt32

func (w *Writer) PutInt32(bits uint, val int32)

PutInt32 writes up to 32 signed bits in big-endian order.

func (*Writer) PutInt64

func (w *Writer) PutInt64(bits uint, val int64)

PutInt64 writes up to 64 signed bits in big-endian order.

func (*Writer) PutInt8

func (w *Writer) PutInt8(bits uint, val int8)

PutInt8 writes up to 8 signed bits.

func (*Writer) PutLe16

func (w *Writer) PutLe16(val uint16)

PutLe16 writes 16 bits in little-endian order.

func (*Writer) PutLe32

func (w *Writer) PutLe32(val uint32)

PutLe32 writes 32 bits in little-endian order.

func (*Writer) PutLe64

func (w *Writer) PutLe64(val uint64)

PutLe64 writes 64 bits in little-endian order.

func (*Writer) PutUint16

func (w *Writer) PutUint16(bits uint, val uint16)

PutUint16 writes up to 16 bits in big-endian order.

func (*Writer) PutUint32

func (w *Writer) PutUint32(bits uint, val uint32)

PutUint32 writes up to 32 bits in big-endian order.

func (*Writer) PutUint64

func (w *Writer) PutUint64(bits uint, val uint64)

PutUint64 writes up to 64 bits in big-endian order.

func (*Writer) PutUint8

func (w *Writer) PutUint8(bits uint, val byte)

PutUint8 writes up to 8 bits.

func (*Writer) Reset

func (w *Writer) Reset()

Reset resets the writer to its initial position.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write writes a whole slice p at once. Returns an error if the writer is not byte-aligned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL