bin

package module
v0.0.0-...-8573cf3 Latest Latest
Warning

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

Go to latest
Published: May 7, 2018 License: MIT Imports: 6 Imported by: 1

README

b71729/bin

A set of utility interfaces for working with binary data streams.

Coverage Build Status GoDoc


Installation

$ go get -u github.com/b71729/bin

Alternative packages

binary

An alternative to this package would be to use the Read(interface{}) exposed in the binary package for reading data. Although a relatively simple interface is provided, there are some distinct advantages to using this package over binary.Read:

  • IDE and compile-time type checking is possible; this helps prevent bugs
  • Built-in tracking of the reader's offset is provided
  • A byte order can be set for subsequent calls, without needing to keep track of it every call
  • Is ~2-3x faster in benchmarks
  • Allocates no objects in the various Read/ReadBytes/ReadXYZ methods.

Documentation

API documentation is hosted on GoDoc

Documentation

Overview

Package bin provides utility interfaces for working with binary data streams

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

Reader provides methods for reading various data types from an `io.Reader`.

func NewReader

func NewReader(source io.Reader, bo binary.ByteOrder) Reader

NewReader creates a new `Reader` encapsulating the given `source`, and using the byte order `bo` to specify endianness.

For futureproofing, it is suggested to use these constructors rather than manually creating an instance (i.e. `br := Reader{}`)

func NewReaderBytes

func NewReaderBytes(source []byte, bo binary.ByteOrder) Reader

NewReaderBytes creates a new `Reader` to read from the given `source`, using the byte order `bo` to specify endianness.

Since `source` is a slice of bytes, a `bytes.Reader` will wrap the slice to satisfy the `io.Reader` interface.

For futureproofing, it is suggested to use these constructors rather than manually creating an instance (i.e. `br := Reader{}`)

func (*Reader) Discard

func (b *Reader) Discard(n int64) error

Discard reads `n` bytes into a discarded buffer.

func (*Reader) GetByteOrder

func (b *Reader) GetByteOrder() binary.ByteOrder

GetByteOrder returns the current byte order.

Note that this can be `nil` if the interface was not created via a constructor method.

func (*Reader) GetPosition

func (b *Reader) GetPosition() int64

GetPosition returns the current reader offset as a 64-bit integer.

func (*Reader) Peek

func (b *Reader) Peek(dst []byte) error

Peek returns the next n bytes without advancing the reader. If the operation cannot fully write to `dst`, it will return an error.

func (*Reader) Read

func (b *Reader) Read(p []byte) (n int, err error)

Read satisfies the Liskov Subsitution Principle of its base `io.Reader`

func (*Reader) ReadByte

func (b *Reader) ReadByte(dst *byte) error

ReadByte reads one byte into `dst`.

func (*Reader) ReadBytes

func (b *Reader) ReadBytes(dst []byte) error

ReadBytes attempts to read `len(dst)` bytes into `dst`. Multiple calls will be made to `source.Read` in the case of a partial read.

If unable to completely read into `dst`, `io.ErrUnexpectedEOF` will be returned.

func (*Reader) ReadFloat32

func (b *Reader) ReadFloat32(dst *float32) error

ReadFloat32 reads a 32-bit IEEE 754 floating-point integer into `dst` according to the current byte order.

func (*Reader) ReadFloat64

func (b *Reader) ReadFloat64(dst *float64) error

ReadFloat64 reads a 64-bit IEEE 754 floating-point integer into `dst` according to the current byte order.

func (*Reader) ReadUint16

func (b *Reader) ReadUint16(dst *uint16) error

ReadUint16 reads an unsigned 16-bit integer into `dst` according to the current byte order.

func (*Reader) ReadUint32

func (b *Reader) ReadUint32(dst *uint32) error

ReadUint32 reads an unsigned 32-bit integer into `dst` according to the current byte order.

func (*Reader) ReadUint64

func (b *Reader) ReadUint64(dst *uint64) error

ReadUint64 reads an unsigned 64-bit integer into `dst` according to the current byte order.

func (*Reader) Reset

func (b *Reader) Reset(source io.Reader, bo binary.ByteOrder)

Reset resets the reader position and source `io.Reader` to `source`

func (*Reader) SetByteOrder

func (b *Reader) SetByteOrder(bo binary.ByteOrder)

SetByteOrder sets the current byte order to `bo`. This can be done on-the-fly.

type Writer

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

Writer provides methods for reading various data types to an `io.Writer`.

func NewWriter

func NewWriter(dest io.Writer, bo binary.ByteOrder) (bw Writer)

NewWriter creates a new `Writer` targetted at the given `dest`, and using the byte order `bo` to specify endianness.

For futureproofing, it is suggested to use these constructors rather than manually creating an instance (i.e. `bw := Writer{}`)

func (*Writer) GetByteOrder

func (b *Writer) GetByteOrder() binary.ByteOrder

GetByteOrder returns the current byte order.

Note that this can be `nil` if the interface was not created via a constructor method.

func (*Writer) GetPosition

func (b *Writer) GetPosition() int64

GetPosition returns the current reader offset as a 64-bit integer.

func (*Writer) Reset

func (b *Writer) Reset(dest io.Writer, bo binary.ByteOrder)

Reset resets the writer position and source `io.Writer` to `dest`

func (*Writer) SetByteOrder

func (b *Writer) SetByteOrder(bo binary.ByteOrder)

SetByteOrder sets the current byte order to `bo`. This can be done on-the-fly.

func (*Writer) Write

func (b *Writer) Write(p []byte) (n int, err error)

Write satisfies the Liskov Subsitution Principle of its base `io.Writer`

func (*Writer) WriteByte

func (b *Writer) WriteByte(src byte) error

WriteByte writes a byte

func (*Writer) WriteBytes

func (b *Writer) WriteBytes(src []byte) error

WriteBytes writes all bytes from `src`

func (*Writer) WriteFloat32

func (b *Writer) WriteFloat32(src float32) error

WriteFloat32 writes a 32-bit IEEE 754 floating-point integer according to the current byte order.

func (*Writer) WriteFloat64

func (b *Writer) WriteFloat64(src float64) error

WriteFloat64 writes a 64-bit IEEE 754 floating-point integer according to the current byte order.

func (*Writer) WriteUint16

func (b *Writer) WriteUint16(src uint16) error

WriteUint16 writes an unsigned 16-bit integer according to the current byte order.

func (*Writer) WriteUint32

func (b *Writer) WriteUint32(src uint32) error

WriteUint32 writes an unsigned 32-bit integer according to the current byte order.

func (*Writer) WriteUint64

func (b *Writer) WriteUint64(src uint64) error

WriteUint64 writes an unsigned 64-bit integer according to the current byte order.

func (*Writer) ZeroFill

func (b *Writer) ZeroFill(n int64) error

ZeroFill writes `n` null-bytes.

Jump to

Keyboard shortcuts

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