Documentation
¶
Index ¶
- Constants
- Variables
- func AppendVarint(buf []byte, x int) []byte
- func PutVarint(buf []byte, x int) int
- func ReadVarint(r io.ByteReader) (int, error)
- func Varint(buf []byte) (i int, n int)
- type Huffman
- type MsgPacker
- type Packer
- type Unpacker
- func (u *Unpacker) Bytes() []byte
- func (u *Unpacker) NextByte() (b byte, err error)
- func (u *Unpacker) NextBytes(size int) (b []byte, err error)
- func (u *Unpacker) NextInt() (i int, err error)
- func (u *Unpacker) NextString() (s string, err error)
- func (u *Unpacker) Reset(b []byte)
- func (u *Unpacker) Size() int
Constants ¶
const ( HuffmanEOFSymbol = protocol.HuffmanEOFSymbol HuffmanMaxSymbols = protocol.HuffmanMaxSymbols HuffmanMaxNodes = (HuffmanMaxSymbols)*2 + 1 // +1 for additional EOF symbol HuffmanLookupTableBits = 10 HuffmanLookupTableSize = (1 << HuffmanLookupTableBits) HuffmanLookupTableMask = (HuffmanLookupTableSize - 1) )
const ( // StringTerminator is the zero byte that terminates the string StringTerminator byte = 0 // with how many bytes the packer is initialized PackerBufferSize = 1024 * 2 )
const (
// max bytes that can be received for one integer
MaxVarintLen32 = 5
)
Variables ¶
var ( ErrHuffmanCompress = errors.New("compression error") ErrHuffmanDecompress = errors.New("decompression error") )
var ( // ErrNoDataToUnpack is returned if the compressed array does not have sufficient data to unpack ErrNoDataToUnpack = fmt.Errorf("%w: no data", io.EOF) // ErrNotAString if no separator after a string is found, the string cannot be unpacked, as there is no string ErrNotAString = errors.New("could not unpack string: terminator not found") // ErrNotEnoughData is used when the user tries to retrieve more data with NextBytes() than there is available. ErrNotEnoughData = errors.New("trying to read more data than available") )
Functions ¶
func AppendVarint ¶ added in v1.4.0
AppendVarint appends the varint-encoded form of x, as generated by PutVarint, to buf and returns the extended buffer.
func PutVarint ¶ added in v1.4.0
PutVarint encodes an int32 into buf and returns the number of bytes written. If the buffer is too small, PutVarint will panic. Format: ESDDDDDD EDDDDDDD EDD... Extended, Data, Sign E: is next byte part of the current integer S: Sign of integer Data, Integer bits that follow the sign
func ReadVarint ¶ added in v1.4.0
func ReadVarint(r io.ByteReader) (int, error)
ReadVarint can decode a stream of bytes
func Varint ¶ added in v1.4.0
Varint decodes an int from buf and returns that value and the number of bytes read (> 0). If an error occurred, the value is 0 and the number of bytes n is <= 0 with the following meaning:
n == 0: buf too small
n < 0: value larger than 32 bits (overflow)
and -n is the number of bytes read
Types ¶
type Huffman ¶ added in v1.4.0
type Huffman struct {
// contains filtered or unexported fields
}
func NewHuffman ¶ added in v1.4.0
func NewHuffman(frequencyTable [HuffmanMaxSymbols]uint32) *Huffman
NewHuffman expects a frequency table aka index -> symbol You can use the default one that can be found under protocol.FrequencyTable You can put the frequency at index HuffmanMaxSymbols -1 to the value 1. It is the EOF value which will ne overwritten anyway.
type Packer ¶
type Packer struct {
// contains filtered or unexported fields
}
Packer compresses data
type Unpacker ¶
type Unpacker struct {
// contains filtered or unexported fields
}
Unpacker unpacks received messages
func NewUnpacker ¶ added in v1.4.0
NewUnpacker constructs a new Unpacker
func (*Unpacker) Bytes ¶ added in v1.4.0
Bytes returns the not yet used bytes. This operation consumes the buffer leaving it empty
func (*Unpacker) NextString ¶
NextString unpacks the next string from the message