⚡️ Fast bitstream reader/writer ⚡️
Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.travis.yml
LICENSE
README.md
benchmark_test.go
buffer.go
buffer_test.go
error.go
reader.go
reader_test.go
writer.go
writer_test.go

README.md

bitstream

Build Status GoDoc Go Report Card Coverage Status

bitstream is a Go library to read and write bit-length values on a stream of bytes. It has been designed and optimized to be fast and this is the main goal of this library.

Installation

To install bitstream, simply run:

$ go get -v -t github.com/vteromero/bitstream

Once the get completes, you should find the bitstream executable inside $GOPATH/bin.

Examples

Here is an example of bitstream.Reader:

package main

import (
	"fmt"

	"github.com/vteromero/bitstream"
)

func showValues(v uint64, m int, err error) {
	fmt.Printf("value: %b, size: %d, error: %v\n", v, m, err)
}

func main() {
	data := []byte{0x55, 0x55} // in binary: 01010101 01010101

	// create a new Reader
	r := bitstream.NewReader(data)

	// read 16 bits
	v, m, err := r.Read(16)
	showValues(v, m, err)

	// reset the Reader to start over
	r.Reset()

	// read bit to bit until EOF
	for {
		v, m, err := r.Read(1)
		if err != nil {
			fmt.Println(err)
			break
		}
		showValues(v, m, err)
	}
}

And the following shows how to use bitstream.Writer:

package main

import (
	"fmt"

	"github.com/vteromero/bitstream"
)

func main() {
	// create an empty byte slice
	data := make([]byte, 10)

	// create a new Writer
	w := bitstream.NewWriter(data)

	// some writings
	w.Write(0x8877665544332211, 64)
	w.Write(0x2, 2)
	w.Write(0x2, 2)
	w.Write(0x2, 2)
	w.Write(0x2, 2)
	w.Write(0xb, 4)
	w.Write(0xb, 4)

	// always close the writer
	w.Close()

	fmt.Printf("% x\n", data)
}