uio

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2020 License: BSD-3-Clause Imports: 11 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 added in v0.4.0

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

FromBigEndian unmarshals b into obj in big endian byte order.

func FromBytes added in v0.4.0

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

FromBytes unmarshals b into obj in the given byte order.

func FromLittleEndian added in v0.4.0

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

FromLittleEndian unmarshals b into obj in little endian byte order.

func FullLineWriter added in v0.6.0

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 added in v0.6.0

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 added in v0.4.0

func ReaderAtEqual(r1, r2 io.ReaderAt) bool

ReaderAtEqual compares the contents of r1 and r2.

func ToBigEndian added in v0.4.0

func ToBigEndian(m Marshaler) []byte

ToBigEndian marshals m to big endian byte order.

func ToBytes added in v0.4.0

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

ToBytes marshals m in the given byte order.

func ToLittleEndian added in v0.4.0

func ToLittleEndian(m Marshaler) []byte

ToLittleEndian marshals m to little endian byte order.

Types

type AlignReader added in v0.5.0

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 added in v0.5.0

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 added in v0.5.0

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

Read reads from the underlying io.Reader.

func (*AlignReader) ReadByte added in v0.5.0

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

ReadByte reads one byte from the underlying io.Reader.

type AlignWriter added in v0.5.0

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 added in v0.5.0

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 added in v0.5.0

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

Write writes to the underlying io.Writew.

type Buffer added in v0.4.0

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

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

func NewBuffer added in v0.4.0

func NewBuffer(b []byte) *Buffer

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

func (*Buffer) Cap added in v0.4.0

func (b *Buffer) Cap() int

Cap returns the available capacity.

func (*Buffer) Data added in v0.4.0

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

Data is unConsumed data remaining in the Buffer.

func (*Buffer) Has added in v0.4.0

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

Has returns true if n bytes are available.

func (*Buffer) Len added in v0.4.0

func (b *Buffer) Len() int

Len returns the length of the remaining bytes.

func (*Buffer) Preallocate added in v0.4.0

func (b *Buffer) Preallocate(n int)

Preallocate increases the capacity of the buffer by n bytes.

func (*Buffer) ReadN added in v0.4.0

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

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

func (*Buffer) WriteN added in v0.4.0

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 NewLazyFile added in v0.5.0

func NewLazyFile(path string) *LazyOpenerAt

NewLazyFile returns a lazy ReaderAt opened from path.

func NewLazyOpenerAt

func NewLazyOpenerAt(filename string, open func() (io.ReaderAt, error)) *LazyOpenerAt

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

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.

func (*LazyOpenerAt) String added in v0.7.0

func (loa *LazyOpenerAt) String() string

String implements fmt.Stringer.

type Lexer added in v0.4.0

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 added in v0.4.0

func NewBigEndianBuffer(b []byte) *Lexer

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

func NewLexer added in v0.4.0

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

NewLexer returns a new coder for buffers.

func NewLittleEndianBuffer added in v0.4.0

func NewLittleEndianBuffer(b []byte) *Lexer

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

func NewNativeEndianBuffer added in v0.4.0

func NewNativeEndianBuffer(b []byte) *Lexer

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

func (*Lexer) Align added in v0.6.0

func (l *Lexer) Align(n int)

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

func (*Lexer) Append added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

func (l *Lexer) Error() error

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

func (*Lexer) FinError added in v0.4.0

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 added in v0.4.0

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

Read implements io.Reader.Read.

func (*Lexer) Read16 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.4.0

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 added in v0.6.0

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 added in v0.4.0

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 ProgressReader added in v0.7.0

type ProgressReader struct {
	R io.Reader

	Symbol   string
	Interval int
	W        io.Writer
	// contains filtered or unexported fields
}

ProgressReader implements io.Reader and prints Symbol to W after every Interval bytes passes through R.

func (*ProgressReader) Read added in v0.7.0

func (r *ProgressReader) Read(p []byte) (n int, err error)

Read implements io.Reader for ProgressReader.

type Unmarshaler added in v0.4.0

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 added in v0.6.0

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".

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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