bitio: github.com/icza/bitio Index | Files

package bitio

import "github.com/icza/bitio"

Package bitio provides a highly optimized bit-level Reader and Writer.

You can use Reader.ReadBits() to read arbitrary number of bits from an io.Reader and return it as an uint64, and Writer.WriteBits() to write arbitrary number of bits of an uint64 value to an io.Writer.

Both Reader and Writer also provide highly optimized methods for reading / writing 1 bit of information in the form of a bool value: Reader.ReadBool() and Writer.WriteBool(). These make this package ideal for compression algorithms that use Huffman coding for example, where decision whether to step left or right in the Huffman tree is the most frequent operation.

Reader and Writer give a bit-level view of the underlying io.Reader and io.Writer, but they also provide a byte-level view (io.Reader and io.Writer) at the same time. This means you can also use the Reader.Read() and Writer.Write() methods to read and write slices of bytes. These will give you best performance ifthe underlying io.Reader and io.Writer are aligned to a byte boundary (else all the individual bytes are assembled from / spread to multiple bytes). You can ensure byte boundary by calling the Align() method of Reader and Writer.

Bit order

The more general highest-bits-first order is used. So for example if the input provides the bytes 0x8f and 0x55:

HEXA    8    f     5    5
BINARY  1100 1111  0101 0101
        aaaa bbbc  ccdd dddd

Then ReadBits will return the following values:

r := NewReader(bytes.NewBuffer([]byte{0x8f, 0x55}))
a, err := r.ReadBits(4) //   1100 = 0x08
b, err := r.ReadBits(3) //    111 = 0x07
c, err := r.ReadBits(3) //    101 = 0x05
d, err := r.ReadBits(6) // 010101 = 0x15

Writing the above values would result in the same sequence of bytes:

b := &bytes.Buffer{}
w := NewWriter(b)
err := w.WriteBits(0x08, 4)
err = w.WriteBits(0x07, 3)
err = w.WriteBits(0x05, 3)
err = w.WriteBits(0x15, 6)
err = w.Close()
// b will hold the bytes: 0x8f and 0x55

Index

Package Files

doc.go reader.go writer.go

type Reader Uses

type Reader interface {
    // Reader is an io.Reader
    io.Reader

    // Reader is also an io.ByteReader.
    // ReadByte reads the next 8 bits and returns them as a byte.
    io.ByteReader

    // ReadBits reads n bits and returns them as the lowest n bits of u.
    ReadBits(n byte) (u uint64, err error)

    // ReadBool reads the next bit, and returns true if it is 1.
    ReadBool() (b bool, err error)

    // Align aligns the bit stream to a byte boundary,
    // so next read will read/use data from the next byte.
    // Returns the number of unread / skipped bits.
    Align() (skipped byte)
}

Reader is the bit reader interface.

func NewReader Uses

func NewReader(in io.Reader) Reader

NewReader returns a new Reader using the specified io.Reader as the input (source).

type Writer Uses

type Writer interface {
    // Writer is an io.Writer and io.Closer.
    // Close closes the bit writer, writes out cached bits.
    // It does not close the underlying io.Writer.
    io.WriteCloser

    // Writer is also an io.ByteWriter.
    // WriteByte writes 8 bits.
    io.ByteWriter

    // WriteBits writes out the n lowest bits of r.
    // r cannot have bits set at positions higher than n-1 (zero indexed).
    WriteBits(r uint64, n byte) (err error)

    // WriteBool writes one bit: 1 if param is true, 0 otherwise.
    WriteBool(b bool) (err error)

    // Align aligns the bit stream to a byte boundary,
    // so next write will start/go into a new byte.
    // If there are cached bits, they are first written to the output.
    // Returns the number of skipped (unset but still written) bits.
    Align() (skipped byte, err error)
}

Writer is the bit writer interface. Must be closed in order to flush cached data. If you can't or don't want to close it, flushing data can also be forced by calling Align().

func NewWriter Uses

func NewWriter(out io.Writer) Writer

NewWriter returns a new Writer using the specified io.Writer as the output.

Package bitio imports 2 packages (graph) and is imported by 9 packages. Updated 2018-02-27. Refresh now. Tools for package owners.