uio

package
v6.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2019 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package uio unifies commonly used io utilities for u-root.

uio's most used feature is the Buffer/Lexer combination to parse binary data of arbitrary endianness into data structures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromBigEndian

func FromBigEndian(obj Unmarshaler, b []byte) error

FromBigEndian unmarshals b into obj in big endian byte order.

func FromBytes

func FromBytes(obj Unmarshaler, b []byte, order binary.ByteOrder) error

FromBytes unmarshals b into obj in the given byte order.

func FromLittleEndian

func FromLittleEndian(obj Unmarshaler, b []byte) error

FromLittleEndian unmarshals b into obj in little endian byte order.

func FullLineWriter

func FullLineWriter(w LineWriter) io.WriteCloser

FullLineWriter returns an io.Writer that waits for a full line of prints before calling w.Write on one line each.

func MultiWriteCloser

func MultiWriteCloser(w ...io.Writer) io.WriteCloser

MultiWriteCloser is an io.MultiWriter that has an io.Closer and attempts to close those w's that have optional io.Closers.

func NewLazyOpener

func NewLazyOpener(open func() (io.Reader, error)) io.ReadCloser

NewLazyOpener returns a lazy io.Reader based on `open`.

func ReadAll

func ReadAll(r io.ReaderAt) ([]byte, error)

ReadAll reads everything that r contains.

Callers *must* not modify bytes in the returned byte slice.

If r is an in-memory representation, ReadAll will attempt to return a pointer to those bytes directly.

func Reader

func Reader(r io.ReaderAt) io.Reader

Reader generates a Reader from a ReaderAt.

func ReaderAtEqual

func ReaderAtEqual(r1, r2 io.ReaderAt) bool

ReaderAtEqual compares the contents of r1 and r2.

func ToBigEndian

func ToBigEndian(m Marshaler) []byte

ToBigEndian marshals m to big endian byte order.

func ToBytes

func ToBytes(m Marshaler, order binary.ByteOrder) []byte

ToBytes marshals m in the given byte order.

func ToLittleEndian

func ToLittleEndian(m Marshaler) []byte

ToLittleEndian marshals m to little endian byte order.

Types

type AlignReader

type AlignReader struct {
	R io.Reader
	N int
}

AlignReader keeps track of how many bytes were read so the reader can be aligned at a future time.

func (*AlignReader) Align

func (r *AlignReader) Align(n int) ([]byte, error)

Align aligns the reader to the given number of bytes and returns the bytes read to pad it.

func (*AlignReader) Read

func (r *AlignReader) Read(b []byte) (int, error)

Read reads from the underlying io.Reader.

func (*AlignReader) ReadByte

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

ReadByte reads one byte from the underlying io.Reader.

type AlignWriter

type AlignWriter struct {
	W io.Writer
	N int
}

AlignWriter keeps track of how many bytes were written so the writer can be aligned at a future time.

func (*AlignWriter) Align

func (w *AlignWriter) Align(n int, pad byte) error

Align aligns the writer to the given number of bytes using the given pad value.

func (*AlignWriter) Write

func (w *AlignWriter) Write(b []byte) (int, error)

Write writes to the underlying io.Writew.

type Buffer

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

Buffer implements functions to manipulate byte slices in a zero-copy way.

func NewBuffer

func NewBuffer(b []byte) *Buffer

NewBuffer Consumes b for marshaling or unmarshaling in the given byte order.

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap returns the available capacity.

func (*Buffer) Data

func (b *Buffer) Data() []byte

Data is unConsumed data remaining in the Buffer.

func (*Buffer) Has

func (b *Buffer) Has(n int) bool

Has returns true if n bytes are available.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the length of the remaining bytes.

func (*Buffer) Preallocate

func (b *Buffer) Preallocate(n int)

Preallocate increases the capacity of the buffer by n bytes.

func (*Buffer) ReadN

func (b *Buffer) ReadN(n int) ([]byte, error)

Consume Consumes n bytes from the Buffer. It returns nil, false if there aren't enough bytes left.

func (*Buffer) WriteN

func (b *Buffer) WriteN(n int) []byte

WriteN appends n bytes to the Buffer and returns a slice pointing to the newly appended bytes.

type CachingReader

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

CachingReader is a lazily caching wrapper of an io.Reader.

The wrapped io.Reader is only read from on demand, not upfront.

func NewCachingReader

func NewCachingReader(r io.Reader) *CachingReader

NewCachingReader buffers reads from r.

r is only read from when Read() is called.

func (*CachingReader) NewReader

func (cr *CachingReader) NewReader() io.Reader

NewReader returns a new io.Reader that reads cr from offset 0.

func (*CachingReader) Read

func (cr *CachingReader) Read(p []byte) (int, error)

Read reads from cr; implementing io.Reader.

TODO(chrisko): Decide whether to keep this or only keep NewReader().

func (*CachingReader) ReadAt

func (cr *CachingReader) ReadAt(p []byte, off int64) (int, error)

ReadAt reads from cr; implementing io.ReaderAt.

type LazyOpener

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

LazyOpener is a lazy io.Reader.

LazyOpener will use a given open function to derive an io.Reader when Read is first called on the LazyOpener.

func (*LazyOpener) Close

func (lr *LazyOpener) Close() error

Close implements io.Closer.Close.

func (*LazyOpener) Read

func (lr *LazyOpener) Read(p []byte) (int, error)

Read implements io.Reader.Read lazily.

If called for the first time, the underlying reader will be obtained and then used for the first and subsequent calls to Read.

type LazyOpenerAt

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

LazyOpenerAt is a lazy io.ReaderAt.

LazyOpenerAt will use a given open function to derive an io.ReaderAt when ReadAt is first called.

func (*LazyOpenerAt) Close

func (loa *LazyOpenerAt) Close() error

Close implements io.Closer.Close.

func (*LazyOpenerAt) ReadAt

func (loa *LazyOpenerAt) ReadAt(p []byte, off int64) (int, error)

ReadAt implements io.ReaderAt.ReadAt.

type Lexer

type Lexer struct {
	*Buffer
	// contains filtered or unexported fields
}

Lexer is a convenient encoder/decoder for buffers.

Use:

func (s *something) Unmarshal(l *Lexer) {
  s.Foo = l.Read8()
  s.Bar = l.Read8()
  s.Baz = l.Read16()
  return l.Error()
}

func NewBigEndianBuffer

func NewBigEndianBuffer(b []byte) *Lexer

NewBigEndianBuffer returns a new big endian coder for a new buffer.

func NewLexer

func NewLexer(b *Buffer, order binary.ByteOrder) *Lexer

NewLexer returns a new coder for buffers.

func NewLittleEndianBuffer

func NewLittleEndianBuffer(b []byte) *Lexer

NewLittleEndianBuffer returns a new little endian coder for a new buffer.

func NewNativeEndianBuffer

func NewNativeEndianBuffer(b []byte) *Lexer

NewNativeEndianBuffer returns a new native endian coder for a new buffer.

func (*Lexer) Align

func (l *Lexer) Align(n int)

Align appends bytes to align the length of the buffer to be divisible by n.

func (*Lexer) Append

func (l *Lexer) Append(n int) []byte

Append returns a newly appended n-size Buffer to write to.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Consume

func (l *Lexer) Consume(n int) []byte

Consume returns a slice of the next n bytes from the buffer.

Consume gives direct access to the underlying data.

func (*Lexer) CopyN

func (l *Lexer) CopyN(n int) []byte

CopyN returns a copy of the next n bytes.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Error

func (l *Lexer) Error() error

Error returns an error if an error occurred reading from the buffer.

func (*Lexer) FinError

func (l *Lexer) FinError() error

FinError returns an error if an error occurred or if there is more data left to read in the buffer.

func (*Lexer) Read

func (l *Lexer) Read(p []byte) (int, error)

Read implements io.Reader.Read.

func (*Lexer) Read16

func (l *Lexer) Read16() uint16

Read16 reads a 16-bit value from the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Read32

func (l *Lexer) Read32() uint32

Read32 reads a 32-bit value from the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Read64

func (l *Lexer) Read64() uint64

Read64 reads a 64-bit value from the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Read8

func (l *Lexer) Read8() uint8

Read8 reads a byte from the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) ReadAll

func (l *Lexer) ReadAll() []byte

ReadAll Consumes and returns a copy of all remaining bytes in the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) ReadBytes

func (l *Lexer) ReadBytes(p []byte)

ReadBytes reads exactly len(p) values from the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) ReadData

func (l *Lexer) ReadData(data interface{})

ReadData reads the binary representation of data from the buffer.

See binary.Read.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Write

func (l *Lexer) Write(p []byte) (int, error)

Write implements io.Writer.Write.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Write16

func (l *Lexer) Write16(v uint16)

Write16 writes a 16-bit value to the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Write32

func (l *Lexer) Write32(v uint32)

Write32 writes a 32-bit value to the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Write64

func (l *Lexer) Write64(v uint64)

Write64 writes a 64-bit value to the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) Write8

func (l *Lexer) Write8(v uint8)

Write8 writes a byte to the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) WriteBytes

func (l *Lexer) WriteBytes(p []byte)

WriteBytes writes p to the Buffer.

If an error occurred, Error() will return a non-nil error.

func (*Lexer) WriteData

func (l *Lexer) WriteData(data interface{})

WriteData writes a binary representation of data to the buffer.

See binary.Write.

If an error occurred, Error() will return a non-nil error.

type LineWriter

type LineWriter interface {
	// OneLine is always called with exactly one line of output.
	OneLine(b []byte)
}

LineWriter processes one line of log output at a time.

type Marshaler

type Marshaler interface {
	Marshal(l *Lexer)
}

Marshaler is the interface implemented by an object that can marshal itself into binary form.

Marshal appends data to the buffer b.

type ReadAtCloser

type ReadAtCloser interface {
	io.ReaderAt
	io.Closer
}

ReadAtCloser is an io.ReaderAt and an io.Closer.

func NewLazyFile

func NewLazyFile(path string) ReadAtCloser

NewLazyFile returns a lazy ReaderAt opened from path.

func NewLazyOpenerAt

func NewLazyOpenerAt(open func() (io.ReaderAt, error)) ReadAtCloser

NewLazyOpenerAt returns a lazy io.ReaderAt based on `open`.

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(l *Lexer) error
}

Unmarshaler is the interface implemented by an object that can unmarshal a binary representation of itself.

Unmarshal Consumes data from the buffer b.

type WriteNameCloser

type WriteNameCloser interface {
	io.Writer
	io.Closer
	Name() string
}

WriteNameCloser is the interface that groups Write, Close, and Name methods.

var Discard WriteNameCloser = devNull(0)

Discard is a WriteNameCloser on which all Write and Close calls succeed without doing anything, and the Name call returns "null".

Jump to

Keyboard shortcuts

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