io

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 4 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsSerializable

func AsSerializable(se ISerializable, data []byte) error

func ToArray

func ToArray(p ISerializable) ([]byte, error)

Types

type BinaryReader

type BinaryReader struct {
	Err error // errs []error // new design, put all errors in the array
	// contains filtered or unexported fields
}

BinaryReader is a convenient wrapper around an io.Reader and error object. Used to simplify error handling when reading into a struct with many fields.

func NewBinaryReaderFromBuf

func NewBinaryReaderFromBuf(b []byte) *BinaryReader

NewBinaryReaderFromBuf makes a BinaryReader from byte buffer.

func NewBinaryReaderFromIO

func NewBinaryReaderFromIO(ior io.Reader) *BinaryReader

NewBinaryReaderFromIO makes a BinaryReader from io.Reader.

func (*BinaryReader) ReadAllBytes

func (br *BinaryReader) ReadAllBytes() []byte

func (*BinaryReader) ReadBE

func (br *BinaryReader) ReadBE(v interface{})

ReadBE reads from the underlying io.Reader into the interface v in big-endian format.

func (*BinaryReader) ReadBytesWithGrouping

func (br *BinaryReader) ReadBytesWithGrouping() ([]byte, error)

ReadBytesWithGrouping ...

func (*BinaryReader) ReadLE

func (br *BinaryReader) ReadLE(v interface{})

ReadLE reads from the underlying io.Reader into the interface v in little-endian format.

func (*BinaryReader) ReadOneByte added in v1.2.0

func (br *BinaryReader) ReadOneByte() byte

ReadOneByte reads exactly one byte from the underlying io.Reader

func (*BinaryReader) ReadUInt64Bytes

func (br *BinaryReader) ReadUInt64Bytes() []byte

ReadUInt64Bytes reads from the underlying io.Reader into the interface v in little-endian format

func (*BinaryReader) ReadVarBytes

func (br *BinaryReader) ReadVarBytes() []byte

ReadVarBytes reads the next set of bytes from the underlying reader.

func (*BinaryReader) ReadVarBytesWithMaxLimit

func (br *BinaryReader) ReadVarBytesWithMaxLimit(max int) []byte

ReadVarBytesWithMaxLimit reads the next set of bytes from the underlying reader. ReadVarUInt() is used to determine how large that slice is length should not exceed 0x1000000 = 16,777,216

func (*BinaryReader) ReadVarString

func (br *BinaryReader) ReadVarString(max int) string

ReadVarString calls ReadVarBytes and casts the results as a string. "max" should not exceed 0x1000000 = 16,777,216

func (*BinaryReader) ReadVarUInt

func (br *BinaryReader) ReadVarUInt() uint64

func (*BinaryReader) ReadVarUIntWithMaxLimit

func (br *BinaryReader) ReadVarUIntWithMaxLimit(max uint64) uint64

ReadVarUIntWithMaxLimit reads a variable-length-encoded integer from the underlying reader. The result should not exceed the max value of uint64

type BinaryWriter

type BinaryWriter struct {
	Err error
	// contains filtered or unexported fields
}

BinaryWriter is a convenient wrapper around a io.Writer and err object. Used to simplify error handling when writing into a io.Writer from a struct with many fields.

func NewBinaryWriterFromIO

func NewBinaryWriterFromIO(iow io.Writer) *BinaryWriter

NewBinaryWriterFromIO makes a BinaryWriter from io.Writer.

func (*BinaryWriter) WriteBE

func (w *BinaryWriter) WriteBE(v interface{})

WriteBE writes into the underlying io.Writer from an object v in big-endian format.

func (*BinaryWriter) WriteBytesWithGrouping

func (writer *BinaryWriter) WriteBytesWithGrouping(value []byte)

WriteBytesWithGrouping ...

func (*BinaryWriter) WriteLE

func (w *BinaryWriter) WriteLE(v interface{})

WriteLE writes into the underlying io.Writer from an object v in little-endian format.

func (*BinaryWriter) WriteVarBytes

func (w *BinaryWriter) WriteVarBytes(b []byte)

WriteBytes writes a variable length byte array into the underlying io.Writer.

func (*BinaryWriter) WriteVarString

func (w *BinaryWriter) WriteVarString(s string)

WriteVarString writes a variable length string into the underlying io.Writer.

func (*BinaryWriter) WriteVarUInt

func (w *BinaryWriter) WriteVarUInt(val uint64)

WriteVarUint writes a uint64 into the underlying writer using variable-length encoding.

type BufBinaryWriter

type BufBinaryWriter struct {
	*BinaryWriter
	// contains filtered or unexported fields
}

BufBinaryWriter is an additional layer on top of BinaryWriter that automatically creates buffer to write into that you can get after all writes via Bytes().

func NewBufBinaryWriter

func NewBufBinaryWriter() *BufBinaryWriter

NewBufBinaryWriter makes a BufBinaryWriter with an empty byte buffer.

func (*BufBinaryWriter) Bytes

func (bw *BufBinaryWriter) Bytes() []byte

Bytes returns resulting buffer and reset to prevent future writes

func (*BufBinaryWriter) Reset

func (bw *BufBinaryWriter) Reset()

Reset resets the state of the buffer, making it usable again. It can make buffer usage somewhat more efficient, because you don't need to create it again, but beware that the buffer is gonna be the same as the one returned by Bytes(), so if you need that data after Reset() you have to copy it yourself.

type ISerializable

type ISerializable interface {
	Deserialize(*BinaryReader)
	Serialize(*BinaryWriter)
}

ISerializable defines the binary encoding/decoding interface. Errors are returned via BinaryReader/BinaryWriter Err field. These functions must have safe behavior when passed BinaryReader/BinaryWriter with Err already set. Invocations to these functions tend to be nested, with this mechanism only the top-level caller should handle the error once and all the other code should just not panic in presence of error.

Jump to

Keyboard shortcuts

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