binarystreams

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package binarystreams is an abstraction on stream reading and writing to support processing standard data types (int, string, float, etc.) from/to binary 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 implements a bytestream reader. For now it is LSB only.

func NewReader

func NewReader(reader io.Reader) *Reader

NewReader returns a new Reader based on an io.Reader. This will end up opening a bufio.Reader from the handed reader.

func NewReaderFromBytes

func NewReaderFromBytes(data []byte) *Reader

NewReaderFromBytes returns a new Reader based on a byte array

func NewReaderFromFile

func NewReaderFromFile(path string) (*Reader, *os.File, error)

NewReaderFromFile returns a new Reader based on using os.Open on the passed path. It also returns the opened file object, for you to be able to close when done.

func (*Reader) IsEOF

func (r *Reader) IsEOF() bool

IsEOF returns whether the reader is at the end of the buffer

func (*Reader) PeekByte

func (r *Reader) PeekByte() (byte, error)

PeekByte takes advantage of the bufio.Reader to read a byte without moving the underlying stream offset forward (usually called a "Peek" operation).

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

ReadByte returns a single byte from the stream

func (*Reader) ReadBytes

func (r *Reader) ReadBytes(numBytes int) ([]byte, error)

ReadBytes reads a passed number of bytes from the stream and returns them as an array. If anything other than the exact requested number of bytes are read/returned, an error will be returned instead. ReadBytes handles re-issuing underlying IO reads to fill out the entire requested number of bytes.

func (*Reader) ReadFloat32

func (r *Reader) ReadFloat32() (float32, error)

ReadFloat32 reads a single float32 from the stream

func (*Reader) ReadFloat64

func (r *Reader) ReadFloat64() (float64, error)

ReadFloat64 reads a single float64 from the stream

func (*Reader) ReadInt8

func (r *Reader) ReadInt8() (int8, error)

ReadInt8 returns a single int8 from the stream

func (*Reader) ReadInt16

func (r *Reader) ReadInt16() (int16, error)

ReadInt16 returns a single int16 from the stream

func (*Reader) ReadInt32

func (r *Reader) ReadInt32() (int32, error)

ReadInt32 returns a single int32 from the stream

func (*Reader) ReadString

func (r *Reader) ReadString(numChars int) (string, error)

ReadString reads the passed number of bytes and returns them as a string.

func (*Reader) ReadStringToTerminator

func (r *Reader) ReadStringToTerminator(terminator byte) (string, uint32, error)

ReadStringToTerminator reads bytes until it runs into the passed terminator byte, and then returns everything up to (but not including) the terminator byte as a string.

func (*Reader) ReadUInt8

func (r *Reader) ReadUInt8() (uint8, error)

ReadUInt8 returns a single uint8 from the stream (same thing as ReadByte)

func (*Reader) ReadUInt16

func (r *Reader) ReadUInt16() (uint16, error)

ReadUInt16 returns a single uint16 from the stream

func (*Reader) ReadUInt32

func (r *Reader) ReadUInt32() (uint32, error)

ReadUInt32 returns a single uint32 from the stream

func (*Reader) ReadUInt64

func (r *Reader) ReadUInt64() (uint64, error)

ReadUInt64 returns a single uint64 from the stream

func (*Reader) ReadUint64String

func (r *Reader) ReadUint64String(numChars int) (uint64, error)

ReadUint64String reads the passed number of bytes in as a string and then converts them to a Uint64 with strconv.ParseUint (assumes base 10).

func (*Reader) ReadWideStringToTerminator

func (r *Reader) ReadWideStringToTerminator(terminator uint16) (string, uint32, error)

ReadWideStringToTerminator reads runes until it runs into the passed terminator rune, and then returns everything up to (but not including) the terminator rune as a string, as well as the length of bytes read.

func (*Reader) SkipBytes

func (r *Reader) SkipBytes(count int) error

SkipBytes skips the read offset forward a number of bytes

func (*Reader) Slice

func (r *Reader) Slice(count int) (*Reader, error)

Slice reads a byte array of the passed length and builds a sub-reader out of it

type Writer

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

Writer implements a bytestream writer. For now it is LSB only.

func NewMemoryWriter

func NewMemoryWriter() (*Writer, *bytes.Buffer)

NewMemoryWriter returns a new Writer that writes to a bytes.Buffer

func NewWriter

func NewWriter(writer io.Writer) *Writer

NewWriter returns a Writer object from an io.Writer. Note: This will end up building a bufio.Writer from it.

func NewWriterToFile

func NewWriterToFile(path string) (*Writer, *os.File, error)

NewWriterToFile builds a new Writer by calling os.Create on the passed path and handing it to NewWriter. This also returns the underlying *os.File for the caller to manually close when complete.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush flushes the underlying bufio.Writer stream, so call this before closing a file, if you are using a file stream.

func (*Writer) WriteByte

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

WriteByte writes a single passed byte to the underlying stream.

func (*Writer) WriteBytes

func (w *Writer) WriteBytes(bytes []byte) error

WriteBytes writes all of the passed bytes to the underlying stream, erroring if anything other than the entire slice was written out successfully.

func (*Writer) WriteFloat32

func (w *Writer) WriteFloat32(v float32) error

WriteFloat32 writes a float32, in big-endian

func (*Writer) WriteFloat64

func (w *Writer) WriteFloat64(v float64) error

WriteFloat64 writes a float64, in big-endian

func (*Writer) WriteInt8

func (w *Writer) WriteInt8(i int8) error

WriteInt8 writes a single passed int8 to the underlying stream.

func (*Writer) WriteInt16

func (w *Writer) WriteInt16(i int16) error

WriteInt16 writes a single passed int16 to the underlying stream.

func (*Writer) WriteInt32

func (w *Writer) WriteInt32(i int32) error

WriteInt32 writes a single passed int32 to the underlying stream.

func (*Writer) WriteUInt8

func (w *Writer) WriteUInt8(i uint8) error

WriteUInt8 writes a single passed uint8 to the underlying stream.

func (*Writer) WriteUInt16

func (w *Writer) WriteUInt16(i uint16) error

WriteUInt16 writes a single passed uint16 to the underlying stream.

func (*Writer) WriteUInt32

func (w *Writer) WriteUInt32(i uint32) error

WriteUInt32 writes a single passed uint32 to the underlying stream.

func (*Writer) WriteUInt64

func (w *Writer) WriteUInt64(i uint64) error

WriteUInt64 writes a single passed uint64 to the underlying stream.

func (*Writer) WriteUint64String

func (w *Writer) WriteUint64String(num uint64, numChars int) error

WriteUint64String takes a passed (uint64) number, converts it to a string, zero-pads it to make it numChars long, and then writes it out to the underlying stream (passes through to WriteBytes, in fact). If the stringified-number is longer than numChars characters, it will also error -- the intent of numChars is to write out exactly numChars bytes, no more, no fewer.

Jump to

Keyboard shortcuts

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