bitio

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2019 License: MIT Imports: 4 Imported by: 0

README

go-bitio

TravisCI GolangCI CodeCov GoDoc

Package bitio implements a simple bitwise reader and writer.

The client can use BitReader.ReadBit() to read a bit from an io.ByteReader, and also use BitWriter.WriteBit() to write a bit to an io.ByteWriter. After write with BitWriter.WriteBit(), you must call BitWriter.Flush() or BitWriter.FlushWithOnes().

Examples

BitReader
r := NewBitReader(bytes.NewBuffer([]byte{0b10110100}))
bit, err := r.ReadBit()	// 1, nil
bit, err := r.ReadBit()	// 0, nil
bit, err := r.ReadBit()	// 1, nil
bit, err := r.ReadBit()	// 1, nil
bit, err := r.ReadBit()	// 0, nil
bit, err := r.ReadBit()	// 1, nil
bit, err := r.ReadBit()	// 0, nil
bit, err := r.ReadBit()	// 0, nil
bit, err := r.ReadBit()	// 0, io.EOF
BitWriter
buf := new(bytes.Buffer)
w := NewBitWriter(buf)
defer w.Flush()

err := w.WriteBit(1)
err := w.WriteBit(0)
err := w.WriteBit(1)
err := w.WriteBit(1)
err := w.WriteBit(0)
err := w.WriteBit(1)

byte, err := buf.ReadByte()	// 0b10110100, nil

Documentation

Overview

Package bitio implements a simple bitwise reader and writer.

The client can use BitReader.ReadBit() to read a bit from an io.ByteReader, and also use BitWriter.WriteBit() to write a bit to an io.ByteWriter. After write with BitWriter.WriteBit(), you must call BitWriter.Flush() or BitWriter.FlushWithOnes().

BitReader examples:

r := NewBitReader(bytes.NewBuffer([]byte{0b10110100}))
bit, err := r.ReadBit()	// 1, nil
bit, err := r.ReadBit()	// 0, nil
bit, err := r.ReadBit()	// 1, nil
bit, err := r.ReadBit()	// 1, nil
bit, err := r.ReadBit()	// 0, nil
bit, err := r.ReadBit()	// 1, nil
bit, err := r.ReadBit()	// 0, nil
bit, err := r.ReadBit()	// 0, nil
bit, err := r.ReadBit()	// 0, io.EOF

BitWriter examples:

buf := new(bytes.Buffer)
w := NewBitWriter(buf)
defer w.Flush()

err := w.WriteBit(1)
err := w.WriteBit(0)
err := w.WriteBit(1)
err := w.WriteBit(1)
err := w.WriteBit(0)
err := w.WriteBit(1)

byte, err := buf.ReadByte()	// 0b10110100, nil

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BitReader

type BitReader struct {
	// contains filtered or unexported fields
}

BitReader implements a bitwise reader.

func NewBitReader

func NewBitReader(r io.ByteReader) *BitReader

NewBitReader returns a new BitReader. The reader changes the state of br internally.

func (*BitReader) ReadBit

func (br *BitReader) ReadBit() (bit int, err error)

ReadBit reads the next bit and returns it. At the EOF, err will be io.EOF

func (*BitReader) ReadBits added in v0.2.0

func (br *BitReader) ReadBits(n int) (bits []byte, l int, err error)

ReadBits reads next n bits and returns them. It returns the number of bits successfully read. When didn't read any bit, bits will be nil.

type BitWriter

type BitWriter struct {
	// contains filtered or unexported fields
}

BitWriter implements a bitwise writer. The client should call the Flush or FlushWithOnes method when all data have been written.

func NewBitWriter

func NewBitWriter(w io.ByteWriter) *BitWriter

NewBitWriter returns a new BifWriter. The writer changes the state of w internally.

func (*BitWriter) Flush

func (bw *BitWriter) Flush() error

Flush writes down an internal buffer with zeros. The client should call this method when the all data have been written.

func (*BitWriter) FlushWithOnes

func (bw *BitWriter) FlushWithOnes() error

FlushWithOnes writes down an internal buffer with ones. The client should call this method when the all data have been written.

func (*BitWriter) WriteBit

func (bw *BitWriter) WriteBit(bit int) error

WriteBit writes bit. The err will be not nil when io.ByteWriter returns non-nil error.

func (*BitWriter) WriteBits added in v0.2.0

func (bw *BitWriter) WriteBits(bits []byte, n int) (l int, err error)

WriteBits writes first n bits of bits. It returns the number of bits successfully wrote.

type NotBitError

type NotBitError struct {
	// contains filtered or unexported fields
}

NotBitError will be used when calling WriteBit with an incorrect argument.

func (*NotBitError) Error

func (e *NotBitError) Error() string

Error returns a NotBitError message.

Jump to

Keyboard shortcuts

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