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 ¶
- Constants
- Variables
- type Arch
- type Decoder
- type Encoder
- type Header
- type Marshaler
- type RBuffer
- func (r *RBuffer) Err() error
- func (r *RBuffer) Read(p []byte) (int, error)
- func (r *RBuffer) ReadBool() bool
- func (r *RBuffer) ReadC128() complex128
- func (r *RBuffer) ReadC64() complex64
- func (r *RBuffer) ReadF32() float32
- func (r *RBuffer) ReadF64() float64
- func (r *RBuffer) ReadHeader() Header
- func (r *RBuffer) ReadI16() int16
- func (r *RBuffer) ReadI32() int32
- func (r *RBuffer) ReadI64() int64
- func (r *RBuffer) ReadI8() int8
- func (r *RBuffer) ReadString() string
- func (r *RBuffer) ReadTypeDescr(typ reflect.Type) TypeDescr
- func (r *RBuffer) ReadU16() uint16
- func (r *RBuffer) ReadU32() uint32
- func (r *RBuffer) ReadU64() uint64
- func (r *RBuffer) ReadU8() uint8
- type TypeDescr
- type Unmarshaler
- type WBuffer
- func (w *WBuffer) Err() error
- func (w *WBuffer) Write(p []byte) (int, error)
- func (w *WBuffer) WriteBool(v bool) error
- func (w *WBuffer) WriteC128(v complex128) error
- func (w *WBuffer) WriteC64(v complex64) error
- func (w *WBuffer) WriteF32(v float32) error
- func (w *WBuffer) WriteF64(v float64) error
- func (w *WBuffer) WriteHeader(hdr Header) error
- func (w *WBuffer) WriteI16(v int16) error
- func (w *WBuffer) WriteI32(v int32) error
- func (w *WBuffer) WriteI64(v int64) error
- func (w *WBuffer) WriteI8(v int8) error
- func (w *WBuffer) WriteString(v string) error
- func (w *WBuffer) WriteTypeDescr(rt reflect.Type) error
- func (w *WBuffer) WriteU16(v uint16) error
- func (w *WBuffer) WriteU32(v uint32) error
- func (w *WBuffer) WriteU64(v uint64) error
- func (w *WBuffer) WriteU8(v uint8) error
Examples ¶
Constants ¶
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 ¶
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 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 ¶
NewDecoder returns a new decoder that reads from r.
The decoder checks the stream has a correct Boost binary header.
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 ¶
NewEncoder returns a new encoder that writes to w.
The encoder writes a correct Boost binary header at the beginning of the archive.
type Header ¶
Header describes a binary boost archive.
func (Header) MarshalBoost ¶
func (*Header) UnmarshalBoost ¶
type Marshaler ¶
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 ¶
NewRBuffer returns a new read-only buffer that reads from r.
func (*RBuffer) ReadC128 ¶
func (r *RBuffer) ReadC128() complex128
func (*RBuffer) ReadHeader ¶
func (*RBuffer) ReadString ¶
type TypeDescr ¶
TypeDescr describes an on-disk binary boost archive type.
func (TypeDescr) MarshalBoost ¶
func (*TypeDescr) UnmarshalBoost ¶
type Unmarshaler ¶
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 (*WBuffer) WriteC128 ¶
func (w *WBuffer) WriteC128(v complex128) error