binser

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2022 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package binser provides types to read and write binary archives from the C++ Boost Serialization library.

Writing values to an output binary archive can be done like so:

enc := binser.NewEncoder(w)
err := enc.Encode("hello")

And reading values from an input binary archive:

dec := binser.NewDecoder(r)
str := ""
err := dec.Decode(&str)

For more informations, look at the examples for Encoder, Decoder and read/write Buffer.

Index

Examples

Constants

View Source
const (
	ArchHW = Arch(ptrSize) // ArchHW enables writing/reading "native-bits" archives (ie: using the architecture's size).
	Arch32 = Arch(32)      // Arch32 enables writing/reading 32-bits archives
	Arch64 = Arch(64)      // Arch64 enables writing/reading 64-bits archives
)

Variables

View Source
var (
	ErrNotBoost         = errors.New("binser: not a Boost binary archive")
	ErrInvalidHeader    = errors.New("binser: invalid Boost binary archive header")
	ErrInvalidTypeDescr = errors.New("binser: invalid Boost binary archive type descriptor")
	ErrTypeNotSupported = errors.New("binser: type not supported")
	ErrInvalidArrayLen  = errors.New("binser: invalid array type")
)

Functions

This section is empty.

Types

type Arch added in v0.7.0

type Arch byte

Arch describes the size of on-disk pointers.

func (Arch) Header added in v0.7.0

func (a Arch) Header() Header

func (Arch) NewEncoder added in v0.7.0

func (a Arch) NewEncoder(w io.Writer) *Encoder

NewEncoder creates a new encoder.

type Decoder

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

A Decoder reads and decodes values from a Boost binary serialization stream.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/go-boostio/boostio/binser"
)

func main() {
	f, err := os.Open("testdata/data64.bin")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	dec := binser.NewDecoder(f)

	var v1 bool
	err = dec.Decode(&v1)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("bool: %v\n", v1)

	err = dec.Decode(&v1)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("bool: %v\n", v1)

	var i8 int8
	err = dec.Decode(&i8)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("int8: %#x\n", i8)

}
Output:

bool: false
bool: true
int8: 0x11

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

The decoder checks the stream has a correct Boost binary header.

func (*Decoder) Decode

func (dec *Decoder) Decode(ptr interface{}) error

Decode reads the next value from its input and stores it in the value pointed to by ptr.

type Encoder

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

An Encoder writes and encodes values to a Boost binary serialization stream.

Example
package main

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"log"

	"github.com/go-boostio/boostio/binser"
)

func main() {
	buf := new(bytes.Buffer)
	enc := binser.NewEncoder(buf)

	for _, v := range []interface{}{"hello", int32(0x44444444)} {
		err := enc.Encode(v)
		if err != nil {
			log.Fatal(err)
		}
	}

	fmt.Printf("%s\n", hex.Dump(buf.Bytes()))

	dec := binser.NewDecoder(buf)
	var str = ""
	err := dec.Decode(&str)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("string: %s\n", str)

	var i32 int32
	err = dec.Decode(&i32)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("int32:  %#x\n", i32)

}
Output:

00000000  16 00 00 00 00 00 00 00  73 65 72 69 61 6c 69 7a  |........serializ|
00000010  61 74 69 6f 6e 3a 3a 61  72 63 68 69 76 65 13 00  |ation::archive..|
00000020  04 08 04 08 01 00 00 00  05 00 00 00 00 00 00 00  |................|
00000030  68 65 6c 6c 6f 44 44 44  44                       |helloDDDD|

string: hello
int32:  0x44444444

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

The encoder writes a correct Boost binary header at the beginning of the archive.

func (*Encoder) Encode

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

Encode write the value v to its output.

type Header struct {
	Version uint16
	Flags   uint64
}

Header describes a binary boost archive.

func (Header) MarshalBoost

func (hdr Header) MarshalBoost(w *WBuffer) error

func (*Header) UnmarshalBoost

func (hdr *Header) UnmarshalBoost(r *RBuffer) error

type Marshaler

type Marshaler interface {
	MarshalBoost(w *WBuffer) error
}

Marshaler is the interface implemented by types that can marshal themselves into a valid binary Boost serialization archive.

type RBuffer

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

A RBuffer reads values from a Boost binary serialization stream.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/go-boostio/boostio/binser"
)

func main() {
	f, err := os.Open("testdata/data64.bin")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	r := binser.NewRBuffer(f)

	fmt.Printf("header: %#v\n", r.ReadHeader())

	fmt.Printf("bool: %v\n", r.ReadBool())
	fmt.Printf("bool: %v\n", r.ReadBool())
	fmt.Printf("int8: %#x\n", r.ReadI8())
	fmt.Printf("int16: %#x\n", r.ReadI16())
	fmt.Printf("int32: %#x\n", r.ReadI32())
	fmt.Printf("int64: %#x\n", r.ReadI64())
	fmt.Printf("uint8: %#x\n", r.ReadU8())
	fmt.Printf("uint16: %#x\n", r.ReadU16())
	fmt.Printf("uint32: %#x\n", r.ReadU32())
	fmt.Printf("uint64: %#x\n", r.ReadU64())
	fmt.Printf("float32: %v\n", r.ReadF32())
	fmt.Printf("float64: %v\n", r.ReadF64())

}
Output:

header: binser.Header{Version:0x13, Flags:0x108040804}
bool: false
bool: true
int8: 0x11
int16: 0x2222
int32: 0x33333333
int64: 0x4444444444444444
uint8: 0xff
uint16: 0x2222
uint32: 0x3333333
uint64: 0x444444444444444
float32: 2.2
float64: 3.3

func NewRBuffer

func NewRBuffer(r io.Reader) *RBuffer

NewRBuffer returns a new read-only buffer that reads from r.

func (*RBuffer) Err

func (r *RBuffer) Err() error

func (*RBuffer) Read

func (r *RBuffer) Read(p []byte) (int, error)

func (*RBuffer) ReadBool

func (r *RBuffer) ReadBool() bool

func (*RBuffer) ReadC128

func (r *RBuffer) ReadC128() complex128

func (*RBuffer) ReadC64

func (r *RBuffer) ReadC64() complex64

func (*RBuffer) ReadF32

func (r *RBuffer) ReadF32() float32

func (*RBuffer) ReadF64

func (r *RBuffer) ReadF64() float64

func (*RBuffer) ReadHeader

func (r *RBuffer) ReadHeader() Header

func (*RBuffer) ReadI16

func (r *RBuffer) ReadI16() int16

func (*RBuffer) ReadI32

func (r *RBuffer) ReadI32() int32

func (*RBuffer) ReadI64

func (r *RBuffer) ReadI64() int64

func (*RBuffer) ReadI8

func (r *RBuffer) ReadI8() int8

func (*RBuffer) ReadString

func (r *RBuffer) ReadString() string

func (*RBuffer) ReadTypeDescr

func (r *RBuffer) ReadTypeDescr(typ reflect.Type) TypeDescr

func (*RBuffer) ReadU16

func (r *RBuffer) ReadU16() uint16

func (*RBuffer) ReadU32

func (r *RBuffer) ReadU32() uint32

func (*RBuffer) ReadU64

func (r *RBuffer) ReadU64() uint64

func (*RBuffer) ReadU8

func (r *RBuffer) ReadU8() uint8

type TypeDescr

type TypeDescr struct {
	Version uint32
	Flags   uint8
}

TypeDescr describes an on-disk binary boost archive type.

func (TypeDescr) MarshalBoost

func (dt TypeDescr) MarshalBoost(w *WBuffer) error

func (*TypeDescr) UnmarshalBoost

func (dt *TypeDescr) UnmarshalBoost(r *RBuffer) error

type Unmarshaler

type Unmarshaler interface {
	UnmarshalBoost(r *RBuffer) error
}

Unmarshaler is the interface implemented by types that can unmarshal a binary Boost description of themselves.

type WBuffer

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

func NewWBuffer

func NewWBuffer(w io.Writer) *WBuffer

func (*WBuffer) Err

func (w *WBuffer) Err() error

func (*WBuffer) Write

func (w *WBuffer) Write(p []byte) (int, error)

func (*WBuffer) WriteBool

func (w *WBuffer) WriteBool(v bool) error

func (*WBuffer) WriteC128

func (w *WBuffer) WriteC128(v complex128) error

func (*WBuffer) WriteC64

func (w *WBuffer) WriteC64(v complex64) error

func (*WBuffer) WriteF32

func (w *WBuffer) WriteF32(v float32) error

func (*WBuffer) WriteF64

func (w *WBuffer) WriteF64(v float64) error

func (*WBuffer) WriteHeader

func (w *WBuffer) WriteHeader(hdr Header) error

func (*WBuffer) WriteI16

func (w *WBuffer) WriteI16(v int16) error

func (*WBuffer) WriteI32

func (w *WBuffer) WriteI32(v int32) error

func (*WBuffer) WriteI64

func (w *WBuffer) WriteI64(v int64) error

func (*WBuffer) WriteI8

func (w *WBuffer) WriteI8(v int8) error

func (*WBuffer) WriteString

func (w *WBuffer) WriteString(v string) error

func (*WBuffer) WriteTypeDescr

func (w *WBuffer) WriteTypeDescr(rt reflect.Type) error

func (*WBuffer) WriteU16

func (w *WBuffer) WriteU16(v uint16) error

func (*WBuffer) WriteU32

func (w *WBuffer) WriteU32(v uint32) error

func (*WBuffer) WriteU64

func (w *WBuffer) WriteU64(v uint64) error

func (*WBuffer) WriteU8

func (w *WBuffer) WriteU8(v uint8) error

Jump to

Keyboard shortcuts

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