io

package
v0.0.0-...-f94ef0f Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const MaxArraySize = 0x1000000

Variables

View Source
var ErrDrained = errors.New("buffer already drained")

ErrDrained is returned on an attempt to use already drained write buffer.

Functions

func FromByteArray

func FromByteArray(s Serializable, data []byte) error

func GetVarSize

func GetVarSize(value interface{}) int

func MakeDirForFile

func MakeDirForFile(filePath string, creator string) error

MakeDirForFile creates directory provided in filePath.

func PutVarUint

func PutVarUint(data []byte, val uint64) int

PutVarUint puts val in varint form to the pre-allocated buffer.

func ToByteArray

func ToByteArray(s Serializable) ([]byte, error)

Types

type BinReader

type BinReader struct {
	io.Reader

	Err error
	// contains filtered or unexported fields
}

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

func NewBinReaderFromBuf

func NewBinReaderFromBuf(b []byte) *BinReader

NewBinReaderFromBuf makes a BinReader from byte buffer.

func NewBinReaderFromIO

func NewBinReaderFromIO(ior io.Reader) *BinReader

NewBinReaderFromIO makes a BinReader from io.Reader.

func (*BinReader) Len

func (r *BinReader) Len() int

Len returns the number of bytes of the unread portion of the buffer if reading from bytes.Reader or -1 otherwise.

func (*BinReader) ReadArray

func (r *BinReader) ReadArray(t interface{}, maxSize ...int)

ReadArray reads array into value which must be a pointer to a slice.

func (*BinReader) ReadB

func (r *BinReader) ReadB() byte

ReadB reads a byte from the underlying io.Reader. On read failures it returns zero.

func (*BinReader) ReadBool

func (r *BinReader) ReadBool() bool

ReadBool reads a boolean value encoded in a zero/non-zero byte from the underlying io.Reader. On read failures it returns false.

func (*BinReader) ReadByte

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

ReadByte to impl rlp.ByteReader

func (*BinReader) ReadBytes

func (r *BinReader) ReadBytes(buf []byte)

ReadBytes copies fixed-size buffer from the reader to provided slice.

func (*BinReader) ReadString

func (r *BinReader) ReadString(maxSize ...int) string

ReadString calls ReadVarBytes and casts the results as a string.

func (*BinReader) ReadU16BE

func (r *BinReader) ReadU16BE() uint16

ReadU16BE reads a big-endian encoded uint16 value from the underlying io.Reader. On read failures it returns zero.

func (*BinReader) ReadU16LE

func (r *BinReader) ReadU16LE() uint16

ReadU16LE reads a little-endian encoded uint16 value from the underlying io.Reader. On read failures it returns zero.

func (*BinReader) ReadU32LE

func (r *BinReader) ReadU32LE() uint32

ReadU32LE reads a little-endian encoded uint32 value from the underlying io.Reader. On read failures it returns zero.

func (*BinReader) ReadU64LE

func (r *BinReader) ReadU64LE() uint64

ReadU64LE reads a little-endian encoded uint64 value from the underlying io.Reader. On read failures it returns zero.

func (*BinReader) ReadVarBytes

func (r *BinReader) ReadVarBytes(maxSize ...int) []byte

ReadVarBytes reads the next set of bytes from the underlying reader. ReadVarUInt() is used to determine how large that slice is.

func (*BinReader) ReadVarUint

func (r *BinReader) ReadVarUint() uint64

ReadVarUint reads a variable-length-encoded integer from the underlying reader.

type BinWriter

type BinWriter struct {
	io.Writer
	Err error
	// contains filtered or unexported fields
}

BinWriter 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 NewBinWriterFromIO

func NewBinWriterFromIO(iow io.Writer) *BinWriter

NewBinWriterFromIO makes a BinWriter from io.Writer.

func (*BinWriter) Grow

func (w *BinWriter) Grow(n int)

Grow tries to increase underlying buffer capacity so that at least n bytes can be written without reallocation. If the writer is not a buffer, this is a no-op.

func (*BinWriter) WriteArray

func (w *BinWriter) WriteArray(arr interface{})

WriteArray writes a slice or an array arr into w. Note that nil slices and empty slices are gonna be treated the same resulting in equal zero-length array encoded.

func (*BinWriter) WriteB

func (w *BinWriter) WriteB(u8 byte)

WriteB writes a byte into the underlying io.Writer.

func (*BinWriter) WriteBool

func (w *BinWriter) WriteBool(b bool)

WriteBool writes a boolean value into the underlying io.Writer encoded as a byte with values of 0 or 1.

func (*BinWriter) WriteBytes

func (w *BinWriter) WriteBytes(b []byte)

WriteBytes writes a variable byte into the underlying io.Writer without prefix.

func (*BinWriter) WriteString

func (w *BinWriter) WriteString(s string)

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

func (*BinWriter) WriteU16BE

func (w *BinWriter) WriteU16BE(u16 uint16)

WriteU16BE writes an uint16 value into the underlying io.Writer in big-endian format.

func (*BinWriter) WriteU16LE

func (w *BinWriter) WriteU16LE(u16 uint16)

WriteU16LE writes an uint16 value into the underlying io.Writer in little-endian format.

func (*BinWriter) WriteU32LE

func (w *BinWriter) WriteU32LE(u32 uint32)

WriteU32LE writes an uint32 value into the underlying io.Writer in little-endian format.

func (*BinWriter) WriteU64LE

func (w *BinWriter) WriteU64LE(u64 uint64)

WriteU64LE writes an uint64 value into the underlying io.Writer in little-endian format.

func (*BinWriter) WriteVarBytes

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

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

func (*BinWriter) WriteVarUint

func (w *BinWriter) WriteVarUint(val uint64)

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

type BufBinWriter

type BufBinWriter struct {
	*BinWriter
	// contains filtered or unexported fields
}

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

func NewBufBinWriter

func NewBufBinWriter() *BufBinWriter

NewBufBinWriter makes a BufBinWriter with an empty byte buffer.

func (*BufBinWriter) Bytes

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

Bytes returns resulting buffer and makes future writes return an error.

func (*BufBinWriter) Len

func (bw *BufBinWriter) Len() int

Len returns the number of bytes of the unread portion of the buffer.

func (*BufBinWriter) Reset

func (bw *BufBinWriter) 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 Serializable

type Serializable interface {
	DecodeBinary(*BinReader)
	EncodeBinary(*BinWriter)
}

Jump to

Keyboard shortcuts

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