HomeUniDoc
...

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) 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) 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) Reset()
    func (r *Reader) Seek(offset int64, whence int) (int64, error)
    func (r *Reader) StreamPosition() int64
type StreamReader
type SubstreamReader
    func NewSubstreamReader(r StreamReader, offset, length uint64) (*SubstreamReader, error)
    func (s *SubstreamReader) Align() (skipped byte)
    func (s *SubstreamReader) BitPosition() int
    func (s *SubstreamReader) Length() uint64
    func (s *SubstreamReader) Mark()
    func (s *SubstreamReader) Offset() uint64
    func (s *SubstreamReader) Read(b []byte) (n int, err error)
    func (s *SubstreamReader) ReadBit() (bit int, err error)
    func (s *SubstreamReader) ReadBits(n byte) (u uint64, err error)
    func (s *SubstreamReader) ReadBool() (bool, error)
    func (s *SubstreamReader) ReadByte() (byte, error)
    func (s *SubstreamReader) ReadUint32() (uint32, error)
    func (s *SubstreamReader) Reset()
    func (s *SubstreamReader) Seek(offset int64, whence int) (int64, error)
    func (s *SubstreamReader) StreamPosition() int64
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 substream.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) Align

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

Align implements StreamReader interface.

func (*Reader) BitPosition

func (r *Reader) BitPosition() int

BitPosition implements StreamReader inteface.

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 implements StreamReader interface.

func (*Reader) Mark

func (r *Reader) Mark()

Mark implements StreamReader interface.

func (*Reader) Read

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

Read implements io.Reader interface.

func (*Reader) ReadBit

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

ReadBit implements StreamReader interface.

func (*Reader) ReadBits

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

ReadBits implements StreamReader interface.

func (*Reader) ReadBool

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

ReadBool implements StreamReader interface.

func (*Reader) ReadByte

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

ReadByte implements io.ByteReader.

func (*Reader) ReadUint32

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

ReadUint32 implements StreamReader interface.

func (*Reader) Reset

func (r *Reader) Reset()

Reset implements StreamReader interface.

func (*Reader) Seek

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

Seek implements the io.Seeker interface.

func (*Reader) StreamPosition

func (r *Reader) StreamPosition() int64

StreamPosition implements StreamReader 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()
    // StreamPosition gets the stream position of the stream reader.
    StreamPosition() int64
}

type SubstreamReader

SubstreamReader is the wrapper over the Reader's parts that is allowed only to operate on the selected data space.

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

func NewSubstreamReader

func NewSubstreamReader(r StreamReader, offset, length uint64) (*SubstreamReader, error)

NewSubstreamReader creates new SubStreamReader for the provided wrapped StreamReader 'r' with defined 'offset' and 'length'.

func (*SubstreamReader) Align

func (s *SubstreamReader) Align() (skipped byte)

Align implements StreamReader interface.

func (*SubstreamReader) BitPosition

func (s *SubstreamReader) BitPosition() int

BitPosition implements StreamReader interface.

func (*SubstreamReader) Length

func (s *SubstreamReader) Length() uint64

Length implements StreamReader interface.

func (*SubstreamReader) Mark

func (s *SubstreamReader) Mark()

Mark implements StreamReader interface.

func (*SubstreamReader) Offset

func (s *SubstreamReader) Offset() uint64

Offset returns current SubstreamReader offset.

func (*SubstreamReader) Read

func (s *SubstreamReader) Read(b []byte) (n int, err error)

Read implements io.Reader interface.

func (*SubstreamReader) ReadBit

func (s *SubstreamReader) ReadBit() (bit int, err error)

ReadBit implements StreamReader interface.

func (*SubstreamReader) ReadBits

func (s *SubstreamReader) ReadBits(n byte) (u uint64, err error)

ReadBits implements StreamReader interface.

func (*SubstreamReader) ReadBool

func (s *SubstreamReader) ReadBool() (bool, error)

ReadBool implements StreamReader interface.

func (*SubstreamReader) ReadByte

func (s *SubstreamReader) ReadByte() (byte, error)

ReadByte implements io.ByteReader.

func (*SubstreamReader) ReadUint32

func (s *SubstreamReader) ReadUint32() (uint32, error)

ReadUint32 implements Streamreader interface.

func (*SubstreamReader) Reset

func (s *SubstreamReader) Reset()

Reset implements StreamReader interface.

func (*SubstreamReader) Seek

func (s *SubstreamReader) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface.

func (*SubstreamReader) StreamPosition

func (s *SubstreamReader) StreamPosition() int64

StreamPosition implements StreamReader interface.

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.