bin

package
v0.0.0-...-1de2f69 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2020 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package bin provides support for encoding and decoding binary data.

Example
var b bytes.Buffer
e := NewEncoder(&b)
e.Uint64(42)
data := b.Bytes()
r := NewDecoder(data)
fmt.Println(r.Uint64())
Output:

42

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

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

Decoder streams binary data from a byte buffer.

Example
r := NewDecoder([]byte{0x34, 0x1c, 0, 0})
fmt.Printf("%#x", r.Uint32())
Output:

0x1c34

func NewDecoder

func NewDecoder(b []byte) *Decoder

NewDecoder creates a decoder that parses data from buffer b.

Retains b, which the caller should not use afterward.

func (*Decoder) Array

func (r *Decoder) Array() []byte

Array decodes an array prefixed with a varint length.

func (*Decoder) Array16

func (r *Decoder) Array16() []byte

Array16 decodes an array prefixed with a 16-byte length.

func (*Decoder) Bytes

func (r *Decoder) Bytes(n int) []byte

Bytes is a primitive decoder that reads a fixed number of bytes.

func (Decoder) RemainingBytes

func (r Decoder) RemainingBytes() int

RemainingBytes gives the number of bytes remaining in the buffer.

func (*Decoder) Uint16

func (r *Decoder) Uint16() uint16

Uint16 decodes a uint16 (in little endian format).

func (*Decoder) Uint32

func (r *Decoder) Uint32() uint32

Uint32 decodes a uint32 (in little endian format).

func (*Decoder) Uint64

func (r *Decoder) Uint64() uint64

Uint64 decodes a uint64 (in little endian format).

func (*Decoder) Uint8

func (r *Decoder) Uint8() uint8

Uint8 decodes a uint8

func (*Decoder) VarInt

func (r *Decoder) VarInt() uint64

VarInt parses a varint

See the protocol-buffer documentation for the encoding format: https://developers.google.com/protocol-buffers/docs/encoding#varints.

type Encoder

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

Encoder encodes values to an output stream.

Example
var b bytes.Buffer
e := NewEncoder(&b)
e.Uint32(0x1c34)
fmt.Println(b.Bytes())
Output:

[52 28 0 0]

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder creates an encoder that writes data to w.

func (*Encoder) Array

func (r *Encoder) Array(data []byte)

Array encodes an array prefixed with a varint length.

func (*Encoder) Array16

func (w *Encoder) Array16(b []byte)

Array16 encodes an array prefixed with a 16-byte length.

func (*Encoder) Bytes

func (w *Encoder) Bytes(b []byte)

Bytes is a primitive encoder that copies bytes.

func (Encoder) BytesWritten

func (w Encoder) BytesWritten() int

BytesWritten returns the number of bytes written to the encoder since this encoder was created.

func (*Encoder) Uint16

func (w *Encoder) Uint16(v uint16)

Uint16 encodes a uint16 (in little endian format).

func (*Encoder) Uint32

func (w *Encoder) Uint32(v uint32)

Uint32 encodes a uint32 (in little endian format).

func (*Encoder) Uint64

func (w *Encoder) Uint64(v uint64)

Uint64 encodes a uint64 (in little endian format).

func (*Encoder) Uint8

func (w *Encoder) Uint8(b uint8)

Uint8 encodes a uint8

func (*Encoder) VarInt

func (r *Encoder) VarInt(u uint64)

VarInt encodes a 64-bit int as a varint

Jump to

Keyboard shortcuts

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