rivbin

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2020 License: MIT Imports: 9 Imported by: 12

Documentation

Overview

Package rivbin converts arbitrary objects into byte slices, and vis versa. It also contains helper functions for reading and writing specific/specialised objects.

Index

Constants

View Source
const (
	// MaxSliceSize refers to the maximum size slice could have. Limited
	// to 5 MB.
	MaxSliceSize = 5e6 // 5 MB
)

Variables

View Source
var (
	// ErrSliceTooLarge is an error when encoded slice is too large.
	ErrSliceTooLarge = errors.New("encoded slice is too large")
)

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the encoding of v. For encoding details, see the package docstring.

func MarshalAll

func MarshalAll(vs ...interface{}) ([]byte, error)

MarshalAll encodes all of its inputs and returns their concatenation.

func MarshalBool

func MarshalBool(w io.Writer, b bool) error

MarshalBool writes a bool value as a single byte value.

func MarshalTinySlice

func MarshalTinySlice(w io.Writer, v interface{}) error

MarshalTinySlice allows the marshaling of tiny slices, meaning slices with a length of maximum 255 (elements). All elements are encoded in the order given, using the encoder of this package.

Note that this specialised marshal function should only be used when also in complete control of the unmarshal process, otherwise it is better to use the regular dynamlic slice encoding, using the normal encoding process of this package.

Supported types: `[]byte`, `[]x` (where x can be of any type), `string`

func MarshalUint16

func MarshalUint16(w io.Writer, x uint16) error

MarshalUint16 marshals an uint16 value as a 2-byte little-endian value.

func MarshalUint24

func MarshalUint24(w io.Writer, x uint32) error

MarshalUint24 marshals an uint24 value as a 3-byte little-endian value.

func MarshalUint32

func MarshalUint32(w io.Writer, x uint32) error

MarshalUint32 marshals an uint32 value as a 4-byte little-endian value.

func MarshalUint64

func MarshalUint64(w io.Writer, x uint64) error

MarshalUint64 marshals an uint64 value as an 8-byte little-endian value.

func MarshalUint8

func MarshalUint8(w io.Writer, x uint8) error

MarshalUint8 writes an uint8 value as a single byte value.

func ReadDataSlice

func ReadDataSlice(r io.Reader, maxLen int) ([]byte, error)

ReadDataSlice reads a byte slice length of maximum 4 bytes followed by the number of bytes specified in the (length) prefix. The operation is aborted if the prefix exceeds a specified maximum length.

func ReadObject

func ReadObject(r io.Reader, obj interface{}, maxLen int) error

ReadObject reads and decodes a length-prefixed and marshalled object.

func Unmarshal

func Unmarshal(b []byte, v interface{}) error

Unmarshal decodes the encoded value b and stores it in v, which must be a pointer. The decoding rules are the inverse of those specified in the package docstring for marshaling.

func UnmarshalAll

func UnmarshalAll(b []byte, vs ...interface{}) error

UnmarshalAll decodes the encoded values in b and stores them in vs, which must be pointers.

func UnmarshalBool

func UnmarshalBool(r io.Reader) (bool, error)

UnmarshalBool reads a bool value as a single byte value.

func UnmarshalTinySlice

func UnmarshalTinySlice(r io.Reader, v interface{}) error

UnmarshalTinySlice allows the unmarshaling of tiny slices, meaning slices with a length of maximum 255 (elements). All elements are decoded in the order given, using the decoder of this package. Note that this can only decode values that were encoded using an equivalent algorithm as implemented in `MarshalTinySlice`.

Supported types: `*[]byte`, `*[]x` (where x can be of any type), `*string`

func UnmarshalUint16

func UnmarshalUint16(r io.Reader) (uint16, error)

UnmarshalUint16 unmarshals a 2-byte little-endian value as an uint16 value.

func UnmarshalUint24

func UnmarshalUint24(r io.Reader) (uint32, error)

UnmarshalUint24 unmarshals a 3-byte little-endian value as an uint32 value.

func UnmarshalUint32

func UnmarshalUint32(r io.Reader) (uint32, error)

UnmarshalUint32 unmarshals a 4-byte little-endian value as an uint32 value.

func UnmarshalUint64

func UnmarshalUint64(r io.Reader) (uint64, error)

UnmarshalUint64 unmarshals an 8-byte little-endian value as an uint64 value.

func UnmarshalUint8

func UnmarshalUint8(r io.Reader) (uint8, error)

UnmarshalUint8 reads a single byte and casts its to an uint8 value.

func WriteDataSlice

func WriteDataSlice(w io.Writer, data []byte) error

WriteDataSlice writes a length-prefixed byte slice to w.

func WriteObject

func WriteObject(w io.Writer, v interface{}) error

WriteObject writes a length-prefixed object to w.

Types

type Decoder

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

A Decoder reads and decodes values from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

Decode reads the next encoded value from its input stream and stores it in v, which must be a pointer. The decoding rules are the inverse of those specified in the package docstring.

func (*Decoder) DecodeAll

func (d *Decoder) DecodeAll(vs ...interface{}) error

DecodeAll decodes a variable number of arguments.

type Encoder

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

Encoder writes objects to an output stream.

A modified and improved Binary Encoder based upon the siabin Encoder, found in the <github.com/threefoldtech/rivine/pkg/encoding/siabin> package.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

Encode writes the encoding of v to the stream. For encoding details, see the package docstring.

func (*Encoder) EncodeAll

func (e *Encoder) EncodeAll(vs ...interface{}) error

EncodeAll encodes a variable number of arguments.

type RivineMarshaler

type RivineMarshaler interface {
	MarshalRivine(io.Writer) error
}

A RivineMarshaler can encode and write itself to a stream.

type RivineUnmarshaler

type RivineUnmarshaler interface {
	UnmarshalRivine(io.Reader) error
}

A RivineUnmarshaler can read and decode itself from a stream.

Jump to

Keyboard shortcuts

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