binary

package module
v0.0.0-...-c33f16c Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2018 License: MIT Imports: 5 Imported by: 1

README

GoDoc Build Status

binary

Development of this package has continued on master and has been published as a 2.0 - this is mostly kept for backwards compatibility for packages that rely on binary.

Version 2 has many more performance improvements and you should use that if you're considering this library.

To specify a version, you can either use something like gopkg:

import "gopkg.in/thehowl/binary.v1"

Or you can use dep, which allows to select the highest released version of a package (instead of its default branch).

Have fun!

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

Examples

Constants

This section is empty.

Variables

View Source
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) Bool

func (r *Reader) Bool() bool

Bool decodes a bool from the Reader.

func (*Reader) BoolSlice

func (r *Reader) BoolSlice(length int) []bool

BoolSlice decodes a slice of bools.

func (*Reader) Byte

func (r *Reader) Byte() byte

Byte decodes an uint64 from the Reader.

func (*Reader) ByteSlice

func (r *Reader) ByteSlice(length int) []byte

ByteSlice decodes a slice of bytes.

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) Complex64

func (r *Reader) Complex64() complex64

Complex64 decodes a Complex64 from the Reader.

func (*Reader) Complex64Slice

func (r *Reader) Complex64Slice(length int) []complex64

Complex64Slice decodes a slice of complex64s from the Reader.

func (*Reader) End

func (r *Reader) End() (int, error)

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) Float32

func (r *Reader) Float32() float32

Float32 decodes a float32 from the Reader.

func (*Reader) Float32Slice

func (r *Reader) Float32Slice(length int) []float32

Float32Slice decodes a slice of float32s from the Reader.

func (*Reader) Float64

func (r *Reader) Float64() float64

Float64 decodes a float64 from the Reader.

func (*Reader) Float64Slice

func (r *Reader) Float64Slice(length int) []float64

Float64Slice decodes a slice of float64s from the Reader.

func (*Reader) Int16

func (r *Reader) Int16() int16

Int16 decodes an int16 from the Reader.

func (*Reader) Int16Slice

func (r *Reader) Int16Slice(length int) []int16

Int16Slice decodes a slice of int16s.

func (*Reader) Int32

func (r *Reader) Int32() int32

Int32 decodes an int32 from the Reader.

func (*Reader) Int32Slice

func (r *Reader) Int32Slice(length int) []int32

Int32Slice decodes a slice of int32s.

func (*Reader) Int64

func (r *Reader) Int64() int64

Int64 decodes an int64 from the Reader.

func (*Reader) Int64Slice

func (r *Reader) Int64Slice(length int) []int64

Int64Slice decodes a slice of int64s.

func (*Reader) Int8

func (r *Reader) Int8() int8

Int8 decodes an int8 from the Reader.

func (*Reader) Int8Slice

func (r *Reader) Int8Slice(length int) []int8

Int8Slice decodes a slice of int8s.

func (*Reader) Rune

func (r *Reader) Rune() rune

Rune decodes an rune from the Reader.

func (*Reader) RuneSlice

func (r *Reader) RuneSlice(length int) []rune

RuneSlice decodes a slice of runes.

func (*Reader) String

func (r *Reader) String(length int) string

String decodes a string from the Reader.

func (*Reader) Uint16

func (r *Reader) Uint16() uint16

Uint16 decodes an uint16 from the Reader.

func (*Reader) Uint16Slice

func (r *Reader) Uint16Slice(length int) []uint16

Uint16Slice decodes a slice of uint16s.

func (*Reader) Uint32

func (r *Reader) Uint32() uint32

Uint32 decodes an uint32 from the Reader.

func (*Reader) Uint32Slice

func (r *Reader) Uint32Slice(length int) []uint32

Uint32Slice decodes a slice of uint32s.

func (*Reader) Uint64

func (r *Reader) Uint64() uint64

Uint64 decodes an uint64 from the Reader.

func (*Reader) Uint64Slice

func (r *Reader) Uint64Slice(length int) []uint64

Uint64Slice decodes a slice of uint64s.

func (*Reader) Uint8

func (r *Reader) Uint8() uint8

Uint8 decodes an uint8 from the Reader.

func (*Reader) Uint8Slice

func (r *Reader) Uint8Slice(length int) []uint8

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.

Jump to

Keyboard shortcuts

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