binary

package module
v3.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2018 License: MIT Imports: 3 Imported by: 10

README

Binary

GoDoc

This is a simple binary library written in Go.

Installation

You can get the package with go get command.

go get -u https://github.com/beito123/binary

-u is a option updating the package

License

These codes are under the MIT License.

Examples

Read
func main() {
	data := []byte{0xff, 0xff, 0xff, 0xff} // = -1 (int32)

	stream := binary.NewStreamBytes(data) // stream with bytes
	
	value, err := stream.Int() // int32
	if err != nil {
		panic(err)
	}

	fmt.Printf("Int32 value: %d", value) // Int value: -1
}
Write
func main() {
	stream := binary.NewStream() // empty bytes

	var value int32 = -1
	
	err := stream.WriteInt(value)
	if err != nil {
		panic(err)
	}

	// Bytes: []byte{0xff, 0xff, 0xff, 0xff}
	fmt.Printf("Bytes: %#v", stream.Bytes())
}

Documentation

Index

Constants

View Source
const (
	// ByteSize is byte size of Byte
	ByteSize = 1

	// ShortSize is byte size of Short
	ShortSize = 2

	// IntSize is byte size of Int
	IntSize = 4

	// LongSize is byte size of Long
	LongSize = 8

	// FloatSize is byte size of Float
	FloatSize = 4

	// DoubleSize is byte size of Double
	DoubleSize = 8
)

Variables

View Source
var BigEndian bigEndian

BigEndian .

View Source
var ErrNotEnought = errors.New("binary: not enough bytes")
View Source
var LittleEndian littleEndian

LittleEndian .

Functions

func Read

func Read(reader io.Reader, order Order, data interface{}) error

Read reads data into b by order

func ReadByte

func ReadByte(v []byte) byte

func ReadDouble

func ReadDouble(v []byte) float64

func ReadEByte

func ReadEByte(v []byte) (byte, error)

func ReadEDouble

func ReadEDouble(v []byte) (float64, error)

func ReadEFloat

func ReadEFloat(v []byte) (float32, error)

func ReadEInt

func ReadEInt(v []byte) (int32, error)

func ReadELDouble

func ReadELDouble(v []byte) (float64, error)

func ReadELFloat

func ReadELFloat(v []byte) (float32, error)

func ReadELInt

func ReadELInt(v []byte) (int32, error)

func ReadELLong

func ReadELLong(v []byte) (int64, error)

func ReadELShort

func ReadELShort(v []byte) (int16, error)

func ReadELUInt

func ReadELUInt(v []byte) (uint32, error)

func ReadELULong

func ReadELULong(v []byte) (uint64, error)

func ReadELUShort

func ReadELUShort(v []byte) (uint16, error)

func ReadELong

func ReadELong(v []byte) (int64, error)

func ReadESByte

func ReadESByte(v []byte) (int8, error)

func ReadEShort

func ReadEShort(v []byte) (int16, error)

func ReadEUInt

func ReadEUInt(v []byte) (uint32, error)

func ReadEULong

func ReadEULong(v []byte) (uint64, error)

func ReadEUShort

func ReadEUShort(v []byte) (uint16, error)

func ReadFloat

func ReadFloat(v []byte) float32

func ReadInt

func ReadInt(v []byte) int32

func ReadLDouble

func ReadLDouble(v []byte) float64

func ReadLFloat

func ReadLFloat(v []byte) float32

func ReadLInt

func ReadLInt(v []byte) int32

func ReadLLong

func ReadLLong(v []byte) int64

func ReadLShort

func ReadLShort(v []byte) int16

func ReadLUInt

func ReadLUInt(v []byte) uint32

func ReadLULong

func ReadLULong(v []byte) uint64

func ReadLUShort

func ReadLUShort(v []byte) uint16

func ReadLong

func ReadLong(v []byte) int64

func ReadSByte

func ReadSByte(v []byte) int8

func ReadShort

func ReadShort(v []byte) int16

func ReadUInt

func ReadUInt(v []byte) uint32

func ReadULong

func ReadULong(v []byte) uint64

func ReadUShort

func ReadUShort(v []byte) uint16

func Write

func Write(writer io.Writer, order Order, data interface{}) error

Write writes the contents of data into buffer by order

func WriteByte

func WriteByte(v byte) []byte

func WriteDouble

func WriteDouble(v float64) []byte

func WriteFloat

func WriteFloat(v float32) []byte

func WriteInt

func WriteInt(v int32) []byte

func WriteLDouble

func WriteLDouble(v float64) []byte

func WriteLFloat

func WriteLFloat(v float32) []byte

func WriteLInt

func WriteLInt(v int32) []byte

func WriteLLong

func WriteLLong(v int64) []byte

func WriteLShort

func WriteLShort(v int16) []byte

func WriteLUInt

func WriteLUInt(v uint32) []byte

func WriteLULong

func WriteLULong(v uint64) []byte

func WriteLUShort

func WriteLUShort(v uint16) []byte

func WriteLong

func WriteLong(v int64) []byte

func WriteSByte

func WriteSByte(v int8) []byte

func WriteShort

func WriteShort(v int16) []byte

func WriteUInt

func WriteUInt(v uint32) []byte

func WriteULong

func WriteULong(v uint64) []byte

func WriteUShort

func WriteUShort(v uint16) []byte

Types

type Order

type Order interface {
	Byte(v []byte) byte
	SByte(v []byte) int8
	Short(v []byte) int16
	UShort(v []byte) uint16
	Int(v []byte) int32
	UInt(v []byte) uint32
	Long(v []byte) int64
	ULong(v []byte) uint64
	Float(v []byte) float32
	Double(v []byte) float64
	PutByte(v byte) []byte
	PutSByte(v int8) []byte
	PutShort(v int16) []byte
	PutUShort(v uint16) []byte
	PutInt(v int32) []byte
	PutUInt(v uint32) []byte
	PutLong(v int64) []byte
	PutULong(v uint64) []byte
	PutFloat(v float32) []byte
	PutDouble(v float64) []byte
}

Order is a byte order interface

type OrderStream

type OrderStream struct {
	*Stream
	Order Order
}

OrderStream is a binary stream with order

func NewOrderStream

func NewOrderStream(order Order) *OrderStream

NewOrderStream returns new Stream

func NewOrderStreamBytes

func NewOrderStreamBytes(order Order, b []byte) *OrderStream

NewOrderStreamBytes returns new Stream from bytes

func (*OrderStream) Double

func (bs *OrderStream) Double() (value float64, err error)

Double sets double got from buffer to value

func (*OrderStream) Float

func (bs *OrderStream) Float() (value float32, err error)

Float sets float got from buffer to value

func (*OrderStream) Int

func (bs *OrderStream) Int() (value int32, err error)

Int sets int got from buffer to value

func (*OrderStream) Long

func (bs *OrderStream) Long() (value int64, err error)

Long sets long got from buffer to value

func (*OrderStream) PutDouble

func (bs *OrderStream) PutDouble(value float64) error

PutFloat puts double from value to buffer

func (*OrderStream) PutFloat

func (bs *OrderStream) PutFloat(value float32) error

PutFloat puts float from value to buffer

func (*OrderStream) PutInt

func (bs *OrderStream) PutInt(value int32) error

PutInt puts int from value to buffer

func (*OrderStream) PutLong

func (bs *OrderStream) PutLong(value int64) error

PutLong puts long from value to buffer

func (*OrderStream) PutSShort

func (bs *OrderStream) PutSShort(value int16) error

PutSShort puts short(sign) from value to buffer

func (*OrderStream) PutShort

func (bs *OrderStream) PutShort(value uint16) error

PutShort puts short(unsign) from value to buffer

func (*OrderStream) SShort

func (bs *OrderStream) SShort() (value int16, err error)

SShort sets short(sign) got from buffer to value

func (*OrderStream) Short

func (bs *OrderStream) Short() (value uint16, err error)

Short sets short(unsign) got from buffer to value

type Stream

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

Stream is basic binary stream.

func NewStream

func NewStream() *Stream

NewStream returns new Stream

func NewStreamBytes

func NewStreamBytes(b []byte) *Stream

NewStreamBytes returns new Stream from bytes

func (*Stream) AllBytes

func (bs *Stream) AllBytes() []byte

AllBytes return all bytes

func (*Stream) Bool

func (bs *Stream) Bool() (bool, error)

Bool sets byte got from buffer as bool to value

func (*Stream) Byte

func (bs *Stream) Byte() (byte, error)

Byte sets byte(unsign) got from buffer to value

func (*Stream) Bytes

func (bs *Stream) Bytes() []byte

Bytes returns the bytes left from Buffer.

func (*Stream) Double

func (bs *Stream) Double() (float64, error)

Double sets double got from buffer to value

func (*Stream) Float

func (bs *Stream) Float() (float32, error)

Float sets float got from buffer to value

func (*Stream) Get

func (bs *Stream) Get(n int) []byte

Get returns n bytes from Buffer with []byte

func (*Stream) Int

func (bs *Stream) Int() (int32, error)

Int sets int got from buffer to value

func (*Stream) LDouble

func (bs *Stream) LDouble() (float64, error)

Double sets double got from buffer to value as LittleEndian

func (*Stream) LFloat

func (bs *Stream) LFloat() (float32, error)

Float sets float got from buffer to value as LittleEndian

func (*Stream) LInt

func (bs *Stream) LInt() (int32, error)

Int sets int got from buffer to value as LittleEndian

func (*Stream) LLong

func (bs *Stream) LLong() (int64, error)

Long sets long got from buffer to value as LittleEndian

func (*Stream) LSShort

func (bs *Stream) LSShort() (int16, error)

LSShort sets short(sign) got from buffer as LittleEndian to value

func (*Stream) LShort

func (bs *Stream) LShort() (uint16, error)

LShort sets short(unsign) got from buffer as LittleEndian to value

func (*Stream) Len

func (bs *Stream) Len() int

Len returns len the bytes left

func (*Stream) Long

func (bs *Stream) Long() (int64, error)

Long sets long got from buffer to value

func (*Stream) Off

func (bs *Stream) Off() int

Off returns offset

func (*Stream) Pad

func (bs *Stream) Pad(le int) error

Pad puts empty bytes (0x00) of le (len).

func (*Stream) Put

func (bs *Stream) Put(value []byte) error

Put puts value to buffer

func (*Stream) PutBool

func (bs *Stream) PutBool(value bool) error

PutBool puts bool as byte from value to buffer

func (*Stream) PutByte

func (bs *Stream) PutByte(value byte) error

PutByte puts byte(unsign) from value to buffer

func (*Stream) PutDouble

func (bs *Stream) PutDouble(value float64) error

PutFloat puts double from value to buffer

func (*Stream) PutFloat

func (bs *Stream) PutFloat(value float32) error

PutFloat puts float from value to buffer

func (*Stream) PutInt

func (bs *Stream) PutInt(value int32) error

PutInt puts int from value to buffer

func (*Stream) PutLDouble

func (bs *Stream) PutLDouble(value float64) error

PutFloat puts double from value to buffer as LittleEndian

func (*Stream) PutLFloat

func (bs *Stream) PutLFloat(value float32) error

PutFloat puts float from value to buffer as LittleEndian

func (*Stream) PutLInt

func (bs *Stream) PutLInt(value int32) error

PutInt puts int from value to buffer as LittleEndian

func (*Stream) PutLLong

func (bs *Stream) PutLLong(value int64) error

PutLong puts long from value to buffer as LittleEndian

func (*Stream) PutLSShort

func (bs *Stream) PutLSShort(value int16) error

PutLSShort puts short(sign) from value to buffer as LittleEndian

func (*Stream) PutLShort

func (bs *Stream) PutLShort(value uint16) error

PutLShort puts short(unsign) from value to buffer as LittleEndian

func (*Stream) PutLong

func (bs *Stream) PutLong(value int64) error

PutLong puts long from value to buffer

func (*Stream) PutSByte

func (bs *Stream) PutSByte(value int8) error

PutSByte puts byte(sign) from value to buffer

func (*Stream) PutSShort

func (bs *Stream) PutSShort(value int16) error

PutSShort puts short(sign) from value to buffer

func (*Stream) PutShort

func (bs *Stream) PutShort(value uint16) error

PutShort puts short(unsign) from value to buffer

func (*Stream) Read

func (bs *Stream) Read(p []byte) (n int, err error)

Read reads and sets p

func (*Stream) Reset

func (bs *Stream) Reset()

Reset resets Buffer

func (*Stream) SByte

func (bs *Stream) SByte() (int8, error)

SByte sets byte(sign) got from buffer to value

func (*Stream) SShort

func (bs *Stream) SShort() (int16, error)

SShort sets short(sign) got from buffer to value

func (*Stream) SetBytes

func (bs *Stream) SetBytes(b []byte)

SetBytes sets bytes

func (*Stream) Short

func (bs *Stream) Short() (uint16, error)

Short sets short(unsign) got from buffer to value

func (*Stream) Skip

func (bs *Stream) Skip(n int)

Skip skips n bytes on buffer

func (*Stream) Write

func (bs *Stream) Write(p []byte) (n int, err error)

Write writes p

Jump to

Keyboard shortcuts

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