binstream

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: Apache-2.0 Imports: 6 Imported by: 2

README

binstream

Package binstream implements a flexible and lightweight binary stream with a clean and simple API similar to Go's bytes.Reader.

The package serves as a great foundation for parsing binary file formats, it also gives complete control to the user in a transparent API.

Documentation

Index

Constants

View Source
const CacheSize = 8

CacheSize is the current size of the cache we hold when peeking around the stream

View Source
const ChunkSize = 1024

ChunkSize is the default chunk size we use to read from files.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteStream

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

ByteStream represents an in-memory binary stream.

func NewByteStream

func NewByteStream(b []byte) (*ByteStream, error)

NewByteStream creates a new binary stream from a byte slice (can be memory mapped file).

func (*ByteStream) Close

func (bs *ByteStream) Close() error

Close underlying stream.

func (*ByteStream) IsEOF

func (bs *ByteStream) IsEOF() bool

IsEOF checks if we reached the end of the bytestream.

func (*ByteStream) Len

func (bs *ByteStream) Len() int

Len returns the current binary stream size.

func (*ByteStream) Pos

func (bs *ByteStream) Pos() int

Pos returns the current position within the binary stream.

func (*ByteStream) Read

func (bs *ByteStream) Read(b []byte) (n int, err error)

Read len(b) bytes from the underlying stream.

func (*ByteStream) ReadAt

func (bs *ByteStream) ReadAt(b []byte, off int64) (n int, err error)

ReadAt len(b) bytes from the underlying stream at a given offset.

func (*ByteStream) ReadUint8

func (bs *ByteStream) ReadUint8(off int64) (uint8, error)

ReadUint8 reads a 1 byte little endian unsigned integer.

func (*ByteStream) ReadUint16

func (bs *ByteStream) ReadUint16(off int64) (uint16, error)

ReadUint16 reads a 2 byte little endian unsigned integer.

func (*ByteStream) ReadUint32

func (bs *ByteStream) ReadUint32(off int64) (uint32, error)

ReadUint32 reads a 4 byte little endian unsigned integer.

func (*ByteStream) ReadUint64

func (bs *ByteStream) ReadUint64(off int64) (uint64, error)

ReadUint64 reads a 8 byte little endian unsigned integer.

func (*ByteStream) Seek

func (bs *ByteStream) Seek(off int64, whence int) (n int64, err error)

Seek moves the stream iterator position to offset starting from whence.

type FileStream

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

FileStream represents an in-memory binary stream.

func NewFileStream

func NewFileStream(filename string) (*FileStream, error)

NewFileStream creates a new binary stream from a memory mapped file).

func (*FileStream) Close

func (fs *FileStream) Close() error

Close underlying filestream.

func (*FileStream) IsEOF

func (fs *FileStream) IsEOF() bool

IsEOF checks if we reached the end of the bytestream.

func (*FileStream) Len

func (fs *FileStream) Len() int

Len returns the current binary stream size.

func (*FileStream) Pos

func (fs *FileStream) Pos() int

Pos returns the current position within the binary stream.

func (*FileStream) Read

func (fs *FileStream) Read(b []byte) (n int, err error)

Read len(b) bytes from the underlying stream.

func (*FileStream) ReadAt

func (fs *FileStream) ReadAt(b []byte, off int64) (n int, err error)

ReadAt len(b) bytes from the underlying stream at a given offset.

func (*FileStream) ReadUint8

func (fs *FileStream) ReadUint8(off int64) (uint8, error)

ReadUint8 reads a 1 byte little endian unsigned integer.

func (*FileStream) ReadUint16

func (fs *FileStream) ReadUint16(off int64) (uint16, error)

ReadUint16 reads a 2 byte little endian unsigned integer.

func (*FileStream) ReadUint32

func (fs *FileStream) ReadUint32(off int64) (uint32, error)

ReadUint32 reads a 4 byte little endian unsigned integer.

func (*FileStream) ReadUint64

func (fs *FileStream) ReadUint64(off int64) (uint64, error)

ReadUint64 reads a 8 byte little endian unsigned integer.

func (*FileStream) Seek

func (fs *FileStream) Seek(off int64, whence int) (n int64, err error)

Seek moves the stream iterator position to offset starting from whence.

type Stream

type Stream interface {
	Len() int // Current stream size
	// Small Reader Interface
	Read(b []byte) (n int, err error)              // Read len(b) from stream.
	ReadAt(b []byte, off int64) (n int, err error) // Read len(b) from stream starting at off.
	// Seek and position
	Pos() int                                        // Return current position in the stream
	Seek(off int64, whence int) (n int64, err error) // Move to offset by changing position
	// Close the underlying stream.
	// While this might seem unncessary for in-memory streams (GC will clean up buffer)
	// it is necessary to prevent a mis-use when dealing with file stream.s
	Close() error
}

Stream represents the generic interface each implementation should fullfill.

type StreamType

type StreamType int

StreamType represents the stream type we're reading from (a File or Memory)

const (
	// UNKNOWN is for unknown stream types.
	UNKNOWN StreamType = iota
	// FILE if we're reading from an on-disk file.
	FILE
	// MEMORY if we're reading from memory (byte slice)
	MEMORY
)

Jump to

Keyboard shortcuts

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