bitstream

package
v0.0.0-...-c8519f9 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 4 Imported by: 0

README

Bitstream

The bitstream package provides a manipulating sequences of bits, offering a flexible way to handle bit-level data within bytes. This package is ideal for appliacations requiring direct bit manipulation such as compression algorithms, cryptography, and network protocols.

Features

  • Create Bitstreams from byte slices.
  • Read and Write operations for individual bits.
  • Append bits
  • ** Stream bits** to a writer and reader.

Usage

Create a Bitstream
func main() {
    data := []byte{0x0F, 0xF0}
    bs := bitstream.NewBitstream(data)

    // Print the length of bitstream
    fmt.Println("Length of Bitstream:", bs.Len())
}
Writing to a Bitstream
bs := bitstream.NewBitstream([]byte{})
writer := bitstream.NewBitStreamWriter(bs)

// Write a byte
writer.Write([]byte{0xAA})
writer.Flush()

// Write a single bit
writer.WriteBit(1)
writer.Flush()
Reading from a Bitstream
reader := bitstream.NewBitStreamReader(bs)

// Read a single byte
byteVal, err := reader.ReadByte()
if err != nil {
    log.Fatal(err)
}

// Read a single bit
bitVal, err := reader.ReadBit()
if err != nil {
    log.Fatal(err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOutOfRange      = errors.New("index out of range")
	ErrInvalidBitValue = errors.New("invalid bit value")
)
View Source
var ErrNotEnoughBits = errors.New("not enough bits")

Functions

This section is empty.

Types

type BitStream

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

BitStream represents a sequence of bits storedd in a byte slice.

func FromFile

func FromFile(filename string) (*BitStream, error)

FromFile reads a file containing a list of numbers and returns a Bitstream.

func FromFileWithLimit

func FromFileWithLimit(filename string, limit int) (*BitStream, error)

FromFileWithLimit reads a file containing a list of numbers and returns a Bitstream. It only reads the first 'limit' lines from the file.

func NewBitStream

func NewBitStream(data []byte) *BitStream

NewBitStream creates a new Bitstream from the provided byte slice.

func (*BitStream) Append

func (bs *BitStream) Append(bit byte) error

Append appends a bit to the end of the bitstream. It returns an error if the provided bit value is not 0 or 1.

func (*BitStream) Bit

func (bs *BitStream) Bit(index int) (byte, error)

Bit returns the bit value at the sepcified index. It returns an error if the index is out of range.

func (*BitStream) Bytes

func (bs *BitStream) Bytes() []byte

Bytes returns the underlying byte slice of the bitstream.

func (*BitStream) Len

func (bs *BitStream) Len() int

Len returns the length of the bitsream in bits.

func (*BitStream) SetBit

func (bs *BitStream) SetBit(index int, bit byte) error

SetBit sets the bit value at the specified index. It returns an error if the index is out of range.

type BitStreamReader

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

BitStreamReader provides a way to read bits from a Bitstream.

func NewBitStreamReader

func NewBitStreamReader(bs *BitStream) *BitStreamReader

NewBitStreamReader creates a new BitStreamReader from the provided Bitstream.

func (*BitStreamReader) Read

func (r *BitStreamReader) Read(n int) ([]byte, error)

Read reads the specified number of bits from the Bitstream and returns them as a byte slice.

It returns an error if there are not enough bits to read.

func (*BitStreamReader) ReadBit

func (r *BitStreamReader) ReadBit() (byte, error)

ReadBit reads a single bit from the BitStream and returns it as a byte. It returns an error if there are not enough bits to read.

func (*BitStreamReader) ReadByte

func (r *BitStreamReader) ReadByte() (byte, error)

ReadByte reads a 8 bits from the Bitstream and returns them as a byte. It returns an error if there are not enough bits to read.

type BitStreamWriter

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

BitStreamWriter provides a way to write bits to a Bitstream.

func NewBitStreamWriter

func NewBitStreamWriter(bs *BitStream) *BitStreamWriter

NewBitStreamWriter creates a new BitStreamWriter from the given Bitstream.

func (*BitStreamWriter) Flush

func (w *BitStreamWriter) Flush() error

Flush writes the buffred bits to the Bitstream. It clears the buffer after writing.

func (*BitStreamWriter) Write

func (w *BitStreamWriter) Write(bits []byte) error

Write writes the provided byte slice to the BitStreamWriter. Each byte is written as 8 individual bits.

func (*BitStreamWriter) WriteBit

func (w *BitStreamWriter) WriteBit(bit byte) error

WriteBit writes a single bit to the BitStreamWriter. It returns an error if the given bit value is not 0 or 1.

Jump to

Keyboard shortcuts

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