Documentation ¶
Overview ¶
Package binary approaches binary encoding with a different approach from encoding/binary. The main aim of this package is to be *fast*, and thus not to use reflection, and with more type-safety.
By its nature, this package is statically typed, and as such will not support arrays, structs and slices of non-builtin types (or slices of slices). Apart from that, behaviour is pretty much like that of encoding/binary.
Index ¶
- Variables
- type Reader
- func (r *Reader) Bool() bool
- func (r *Reader) BoolSlice(length int) []bool
- func (r *Reader) Byte() byte
- func (r *Reader) ByteSlice(length int) []byte
- func (r *Reader) Complex128() complex128
- func (r *Reader) Complex128Slice(length int) []complex128
- func (r *Reader) Complex64() complex64
- func (r *Reader) Complex64Slice(length int) []complex64
- func (r *Reader) End() (int, error)
- func (r *Reader) Float32() float32
- func (r *Reader) Float32Slice(length int) []float32
- func (r *Reader) Float64() float64
- func (r *Reader) Float64Slice(length int) []float64
- func (r *Reader) Int16() int16
- func (r *Reader) Int16Slice(length int) []int16
- func (r *Reader) Int32() int32
- func (r *Reader) Int32Slice(length int) []int32
- func (r *Reader) Int64() int64
- func (r *Reader) Int64Slice(length int) []int64
- func (r *Reader) Int8() int8
- func (r *Reader) Int8Slice(length int) []int8
- func (r *Reader) Rune() rune
- func (r *Reader) RuneSlice(length int) []rune
- func (r *Reader) String(length int) string
- func (r *Reader) Uint16() uint16
- func (r *Reader) Uint16Slice(length int) []uint16
- func (r *Reader) Uint32() uint32
- func (r *Reader) Uint32Slice(length int) []uint32
- func (r *Reader) Uint64() uint64
- func (r *Reader) Uint64Slice(length int) []uint64
- func (r *Reader) Uint8() uint8
- func (r *Reader) Uint8Slice(length int) []uint8
- type WriteChain
- func (c *WriteChain) Bool(b bool) *WriteChain
- func (c *WriteChain) BoolSlice(b []bool) *WriteChain
- func (c *WriteChain) Byte(b byte) *WriteChain
- func (c *WriteChain) ByteSlice(b []byte) *WriteChain
- func (c *WriteChain) Complex128(com complex128) *WriteChain
- func (c *WriteChain) Complex128Slice(com []complex128) *WriteChain
- func (c *WriteChain) Complex64(com complex64) *WriteChain
- func (c *WriteChain) Complex64Slice(com []complex64) *WriteChain
- func (c *WriteChain) End() (int, error)
- func (c *WriteChain) Float32(f float32) *WriteChain
- func (c *WriteChain) Float32Slice(f []float32) *WriteChain
- func (c *WriteChain) Float64(f float64) *WriteChain
- func (c *WriteChain) Float64Slice(f []float64) *WriteChain
- func (c *WriteChain) Int16(i int16) *WriteChain
- func (c *WriteChain) Int16Slice(i []int16) *WriteChain
- func (c *WriteChain) Int32(i int32) *WriteChain
- func (c *WriteChain) Int32Slice(i []int32) *WriteChain
- func (c *WriteChain) Int64(i int64) *WriteChain
- func (c *WriteChain) Int64Slice(i []int64) *WriteChain
- func (c *WriteChain) Int8(i int8) *WriteChain
- func (c *WriteChain) Int8Slice(i []int8) *WriteChain
- func (c *WriteChain) Rune(r rune) *WriteChain
- func (c *WriteChain) RuneSlice(r []rune) *WriteChain
- func (c *WriteChain) String(s string) *WriteChain
- func (c *WriteChain) Uint16(u uint16) *WriteChain
- func (c *WriteChain) Uint16Slice(u []uint16) *WriteChain
- func (c *WriteChain) Uint32(u uint32) *WriteChain
- func (c *WriteChain) Uint32Slice(u []uint32) *WriteChain
- func (c *WriteChain) Uint64(u uint64) *WriteChain
- func (c *WriteChain) Uint64Slice(u []uint64) *WriteChain
- func (c *WriteChain) Uint8(u uint8) *WriteChain
- func (c *WriteChain) Uint8Slice(u []uint8) *WriteChain
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( LittleEndian = encodingBinary.LittleEndian BigEndian = encodingBinary.BigEndian )
Brought from the encoding/binary package as a shorthand.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct { Reader io.Reader ByteOrder encodingBinary.ByteOrder // contains filtered or unexported fields }
Reader is the simplified version of ReadChain. Instead of having to pass pointers to decode bytes, using Reader you can read data directly and do, for instance, simple assignments.
Example ¶
package main import ( "bytes" "fmt" "github.com/thehowl/binary" ) func main() { buf := bytes.NewBuffer([]byte("\x05\xff\x02\x00")) r := &binary.Reader{ Reader: buf, ByteOrder: binary.LittleEndian, } var ( b1 = r.Byte() b2 = r.Int8() i = r.Int16() ) _, err := r.End() if err != nil { panic(err) } fmt.Printf("%d - %d - %d\n", b1, b2, i) }
Output: 5 - -1 - 2
func (*Reader) Complex128 ¶
func (r *Reader) Complex128() complex128
Complex128 decodes a Complex128 from the Reader.
func (*Reader) Complex128Slice ¶
func (r *Reader) Complex128Slice(length int) []complex128
Complex128Slice decodes a slice of complex128s from the Reader.
func (*Reader) Complex64Slice ¶
Complex64Slice decodes a slice of complex64s from the Reader.
func (*Reader) End ¶
End returns the amount of read bytes and any eventual error, and sets the internal amount of written bytes to 0, and the error to nil.
func (*Reader) Float32Slice ¶
Float32Slice decodes a slice of float32s from the Reader.
func (*Reader) Float64Slice ¶
Float64Slice decodes a slice of float64s from the Reader.
func (*Reader) Int16Slice ¶
Int16Slice decodes a slice of int16s.
func (*Reader) Int32Slice ¶
Int32Slice decodes a slice of int32s.
func (*Reader) Int64Slice ¶
Int64Slice decodes a slice of int64s.
func (*Reader) Uint16Slice ¶
Uint16Slice decodes a slice of uint16s.
func (*Reader) Uint32Slice ¶
Uint32Slice decodes a slice of uint32s.
func (*Reader) Uint64Slice ¶
Uint64Slice decodes a slice of uint64s.
func (*Reader) Uint8Slice ¶
Uint8Slice decodes a slice of uint8s from the Reader.
type WriteChain ¶
type WriteChain struct { Writer io.Writer ByteOrder encodingBinary.ByteOrder // contains filtered or unexported fields }
WriteChain wraps around an io.Writer and a encoding.ByteOrder, and writes data into the Writer using the given ByteOrder. WriteChain is not thread-safe.
Example ¶
package main import ( "bytes" "fmt" "github.com/thehowl/binary" ) func main() { buf := &bytes.Buffer{} writer := &binary.WriteChain{ Writer: buf, ByteOrder: binary.LittleEndian, } _, err := writer. Uint16(266). Byte(1). Uint32(2). End() if err != nil { panic(err) } fmt.Printf("% x\n", buf.Bytes()) }
Output: 0a 01 01 02 00 00 00
func (*WriteChain) Bool ¶
func (c *WriteChain) Bool(b bool) *WriteChain
Bool encodes a boolean into the write, transforming true into uint8(1) and false into uint8(0).
func (*WriteChain) BoolSlice ¶
func (c *WriteChain) BoolSlice(b []bool) *WriteChain
BoolSlice encodes a slice of bools into the writer.
func (*WriteChain) Byte ¶
func (c *WriteChain) Byte(b byte) *WriteChain
Byte encodes a byte into the writer.
func (*WriteChain) ByteSlice ¶
func (c *WriteChain) ByteSlice(b []byte) *WriteChain
ByteSlice encodes a slice of bytes into the writer.
func (*WriteChain) Complex128 ¶
func (c *WriteChain) Complex128(com complex128) *WriteChain
Complex128 encodes a complex128 into the writer.
func (*WriteChain) Complex128Slice ¶
func (c *WriteChain) Complex128Slice(com []complex128) *WriteChain
Complex128Slice encodes a slice of complex128s into the writer.
func (*WriteChain) Complex64 ¶
func (c *WriteChain) Complex64(com complex64) *WriteChain
Complex64 encodes a complex64 into the writer.
func (*WriteChain) Complex64Slice ¶
func (c *WriteChain) Complex64Slice(com []complex64) *WriteChain
Complex64Slice encodes a slice of complex64s into the writer.
func (*WriteChain) End ¶
func (c *WriteChain) End() (int, error)
End finishes writing to the Writer, and returns the amount of written bytes and any eventual error occured during the WriteChain lifetime. It also clears out the written bytes and the error, making WriteChain usable as defined by the user initially again.
func (*WriteChain) Float32 ¶
func (c *WriteChain) Float32(f float32) *WriteChain
Float32 encodes a float32 into the writer.
func (*WriteChain) Float32Slice ¶
func (c *WriteChain) Float32Slice(f []float32) *WriteChain
Float32Slice encodes a slice of float32s into the writer.
func (*WriteChain) Float64 ¶
func (c *WriteChain) Float64(f float64) *WriteChain
Float64 encodes a float64 into the writer.
func (*WriteChain) Float64Slice ¶
func (c *WriteChain) Float64Slice(f []float64) *WriteChain
Float64Slice encodes a slice of float64s into the writer.
func (*WriteChain) Int16 ¶
func (c *WriteChain) Int16(i int16) *WriteChain
Int16 encodes an int16 into the writer.
func (*WriteChain) Int16Slice ¶
func (c *WriteChain) Int16Slice(i []int16) *WriteChain
Int16Slice encodes a slice of int16s into the writer.
func (*WriteChain) Int32 ¶
func (c *WriteChain) Int32(i int32) *WriteChain
Int32 encodes an int32 into the writer.
func (*WriteChain) Int32Slice ¶
func (c *WriteChain) Int32Slice(i []int32) *WriteChain
Int32Slice encodes a slice of int32s into the writer.
func (*WriteChain) Int64 ¶
func (c *WriteChain) Int64(i int64) *WriteChain
Int64 encodes an int64 into the writer.
func (*WriteChain) Int64Slice ¶
func (c *WriteChain) Int64Slice(i []int64) *WriteChain
Int64Slice encodes a slice of int64s into the writer.
func (*WriteChain) Int8 ¶
func (c *WriteChain) Int8(i int8) *WriteChain
Int8 encodes an int8 into the writer.
func (*WriteChain) Int8Slice ¶
func (c *WriteChain) Int8Slice(i []int8) *WriteChain
Int8Slice encodes a slice of int8s into the writer.
func (*WriteChain) Rune ¶
func (c *WriteChain) Rune(r rune) *WriteChain
Rune encodes a rune into the writer.
func (*WriteChain) RuneSlice ¶
func (c *WriteChain) RuneSlice(r []rune) *WriteChain
RuneSlice encodes a slice of runes into the writer.
func (*WriteChain) String ¶
func (c *WriteChain) String(s string) *WriteChain
String encodes a string into the writer.
func (*WriteChain) Uint16 ¶
func (c *WriteChain) Uint16(u uint16) *WriteChain
Uint16 encodes an uint16 into the writer.
func (*WriteChain) Uint16Slice ¶
func (c *WriteChain) Uint16Slice(u []uint16) *WriteChain
Uint16Slice encodes a slice of uint16s into the writer.
func (*WriteChain) Uint32 ¶
func (c *WriteChain) Uint32(u uint32) *WriteChain
Uint32 encodes an uint32 into the writer.
func (*WriteChain) Uint32Slice ¶
func (c *WriteChain) Uint32Slice(u []uint32) *WriteChain
Uint32Slice encodes a slice of uint32s into the writer.
func (*WriteChain) Uint64 ¶
func (c *WriteChain) Uint64(u uint64) *WriteChain
Uint64 encodes an uint64 into the writer.
func (*WriteChain) Uint64Slice ¶
func (c *WriteChain) Uint64Slice(u []uint64) *WriteChain
Uint64Slice encodes a slice of uint64s into the writer.
func (*WriteChain) Uint8 ¶
func (c *WriteChain) Uint8(u uint8) *WriteChain
Uint8 encodes an uint8 into the writer.
func (*WriteChain) Uint8Slice ¶
func (c *WriteChain) Uint8Slice(u []uint8) *WriteChain
Uint8Slice encodes a slice of uint8s into the writer.