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 ¶
- Variables
- func FromBigEndian(obj Unmarshaler, b []byte) error
- func FromBytes(obj Unmarshaler, b []byte, order binary.ByteOrder) error
- func FromLittleEndian(obj Unmarshaler, b []byte) error
- func FullLineWriter(w LineWriter) io.WriteCloser
- func ReadAll(r io.ReaderAt) ([]byte, error)
- func ReadIntoFile(r io.Reader, p string) error
- func ReadOneByte(r io.ReaderAt) error
- func Reader(r io.ReaderAt) io.Reader
- func ReaderAtEqual(r1, r2 io.ReaderAt) bool
- func ToBigEndian(m Marshaler) []byte
- func ToBytes(m Marshaler, order binary.ByteOrder) []byte
- func ToLittleEndian(m Marshaler) []byte
- type AlignReader
- type AlignWriter
- type ArchiveReader
- type Buffer
- type CachingReader
- type LazyOpener
- type LazyOpenerAt
- type Lexer
- func (l *Lexer) Align(n int)
- func (l *Lexer) Append(n int) []byte
- func (l *Lexer) Consume(n int) []byte
- func (l *Lexer) CopyN(n int) []byte
- func (l *Lexer) Error() error
- func (l *Lexer) FinError() error
- func (l *Lexer) Read(p []byte) (int, error)
- func (l *Lexer) Read16() uint16
- func (l *Lexer) Read32() uint32
- func (l *Lexer) Read64() uint64
- func (l *Lexer) Read8() uint8
- func (l *Lexer) ReadAll() []byte
- func (l *Lexer) ReadBytes(p []byte)
- func (l *Lexer) ReadData(data interface{})
- func (l *Lexer) Write(p []byte) (int, error)
- func (l *Lexer) Write16(v uint16)
- func (l *Lexer) Write32(v uint32)
- func (l *Lexer) Write64(v uint64)
- func (l *Lexer) Write8(v uint8)
- func (l *Lexer) WriteBytes(p []byte)
- func (l *Lexer) WriteData(data interface{})
- type LineWriter
- type Marshaler
- type ProgressReadCloser
- type Unmarshaler
- type WriteNameCloser
Constants ¶
This section is empty.
Variables ¶
var ErrPreReadError = errors.New("pre-read nothing")
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 ReadAll ¶
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 ReadIntoFile ¶
ReadIntoFile reads all from io.Reader into the file at given path.
If the file at given path does not exist, a new file will be created. If the file exists at the given path, but not empty, it will be truncated.
func ReadOneByte ¶ added in v0.12.0
ReadOneByte reads one byte from given io.ReaderAt.
func ReaderAtEqual ¶
ReaderAtEqual compares the contents of r1 and r2.
func ToBigEndian ¶
ToBigEndian marshals m to big endian byte order.
func ToLittleEndian ¶
ToLittleEndian marshals m to little endian byte order.
Types ¶
type AlignReader ¶
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 ¶
AlignWriter keeps track of how many bytes were written so the writer can be aligned at a future time.
type ArchiveReader ¶ added in v0.8.0
type ArchiveReader struct {
// contains filtered or unexported fields
}
ArchiveReader reads from a io.Reader, decompresses source bytes when applicable.
It allows probing for multiple archive format, while still able to read from beginning, by pre-reading a small number of bytes.
Always use newArchiveReader to initialize.
func NewArchiveReader ¶ added in v0.8.0
func NewArchiveReader(r io.Reader) (ArchiveReader, error)
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer implements functions to manipulate byte slices in a zero-copy way.
func (*Buffer) Preallocate ¶
Preallocate increases the capacity of the buffer by n 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.
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 NewLazyOpener ¶
func NewLazyOpener(filename string, open func() (io.Reader, error)) *LazyOpener
NewLazyOpener returns a lazy io.Reader based on `open`.
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.
func (*LazyOpener) String ¶ added in v0.12.0
func (lr *LazyOpener) String() string
String implements fmt.Stringer.
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 ¶
func NewLazyFile(path string) *LazyOpenerAt
NewLazyFile returns a lazy ReaderAt opened from path.
func NewLazyLimitFile ¶ added in v0.12.0
func NewLazyLimitFile(path string, limit int64) *LazyOpenerAt
NewLazyLimitFile returns a lazy ReaderAt opened from path with a limit reader on it.
func NewLazyLimitOpenerAt ¶ added in v0.12.0
func NewLazyLimitOpenerAt(filename string, limit int64, open func() (io.ReaderAt, error)) *LazyOpenerAt
NewLazyLimitOpenerAt returns a lazy io.ReaderAt based on `open`.
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) File ¶ added in v0.12.0
func (loa *LazyOpenerAt) File() *os.File
File returns the backend file of the io.ReaderAt if it is backed by a os.File.
func (*LazyOpenerAt) ReadAt ¶
func (loa *LazyOpenerAt) ReadAt(p []byte, off int64) (int, error)
ReadAt implements io.ReaderAt.ReadAt.
func (*LazyOpenerAt) String ¶
func (loa *LazyOpenerAt) String() string
String implements fmt.Stringer.
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 ¶
NewBigEndianBuffer returns a new big endian coder for a new buffer.
func NewLittleEndianBuffer ¶
NewLittleEndianBuffer returns a new little endian coder for a new buffer.
func NewNativeEndianBuffer ¶
NewNativeEndianBuffer returns a new native endian coder for a new buffer.
func (*Lexer) Append ¶
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 ¶
Consume returns a slice of the next n bytes from the buffer.
Consume gives direct access to the underlying data.
func (*Lexer) CopyN ¶
CopyN returns a copy of the next n bytes.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) FinError ¶
FinError returns an error if an error occurred or if there is more data left to read in the buffer.
func (*Lexer) Read16 ¶
Read16 reads a 16-bit value from the Buffer.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) Read32 ¶
Read32 reads a 32-bit value from the Buffer.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) Read64 ¶
Read64 reads a 64-bit value from the Buffer.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) Read8 ¶
Read8 reads a byte from the Buffer.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) ReadAll ¶
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 ¶
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 ¶
Write implements io.Writer.Write.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) Write16 ¶
Write16 writes a 16-bit value to the Buffer.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) Write32 ¶
Write32 writes a 32-bit value to the Buffer.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) Write64 ¶
Write64 writes a 64-bit value to the Buffer.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) Write8 ¶
Write8 writes a byte to the Buffer.
If an error occurred, Error() will return a non-nil error.
func (*Lexer) WriteBytes ¶
WriteBytes writes p to the Buffer.
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 ProgressReadCloser ¶
type ProgressReadCloser struct { RC io.ReadCloser Symbol string Interval int W io.Writer // contains filtered or unexported fields }
ProgressReadCloser implements io.ReadCloser and prints Symbol to W after every Interval bytes passes through RC.
func (*ProgressReadCloser) Close ¶
func (rc *ProgressReadCloser) Close() error
Read implements io.Closer for ProgressReader.
type Unmarshaler ¶
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 ¶
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".