binpacker

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2021 License: MIT Imports: 5 Imported by: 5

README

binpacker Build Status GoDoc Go Report Card

A binary packer and unpacker.

Install

go get github.com/zhuangsirui/binpacker

Examples

Packer

buffer := new(bytes.Buffer)
packer := binpacker.NewPacker(binary.BigEndian, buffer)
packer.PushByte(0x01)
packer.PushBytes([]byte{0x02, 0x03})
packer.PushUint16(math.MaxUint16)
// You can push data like this
buffer := new(bytes.Buffer)
packer := binpacker.NewPacker(binary.BigEndian, buffer)
packer.PushByte(0x01).PushBytes([]byte{0x02, 0x03}).PushUint16(math.MaxUint16)
packer.Error() // Make sure error is nil

Unpacker

Example data

buffer := new(bytes.Buffer)
packer := binpacker.NewPacker(binary.BigEndian, buffer)
unpacker := binpacker.NewUnpacker(binary.BigEndian, buffer)
packer.PushByte(0x01)
packer.PushUint16(math.MaxUint16)
var val1 byte
var val2 uint16
var err error
val1, err = unpacker.ShiftByte()
val2, err = unpacker.ShiftUint16()
var val1 byte
var val2 uint16
var err error
unpacker.FetchByte(&val1).FetchUint16(&val2)
unpacker.Error() // Make sure error is nil

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddUint16Perfix

func AddUint16Perfix(bytes []byte) []byte

AddUint16Perfix add 2 bytes as uint16 prefixes for bytes.

func AddUint32Perfix

func AddUint32Perfix(bytes []byte) []byte

AddUint32Perfix add 4 bytes as uint32 prefixes for bytes.

func AddUint64Perfix

func AddUint64Perfix(bytes []byte) []byte

AddUint64Perfix add 8 bytes as uint64 prefixes for bytes.

Types

type Packer

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

Packer is a binary packer helps you pack data into an io.Writer.

func NewPacker

func NewPacker(endian binary.ByteOrder, writer io.Writer) *Packer

NewPacker returns a *Packer hold an io.Writer. User must provide the byte order explicitly.

func (*Packer) Error

func (p *Packer) Error() error

Error returns an error if any errors exists

func (*Packer) PushByte

func (p *Packer) PushByte(b byte) *Packer

PushByte write a single byte into writer.

func (*Packer) PushBytes

func (p *Packer) PushBytes(bytes []byte) *Packer

PushBytes write a bytes array into writer.

func (*Packer) PushFloat32

func (p *Packer) PushFloat32(i float32) *Packer

PushFloat32 write a float32 into writer.

func (*Packer) PushFloat64

func (p *Packer) PushFloat64(i float64) *Packer

PushFloat64 write a float64 into writer.

func (*Packer) PushInt16

func (p *Packer) PushInt16(i int16) *Packer

PushUint16 write a int16 into writer.

func (*Packer) PushInt32

func (p *Packer) PushInt32(i int32) *Packer

PushInt32 write a int32 into writer.

func (*Packer) PushInt64

func (p *Packer) PushInt64(i int64) *Packer

PushInt64 write a int64 into writer.

func (*Packer) PushString

func (p *Packer) PushString(s string) *Packer

PushString write a string into writer.

func (*Packer) PushUint16

func (p *Packer) PushUint16(i uint16) *Packer

PushUint16 write a uint16 into writer.

func (*Packer) PushUint32

func (p *Packer) PushUint32(i uint32) *Packer

PushUint32 write a uint32 into writer.

func (*Packer) PushUint64

func (p *Packer) PushUint64(i uint64) *Packer

PushUint64 write a uint64 into writer.

func (*Packer) PushUint8

func (p *Packer) PushUint8(i uint8) *Packer

PushUint8 write a uint8 into writer.

type Unpacker

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

Unpacker helps you unpack binary data from an io.Reader.

func NewUnpacker

func NewUnpacker(endian binary.ByteOrder, reader io.Reader) *Unpacker

NewUnpacker returns a *Unpacker which hold an io.Reader. User must provide the byte order explicitly.

func (*Unpacker) BytesWithUint16Perfix

func (u *Unpacker) BytesWithUint16Perfix(bytes *[]byte) *Unpacker

func (*Unpacker) BytesWithUint16Prefix

func (u *Unpacker) BytesWithUint16Prefix(bytes *[]byte) *Unpacker

BytesWithUint16Prefix read 2 bytes as bytes length, then read N bytes and set it to bytes.

func (*Unpacker) BytesWithUint32Perfix

func (u *Unpacker) BytesWithUint32Perfix(bytes *[]byte) *Unpacker

func (*Unpacker) BytesWithUint32Prefix

func (u *Unpacker) BytesWithUint32Prefix(bytes *[]byte) *Unpacker

BytesWithUint32Prefix read 4 bytes as bytes length, then read N bytes and set it to bytes.

func (*Unpacker) BytesWithUint64Perfix

func (u *Unpacker) BytesWithUint64Perfix(bytes *[]byte) *Unpacker

func (*Unpacker) BytesWithUint64Prefix

func (u *Unpacker) BytesWithUint64Prefix(bytes *[]byte) *Unpacker

BytesWithUint64Prefix read 8 bytes as bytes length, then read N bytes and set it to bytes.

func (*Unpacker) Error

func (u *Unpacker) Error() error

Error returns an error if any errors exists

func (*Unpacker) FetchByte

func (u *Unpacker) FetchByte(b *byte) *Unpacker

FetchByte fetch the first byte in io.Reader and set to b.

func (*Unpacker) FetchBytes

func (u *Unpacker) FetchBytes(n uint64, bytes *[]byte) *Unpacker

FetchBytes read n bytes and set to bytes.

func (*Unpacker) FetchFloat32

func (u *Unpacker) FetchFloat32(i *float32) *Unpacker

FetchFloat32 read 4 bytes, convert it to float32 and set it to i.

func (*Unpacker) FetchFloat64

func (u *Unpacker) FetchFloat64(i *float64) *Unpacker

FetchFloat64 read 8 bytes, convert it to float64 and set it to i.

func (*Unpacker) FetchInt16

func (u *Unpacker) FetchInt16(i *int16) *Unpacker

FetchInt16 read 2 bytes, convert it to int16 and set it to i.

func (*Unpacker) FetchInt32

func (u *Unpacker) FetchInt32(i *int32) *Unpacker

FetchInt32 read 4 bytes, convert it to int32 and set it to i.

func (*Unpacker) FetchInt64

func (u *Unpacker) FetchInt64(i *int64) *Unpacker

FetchInt64 read 8 bytes, convert it to int64 and set it to i.

func (*Unpacker) FetchString

func (u *Unpacker) FetchString(n uint64, s *string) *Unpacker

FetchString read n bytes, convert it to string and set t to s.

func (*Unpacker) FetchUint16

func (u *Unpacker) FetchUint16(i *uint16) *Unpacker

FetchUint16 read 2 bytes, convert it to uint16 and set it to i.

func (*Unpacker) FetchUint32

func (u *Unpacker) FetchUint32(i *uint32) *Unpacker

FetchUint32 read 4 bytes, convert it to uint32 and set it to i.

func (*Unpacker) FetchUint64

func (u *Unpacker) FetchUint64(i *uint64) *Unpacker

FetchUint64 read 8 bytes, convert it to uint64 and set it to i.

func (*Unpacker) FetchUint8

func (u *Unpacker) FetchUint8(i *uint8) *Unpacker

FetchUint8 read 1 byte, convert it to uint8 and set it to i.

func (*Unpacker) ShiftByte

func (u *Unpacker) ShiftByte() (byte, error)

ShiftByte fetch the first byte in io.Reader. Returns a byte and an error if exists.

func (*Unpacker) ShiftBytes

func (u *Unpacker) ShiftBytes(_n uint64) ([]byte, error)

ShiftBytes fetch n bytes in io.Reader. Returns a byte array and an error if exists.

func (*Unpacker) ShiftFloat32

func (u *Unpacker) ShiftFloat32() (float32, error)

ShiftFloat32 fetch 4 bytes in io.Reader and convert it to float32.

func (*Unpacker) ShiftFloat64

func (u *Unpacker) ShiftFloat64() (float64, error)

ShiftFloat64 fetch 8 bytes in io.Reader and convert it to float64.

func (*Unpacker) ShiftInt16

func (u *Unpacker) ShiftInt16() (int16, error)

ShiftInt16 fetch 2 bytes in io.Reader and convert it to int16.

func (*Unpacker) ShiftInt32

func (u *Unpacker) ShiftInt32() (int32, error)

ShiftInt32 fetch 4 bytes in io.Reader and convert it to int32.

func (*Unpacker) ShiftInt64

func (u *Unpacker) ShiftInt64() (int64, error)

ShiftInt64 fetch 8 bytes in io.Reader and convert it to int64.

func (*Unpacker) ShiftString

func (u *Unpacker) ShiftString(n uint64) (string, error)

ShiftString fetch n bytes, convert it to string. Returns string and an error.

func (*Unpacker) ShiftUint16

func (u *Unpacker) ShiftUint16() (uint16, error)

ShiftUint16 fetch 2 bytes in io.Reader and convert it to uint16.

func (*Unpacker) ShiftUint32

func (u *Unpacker) ShiftUint32() (uint32, error)

ShiftUint32 fetch 4 bytes in io.Reader and convert it to uint32.

func (*Unpacker) ShiftUint64

func (u *Unpacker) ShiftUint64() (uint64, error)

ShiftUint64 fetch 8 bytes in io.Reader and convert it to uint64.

func (*Unpacker) ShiftUint8

func (u *Unpacker) ShiftUint8() (uint8, error)

ShiftUint8 fetch 1 byte in io.Reader and covert it to uint8

func (*Unpacker) StringWithUint16Perfix

func (u *Unpacker) StringWithUint16Perfix(s *string) *Unpacker

func (*Unpacker) StringWithUint16Prefix

func (u *Unpacker) StringWithUint16Prefix(s *string) *Unpacker

StringWithUint16Prefix read 2 bytes as string length, then read N bytes, convert it to string and set it to s.

func (*Unpacker) StringWithUint32Perfix

func (u *Unpacker) StringWithUint32Perfix(s *string) *Unpacker

func (*Unpacker) StringWithUint32Prefix

func (u *Unpacker) StringWithUint32Prefix(s *string) *Unpacker

StringWithUint32Prefix read 4 bytes as string length, then read N bytes, convert it to string and set it to s.

func (*Unpacker) StringWithUint64Perfix

func (u *Unpacker) StringWithUint64Perfix(s *string) *Unpacker

func (*Unpacker) StringWithUint64Prefix

func (u *Unpacker) StringWithUint64Prefix(s *string) *Unpacker

StringWithUint64Prefix read 8 bytes as string length, then read N bytes, convert it to string and set it to s.

Jump to

Keyboard shortcuts

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