...

Package bitwise

Overview ▾

Package bitwise provides the utils related to reading and writing bit, byte data in a buffer like manner. It defines the StreamReader interface that allows to read bit, bits, byte, bytes, integers change and get the stream position, align the bits. It also defines the data writer that implements io.Writer, io.ByteWriter and allows to write single bits. The writer WriteBit method might be used in a dual way. By default it writes next bit starting from the LSB - least significant bit. By creating the Writer with NewWriterMSB function the writer would write all bits starting from the MSB - most significant bit.

Index ▾

type BinaryWriter
type BitWriter
type BufferedWriter
    func BufferedMSB() *BufferedWriter
    func (b *BufferedWriter) Data() []byte
    func (b *BufferedWriter) FinishByte()
    func (b *BufferedWriter) Len() int
    func (b *BufferedWriter) Reset()
    func (b *BufferedWriter) ResetBitIndex()
    func (b *BufferedWriter) SkipBits(skip int) error
    func (b *BufferedWriter) Write(d []byte) (int, error)
    func (b *BufferedWriter) WriteBit(bit int) error
    func (b *BufferedWriter) WriteBits(bits uint64, number int) (n int, err error)
    func (b *BufferedWriter) WriteByte(bt byte) error
type Reader
    func NewReader(data []byte) *Reader
    func (r *Reader) AbsoluteLength() uint64
    func (r *Reader) AbsolutePosition() int64
    func (r *Reader) Align() (skipped byte)
    func (r *Reader) BitPosition() int
    func (r *Reader) ConsumeRemainingBits() (uint64, error)
    func (r *Reader) Length() uint64
    func (r *Reader) Mark()
    func (r *Reader) NewPartialReader(offset, length int, relative bool) (*Reader, error)
    func (r *Reader) Read(p []byte) (n int, err error)
    func (r *Reader) ReadBit() (bit int, err error)
    func (r *Reader) ReadBits(n byte) (u uint64, err error)
    func (r *Reader) ReadBool() (bool, error)
    func (r *Reader) ReadByte() (byte, error)
    func (r *Reader) ReadUint32() (uint32, error)
    func (r *Reader) RelativePosition() int64
    func (r *Reader) Reset()
    func (r *Reader) Seek(offset int64, whence int) (int64, error)
type StreamReader
type Writer
    func NewWriter(data []byte) *Writer
    func NewWriterMSB(data []byte) *Writer
    func (w *Writer) Data() []byte
    func (w *Writer) FinishByte()
    func (w *Writer) ResetBit()
    func (w *Writer) SkipBits(skip int) error
    func (w *Writer) UseMSB() bool
    func (w *Writer) Write(p []byte) (int, error)
    func (w *Writer) WriteBit(bit int) error
    func (w *Writer) WriteBits(bits uint64, number int) (n int, err error)
    func (w *Writer) WriteByte(c byte) error

Package files

buffer.go doc.go interface.go reader.go writer.go

type BinaryWriter

BinaryWriter is the interface that implements writer.BitWriter, io.Writer and io.ByteWriter.

type BinaryWriter interface {
    BitWriter
    io.Writer
    io.ByteWriter
    Data() []byte
}

type BitWriter

BitWriter is the interface that allows to write single bits.

type BitWriter interface {
    // WriteBit writes the 'bit' - {0,1} value to the writer.
    WriteBit(bit int) error
    // WriteBits writes 'number' of 'bits'.
    WriteBits(bits uint64, number int) (n int, err error)
    // FinishByte sets the bitIndex to the end of given byte. This resets the bitIndex to 0
    // and the byte index to the next byte.
    FinishByte()
    // SkipBits skips the 'skip' number of bits in the writer - changes the index position of the bit and byte.
    // The value -1 sets the bitIndex to the beginning of the byte.
    SkipBits(skip int) error
}

type BufferedWriter

BufferedWriter is the Writer implementation that works on expandable data slices.

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

func BufferedMSB

func BufferedMSB() *BufferedWriter

BufferedMSB creates a buffered writer with MSB bit writing method.

func (*BufferedWriter) Data

func (b *BufferedWriter) Data() []byte

Data gets the buffer byte slice data. The buffer is the owner of the byte slice data, thus the data is available until next buffer Reset.

func (*BufferedWriter) FinishByte

func (b *BufferedWriter) FinishByte()

FinishByte finishes current bit written byte and sets the current byte index pointe to the next byte.

func (*BufferedWriter) Len

func (b *BufferedWriter) Len() int

Len returns the number of bytes of the unwritten portion of the buffer.

func (*BufferedWriter) Reset

func (b *BufferedWriter) Reset()

Reset resets the data buffer as well as the bit and byte indexes.

func (*BufferedWriter) ResetBitIndex

func (b *BufferedWriter) ResetBitIndex()

ResetBitIndex resets the current bit index.

func (*BufferedWriter) SkipBits

func (b *BufferedWriter) SkipBits(skip int) error

SkipBits implements BitWriter interface.

func (*BufferedWriter) Write

func (b *BufferedWriter) Write(d []byte) (int, error)

Write implements io.Writer interface.

func (*BufferedWriter) WriteBit

func (b *BufferedWriter) WriteBit(bit int) error

WriteBit implements BitWriter interface.

func (*BufferedWriter) WriteBits

func (b *BufferedWriter) WriteBits(bits uint64, number int) (n int, err error)

WriteBits writes 'bits' values of a specific 'number' into the writer.BufferedWriter.

func (*BufferedWriter) WriteByte

func (b *BufferedWriter) WriteByte(bt byte) error

WriteByte implements io.ByteWriter.

type Reader

Reader is the bit reader implementation. Implements io.Reader, io.ByteReader, io.Seeker interfaces.

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

func NewReader

func NewReader(data []byte) *Reader

NewReader creates a new reader.Reader using the byte slice data as input.

func (*Reader) AbsoluteLength

func (r *Reader) AbsoluteLength() uint64

AbsoluteLength returns the absolute length of the input data used by the reader.

func (*Reader) AbsolutePosition

func (r *Reader) AbsolutePosition() int64

AbsolutePosition gets the absolute position of the reader.

func (*Reader) Align

func (r *Reader) Align() (skipped byte)

Align resets the bits position of the given reader. It returns how many bits left were skipped.

func (*Reader) BitPosition

func (r *Reader) BitPosition() int

BitPosition gets the current bit position.

func (*Reader) ConsumeRemainingBits

func (r *Reader) ConsumeRemainingBits() (uint64, error)

ConsumeRemainingBits consumes the remaining bits from the given reader.

func (*Reader) Length

func (r *Reader) Length() uint64

Length returns the length of the total data used by the reader.

func (*Reader) Mark

func (r *Reader) Mark()

Mark marks a position in the reader to be returned to by a subsequent call to 'Reset'.

func (*Reader) NewPartialReader

func (r *Reader) NewPartialReader(offset, length int, relative bool) (*Reader, error)

NewPartialReader creates a new partial reader out of the input reader.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Implements io.Reader interface.

func (*Reader) ReadBit

func (r *Reader) ReadBit() (bit int, err error)

ReadBit reads the next binary value from the current cache. Equivalent of ReadBool method but returns an integer.

func (*Reader) ReadBits

func (r *Reader) ReadBits(n byte) (u uint64, err error)

ReadBits reads the bits of size 'n' from the reader.

func (*Reader) ReadBool

func (r *Reader) ReadBool() (bool, error)

ReadBool reads the next binary value from the current cache.

func (*Reader) ReadByte

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

ReadByte implements io.ByteReader.

func (*Reader) ReadUint32

func (r *Reader) ReadUint32() (uint32, error)

ReadUint32 reads the unsigned uint32 from the reader.

func (*Reader) RelativePosition

func (r *Reader) RelativePosition() int64

RelativePosition gets the relative position of the reader within the bounds of the reader.

func (*Reader) Reset

func (r *Reader) Reset()

Reset returns the stream pointer to its previous position, including the bit offset, at the time of the most recent unmatched call to mark.

func (*Reader) Seek

func (r *Reader) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface.

type StreamReader

StreamReader is the interface that allows to read bit, bits, byte, bytes, integers change and get the stream position, align the bits.

type StreamReader interface {
    io.Reader
    io.ByteReader
    io.Seeker

    // Align resets the bits position of the given reader.
    // It returns how many bits left were skipped.
    Align() byte
    // BitPosition gets the current bit position.
    BitPosition() int
    // Mark marks a position in the stream to be returned to by a subsequent call to 'Reset'.
    Mark()
    // Length returns the length of the total data used by the reader.
    Length() uint64

    // ReadBit reads the next binary value from the current cache.
    // Equivalent of ReadBool method but returns an integer.
    ReadBit() (int, error)
    // ReadBits reads the bits of size 'n' from the reader.
    ReadBits(n byte) (uint64, error)
    // ReadBool reads the next binary value from the current cache.
    ReadBool() (bool, error)
    // ReadUint32 reads the unsigned uint32 from the reader.
    ReadUint32() (uint32, error)

    // Reset returns the stream pointer to its previous position, including the bit offset,
    // at the time of the most recent unmatched call to mark.
    Reset()
    // AbsolutePosition gets the stream position of the stream reader.
    AbsolutePosition() int64
}

type Writer

Writer is the structure used to write bits, bytes into predefined data. It allows to write the bits in two modes. The first and default writes bytes with the initial bitIndex 0 as the LSB (Least Significant Bit) The second mode writes bits in an opposite manner starting from the MSB (Most Significant Bit). The writer is being created by the methods: 'NewWriter' and 'NewWriterMSB', where the first creates default writer and the second the 'msb' flagged writer. Implements io.Writer, io.ByteWriter interfaces.

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

func NewWriter

func NewWriter(data []byte) *Writer

NewWriter creates new writer for the provided data.

func NewWriterMSB

func NewWriterMSB(data []byte) *Writer

NewWriterMSB creates new writer with the msb flag. While default writer writes single bits into LSB, the msbWriter writes single bits starting from the MSB. Example:

InverseWriter contains following data:
data - 10010100 01001110 00000000
					 	 ^
The default current bit index is pointed by '^'.
Writing new '1' bit to the following data would result as:
data - 10010100 01001110 10000000

func (*Writer) Data

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

Data gets the writer data.

func (*Writer) FinishByte

func (w *Writer) FinishByte()

FinishByte implements BitWriter interface.

func (*Writer) ResetBit

func (w *Writer) ResetBit()

ResetBit resets the bit counter setting it to '0'.

func (*Writer) SkipBits

func (w *Writer) SkipBits(skip int) error

SkipBits implements BitWriter interface.

func (*Writer) UseMSB

func (w *Writer) UseMSB() bool

UseMSB gets the writer flag if it works on the MSB mode.

func (*Writer) Write

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

Write implements io.Writer interface.

func (*Writer) WriteBit

func (w *Writer) WriteBit(bit int) error

WriteBit writes single bit into provided bit writer data.

func (*Writer) WriteBits

func (w *Writer) WriteBits(bits uint64, number int) (n int, err error)

WriteBits writes the 'bits' of the specific 'number' into writer.

func (*Writer) WriteByte

func (w *Writer) WriteByte(c byte) error

WriteByte implements io.ByteWriter interface.