iostream

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2021 License: MIT Imports: 9 Imported by: 5

README

kelindar/iostream
Go Version PkgGoDev Go Report Card License Coverage

Simple Binary Stream Reader/Writer

This package contains a set of simple utility reader and writer that can be used to efficiently read/write binary information over the wire.

import "github.com/kelindar/iostream"

Usage

In order to use it, you can simply instantiate either a Reader or a Writer which require an io.Reader or io.Writer respectively. Here's a simple example

// Fake stream
stream := bytes.NewBuffer(nil)

// Write some data into the stream...
w := iostream.NewWriter(stream)
w.WriteString("Roman")
w.WriteUint32(36)

// Read the data back...
r := iostream.NewReader(stream)
name, err := r.ReadString()
age, err  := r.ReadUint32()

Contributing

We are open to contributions, feel free to submit a pull request and we'll review it as quickly as we can. This library is maintained by Roman Atachiants

License

Tile is licensed under the MIT License.

Documentation

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 represents a stream reader.

func NewReader

func NewReader(src io.Reader) *Reader

NewReader creates a stream reader.

func (*Reader) Offset added in v1.1.0

func (r *Reader) Offset() int64

Offset returns the number of bytes read through this reader.

func (*Reader) Read added in v1.0.1

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

Read implements io.Reader interface by simply calling the Read method on the underlying stream.

func (*Reader) ReadBinary

func (r *Reader) ReadBinary(v encoding.BinaryUnmarshaler) error

ReadBinary reads the bytes from the stream and unmarshals it into the destination interface using UnmarshalBinary() function.

func (*Reader) ReadBool

func (r *Reader) ReadBool() (bool, error)

ReadBool reads a single boolean value from the slice.

func (*Reader) ReadBytes

func (r *Reader) ReadBytes() (out []byte, err error)

ReadBytes a byte string prefixed with a variable-size integer size.

func (*Reader) ReadFloat32

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

ReadFloat32 reads a float32

func (*Reader) ReadFloat64

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

ReadFloat64 reads a float64

func (*Reader) ReadInt

func (r *Reader) ReadInt() (int, error)

ReadUint reads an int

func (*Reader) ReadInt16

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

ReadInt16 reads an int16

func (*Reader) ReadInt32

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

ReadInt32 reads an int32

func (*Reader) ReadInt64

func (r *Reader) ReadInt64() (out int64, err error)

ReadInt64 reads an int64

func (*Reader) ReadInt8

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

ReadInt8 reads an int8

func (*Reader) ReadRange added in v1.2.1

func (r *Reader) ReadRange(fn func(i int, r *Reader) error) error

ReadRange reads the length of the array from the underlying stream and calls a callback function on each element of that array.

func (*Reader) ReadSelf added in v1.2.0

func (r *Reader) ReadSelf(v io.ReaderFrom) error

ReadSelf uses the provider io.ReaderFrom in order to read the data from the source reader.

func (*Reader) ReadString

func (r *Reader) ReadString() (out string, err error)

ReadString a string prefixed with a variable-size integer size.

func (*Reader) ReadText

func (r *Reader) ReadText(v encoding.TextUnmarshaler) error

ReadText reads the bytes from the stream and unmarshals it into the destination interface using UnmarshalText() function.

func (*Reader) ReadUint

func (r *Reader) ReadUint() (uint, error)

ReadUint reads a uint

func (*Reader) ReadUint16

func (r *Reader) ReadUint16() (out uint16, err error)

ReadUint16 reads a uint16

func (*Reader) ReadUint32

func (r *Reader) ReadUint32() (out uint32, err error)

ReadUint32 reads a uint32

func (*Reader) ReadUint64

func (r *Reader) ReadUint64() (out uint64, err error)

ReadUint64 reads a uint64

func (*Reader) ReadUint8

func (r *Reader) ReadUint8() (out uint8, err error)

ReadUint8 reads a uint8

func (*Reader) ReadUvarint

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

ReadUvarint reads a variable-length Uint64 from the buffer.

func (*Reader) ReadVarint

func (r *Reader) ReadVarint() (int64, error)

ReadVarint reads a variable-length Int64 from the buffer.

type Writer

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

Writer represents a stream writer.

func NewWriter

func NewWriter(out io.Writer) *Writer

NewWriter creates a new stream writer.

func (*Writer) Close added in v1.2.0

func (w *Writer) Close() error

Close closes the writer's underlying stream and return its error. If the underlying io.Writer is not an io.Closer, it's a no-op.

func (*Writer) Flush added in v1.3.0

func (w *Writer) Flush() error

Flush flushes the writer to the underlying stream and returns its error. If the underlying io.Writer does not have a Flush() error method, it's a no-op.

func (*Writer) Offset added in v1.1.0

func (w *Writer) Offset() int64

Offset returns the number of bytes written through this writer.

func (*Writer) Reset

func (w *Writer) Reset(out io.Writer)

Reset resets the writer and makes it ready to be reused.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write implements io.Writer interface by simply writing into the underlying souurce.

func (*Writer) WriteBinary

func (w *Writer) WriteBinary(v encoding.BinaryMarshaler) error

WriteBinary marshals the type to its binary representation and writes it downstream, prefixed with its size as a variable-size integer.

func (*Writer) WriteBool

func (w *Writer) WriteBool(v bool) error

WriteBool writes a single boolean value into the buffer

func (*Writer) WriteBytes

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

WriteBytes writes a byte slice prefixed with a variable-size integer.

func (*Writer) WriteFloat32

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

WriteFloat32 a 32-bit floating point number

func (*Writer) WriteFloat64

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

WriteFloat64 a 64-bit floating point number

func (*Writer) WriteInt

func (w *Writer) WriteInt(v int) error

WriteInt writes an int

func (*Writer) WriteInt16

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

WriteInt16 writes an int16

func (*Writer) WriteInt32

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

WriteInt32 writes an int32

func (*Writer) WriteInt64

func (w *Writer) WriteInt64(v int64) error

WriteInt64 writes an int64

func (*Writer) WriteInt8

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

WriteInt8 writes an int8

func (*Writer) WriteRange added in v1.2.1

func (w *Writer) WriteRange(length int, fn func(i int, w *Writer) error) error

WriteRange writes a specified length of an array and for each element of that array calls the callback function with its index.

func (*Writer) WriteSelf added in v1.2.0

func (w *Writer) WriteSelf(v io.WriterTo) error

WriteSelf uses the provider io.WriterTo in order to write the data into the destination writer.

func (*Writer) WriteString

func (w *Writer) WriteString(v string) error

WriteString writes a string prefixed with a variable-size integer.

func (*Writer) WriteText

func (w *Writer) WriteText(v encoding.TextMarshaler) error

WriteText marshals the type to its text representation and writes it downstream, prefixed with its size as a variable-size integer.

func (*Writer) WriteUint

func (w *Writer) WriteUint(v uint) error

WriteUint writes a Uint

func (*Writer) WriteUint16

func (w *Writer) WriteUint16(v uint16) error

WriteUint16 writes a Uint16

func (*Writer) WriteUint32

func (w *Writer) WriteUint32(v uint32) error

WriteUint32 writes a Uint32

func (*Writer) WriteUint64

func (w *Writer) WriteUint64(v uint64) error

WriteUint64 writes a Uint64

func (*Writer) WriteUint8

func (w *Writer) WriteUint8(v uint8) error

WriteUint8 writes a Uint8

func (*Writer) WriteUvarint

func (w *Writer) WriteUvarint(x uint64) error

WriteUvarint writes a variable size unsigned integer

func (*Writer) WriteVarint

func (w *Writer) WriteVarint(v int64) error

WriteVarint writes a variable size signed integer

Jump to

Keyboard shortcuts

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