stream

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedRead = fmt.Errorf("unexpected read")

ErrUnexpectedRead is raised when reading negative length

Functions

func PutUint16

func PutUint16(buf []byte, offset int, value uint16)

PutUint16 reads 2 bytes from buf as uint16

func PutUint32

func PutUint32(buf []byte, offset int, value uint32)

PutUint32 puts uint32 into buf with offset

func PutUint64

func PutUint64(buf []byte, offset int, value uint64)

PutUint64 puts uint64 into buf with offset

func PutUvariantLittleEndian

func PutUvariantLittleEndian(buf []byte, x uint64) int

PutUvariantLittleEndian encodes a uint64 into buf and returns the number of bytes written. The default PutUvarint use big-endian

func ReadUint16

func ReadUint16(buf []byte, offset int) uint16

ReadUint16 reads 2 bytes from buf as uint16

func ReadUint32

func ReadUint32(buf []byte, offset int) uint32

ReadUint32 reads 4 bytes from buf as uint32

func ReadUint64

func ReadUint64(buf []byte, offset int) uint64

ReadUint64 reads 8 bytes from buf as uint64

func ReadUvarint

func ReadUvarint(buf []byte, offset int) (value uint64, readLen int, err error)

ReadUvarint reads an encoded unsigned integer from bytes.Reader and returns it as a uint64.

func UvariantSize

func UvariantSize(value uint64) int

UvariantSize returns the bytes-size of a uint64 uvariant encoded number.

func UvarintLittleEndian

func UvarintLittleEndian(buf []byte) (x uint64, s int)

UvarintLittleEndian decodes a uint64 from buf's tail with little endian and returns that value and the number of bytes read (> 0). If an error occurred, the value is 0 and the number of bytes n is <= 0 meaning:

n == 0: buf too small
n  < 0: value larger than 64 bits (overflow)
        and -n is the number of bytes read

func VariantSize

func VariantSize(value int64) int

VariantSize returns the bytes-size of a int64 variant encoded number.

Types

type BufferWriter

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

BufferWriter is a writer for writing data into a buffer

func NewBufferWriter

func NewBufferWriter(buffer *bytes.Buffer) *BufferWriter

NewBufferWriter creates a binary stream for writing with provided buffer(append write).

func (*BufferWriter) Bytes

func (bw *BufferWriter) Bytes() ([]byte, error)

Bytes returns memory buffer data, if error return err

func (*BufferWriter) Error

func (bw *BufferWriter) Error() error

Error returns the error of BufferWriter

func (*BufferWriter) Len

func (w *BufferWriter) Len() int

Len returns the size of bytes of the written data of the buffer;

func (*BufferWriter) PutByte

func (w *BufferWriter) PutByte(v byte)

PutByte encodes a byte into buf

func (*BufferWriter) PutBytes

func (w *BufferWriter) PutBytes(v []byte)

PutBytes encodes bytes into buf

func (*BufferWriter) PutInt16

func (w *BufferWriter) PutInt16(v int16)

PutInt16 encodes a int16 as 2 bytes into buf

func (*BufferWriter) PutInt32

func (w *BufferWriter) PutInt32(v int32)

PutInt32 encodes a int32 as 4 bytes into buf

func (*BufferWriter) PutInt64

func (w *BufferWriter) PutInt64(v int64)

PutInt64 encodes a int64 as 8 bytes into buf

func (*BufferWriter) PutUInt16

func (w *BufferWriter) PutUInt16(v uint16)

PutUInt16 encodes a uint16 as 2 bytes into buf

func (*BufferWriter) PutUint32

func (w *BufferWriter) PutUint32(v uint32)

PutUint32 encodes a uint32 as 4 bytes into buf

func (*BufferWriter) PutUint64

func (w *BufferWriter) PutUint64(v uint64)

PutUint64 encodes a uint64 as 8 bytes into buf

func (*BufferWriter) PutUvarint32

func (w *BufferWriter) PutUvarint32(v uint32)

PutUvarint32 encodes a uint32 into buf

func (*BufferWriter) PutUvarint64

func (w *BufferWriter) PutUvarint64(v uint64)

PutUvarint64 encodes a uint64 into buf

func (*BufferWriter) PutVarint32

func (w *BufferWriter) PutVarint32(v int32)

PutVarint32 encodes a int32 into buf

func (*BufferWriter) PutVarint64

func (w *BufferWriter) PutVarint64(v int64)

PutVarint64 encodes a int64 into buf

func (*BufferWriter) Reset

func (bw *BufferWriter) Reset()

Reset resets the underling buffer

func (*BufferWriter) SwitchBuffer

func (bw *BufferWriter) SwitchBuffer(newBuffer *bytes.Buffer)

SwitchBuffer switches to write a new buffer

func (*BufferWriter) Write

func (w *BufferWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

type Reader

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

Reader is a stream reader

func NewReader

func NewReader(data []byte) *Reader

NewReader reads data from binary stream

func (*Reader) Empty

func (r *Reader) Empty() bool

Empty reports whether the unread portion of the buffer is empty.

func (*Reader) Error

func (r *Reader) Error() error

Error return binary err

func (*Reader) Position

func (r *Reader) Position() int

Position returns the position where reader at

func (*Reader) ReadAt

func (r *Reader) ReadAt(pos int)

ReadAt moves the cursor to the specified position, this operation is a combination of SeekStart() + _ = ReadSlice(pos)

func (*Reader) ReadByte

func (r *Reader) ReadByte() byte

ReadByte reads 1 byte

func (*Reader) ReadBytes

func (r *Reader) ReadBytes(n int) []byte

ReadBytes reads n len bytes

func (*Reader) ReadInt16

func (r *Reader) ReadInt16() int16

ReadInt16 reads 2 bytes from buf as int16

func (*Reader) ReadInt32

func (r *Reader) ReadInt32() int32

ReadInt32 reads 4 bytes from buf as int32

func (*Reader) ReadInt64

func (r *Reader) ReadInt64() int64

ReadInt64 reads 8 bytes from buf as int64

func (*Reader) ReadSlice

func (r *Reader) ReadSlice(n int) []byte

ReadSlice returns a sub-slice. make sure that the sub-slice is not in use before calling Reset.

func (*Reader) ReadUint16

func (r *Reader) ReadUint16() uint16

ReadUint16 reads 2 bytes from buf as uint16

func (*Reader) ReadUint32

func (r *Reader) ReadUint32() uint32

ReadUint32 reads 4 bytes from buf as uint32

func (*Reader) ReadUint64

func (r *Reader) ReadUint64() uint64

ReadUint64 reads 8 bytes from buf as uint64

func (*Reader) ReadUntil

func (r *Reader) ReadUntil(c byte) []byte

ReadUntil reads a slice until a centain char.

func (*Reader) ReadUvarint32

func (r *Reader) ReadUvarint32() uint32

ReadUvarint32 reads uint32 from buffer

func (*Reader) ReadUvarint64

func (r *Reader) ReadUvarint64() uint64

ReadUvarint64 reads uint64 from buffer

func (*Reader) ReadVarint32

func (r *Reader) ReadVarint32() int32

ReadVarint32 reads int32 from buffer

func (*Reader) ReadVarint64

func (r *Reader) ReadVarint64() int64

ReadVarint64 reads int64 from buffer

func (*Reader) Reset

func (r *Reader) Reset(buf []byte)

Reset resets the Reader, then reads from the buffer

func (*Reader) SeekStart

func (r *Reader) SeekStart()

SeekStart seeks to the start of the underlying slice.

func (*Reader) UnreadSlice

func (r *Reader) UnreadSlice() []byte

UnreadSlice returns the unread sub-slice

type SliceWriter

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

SliceWriter is a writer for writing data into a slice

func NewSliceWriter

func NewSliceWriter(buffer []byte) *SliceWriter

NewSliceWriter creates a binary stream for writing with provided slice(writing start from offset 0). The caller is responsible to make sure the wrote bytes do not exceed the buffer size, otherwise the exceeding data will be missing. and if it is a large buf, buffer grow will cause a expensive reallocation

func (*SliceWriter) Bytes

func (sw *SliceWriter) Bytes() ([]byte, error)

Bytes returns memory buffer data, if error return err

func (*SliceWriter) Error

func (sw *SliceWriter) Error() error

Error returns the error of BufferWriter

func (*SliceWriter) Len

func (w *SliceWriter) Len() int

Len returns the size of bytes of the written data of the buffer;

func (*SliceWriter) PutByte

func (w *SliceWriter) PutByte(v byte)

PutByte encodes a byte into buf

func (*SliceWriter) PutBytes

func (w *SliceWriter) PutBytes(v []byte)

PutBytes encodes bytes into buf

func (*SliceWriter) PutInt16

func (w *SliceWriter) PutInt16(v int16)

PutInt16 encodes a int16 as 2 bytes into buf

func (*SliceWriter) PutInt32

func (w *SliceWriter) PutInt32(v int32)

PutInt32 encodes a int32 as 4 bytes into buf

func (*SliceWriter) PutInt64

func (w *SliceWriter) PutInt64(v int64)

PutInt64 encodes a int64 as 8 bytes into buf

func (*SliceWriter) PutUInt16

func (w *SliceWriter) PutUInt16(v uint16)

PutUInt16 encodes a uint16 as 2 bytes into buf

func (*SliceWriter) PutUint32

func (w *SliceWriter) PutUint32(v uint32)

PutUint32 encodes a uint32 as 4 bytes into buf

func (*SliceWriter) PutUint64

func (w *SliceWriter) PutUint64(v uint64)

PutUint64 encodes a uint64 as 8 bytes into buf

func (*SliceWriter) PutUvarint32

func (w *SliceWriter) PutUvarint32(v uint32)

PutUvarint32 encodes a uint32 into buf

func (*SliceWriter) PutUvarint64

func (w *SliceWriter) PutUvarint64(v uint64)

PutUvarint64 encodes a uint64 into buf

func (*SliceWriter) PutVarint32

func (w *SliceWriter) PutVarint32(v int32)

PutVarint32 encodes a int32 into buf

func (*SliceWriter) PutVarint64

func (w *SliceWriter) PutVarint64(v int64)

PutVarint64 encodes a int64 into buf

func (*SliceWriter) Write

func (w *SliceWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

Jump to

Keyboard shortcuts

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