buffer

package
v5.0.0-...-72ea290 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package buffer implement methods for efficiently writing and reading values to and from io.Writer and io.Reader that also expose their internal buffers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EqualAsUint16Slice

func EqualAsUint16Slice[T any](a, b []T) bool

EqualAsUint16Slice casts &[]T into *[]uint16 and performs a comparison. User must ensure that T can be stored in an uint16.

func EqualAsUint32Slice

func EqualAsUint32Slice[T any](a, b []T) bool

EqualAsUint32Slice casts &[]T into *[]uint32 and performs a comparison. User must ensure that T can be stored in an uint32.

func EqualAsUint64

func EqualAsUint64[T any](a, b T) bool

EqualAsUint64 casts &T to an *uint64 and performs a comparison. User must ensure that T can be stored in an uint64.

func EqualAsUint64Slice

func EqualAsUint64Slice[T any](a, b []T) bool

EqualAsUint64Slice casts &[]T into *[]uint64 and performs a comparison. User must ensure that T can be stored in an uint64.

func EqualAsUint8Slice

func EqualAsUint8Slice[T any](a, b []T) bool

EqualAsUint8Slice casts &[]T into *[]uint8 and performs a comparison. User must ensure that T can be stored in an uint8.

func Read

func Read(r Reader, c []byte) (n int64, err error)

Read reads a slice of bytes from r and copies it on c.

func ReadAsUint16

func ReadAsUint16[T any](r Reader, c *T) (n int64, err error)

ReadAsUint16 reads an uint16 from r and stores the result into c with pointer type casting into type T.

func ReadAsUint16Slice

func ReadAsUint16Slice[T any](r Reader, c []T) (n int64, err error)

ReadAsUint16Slice reads a slice of uint16 from r and stores the result into c with pointer type casting into type T.

func ReadAsUint32

func ReadAsUint32[T any](r Reader, c *T) (n int64, err error)

ReadAsUint32 reads an uint32 from r and stores the result into c with pointer type casting into type T.

func ReadAsUint32Slice

func ReadAsUint32Slice[T any](r Reader, c []T) (n int64, err error)

ReadAsUint32Slice reads a slice of uint32 from r and stores the result into c with pointer type casting into type T.

func ReadAsUint64

func ReadAsUint64[T any](r Reader, c *T) (n int64, err error)

ReadAsUint64 reads an uint64 from r and stores the result into c with pointer type casting into type T.

func ReadAsUint64Slice

func ReadAsUint64Slice[T any](r Reader, c []T) (n int64, err error)

ReadAsUint64Slice reads a slice of uint64 from r and stores the result into c with pointer type casting into type T.

func ReadAsUint8

func ReadAsUint8[T any](r Reader, c *T) (n int64, err error)

ReadAsUint8 reads an uint8 from r and stores the result into c with pointer type casting into type T.

func ReadAsUint8Slice

func ReadAsUint8Slice[T any](r Reader, c []T) (n int64, err error)

ReadAsUint8Slice reads a slice of uint8 from r and stores the result into c with pointer type casting into type T.

func ReadUint16

func ReadUint16(r Reader, c *uint16) (n int64, err error)

ReadUint16 reads a uint16 from r and stores the result into *c.

func ReadUint16Slice

func ReadUint16Slice(r Reader, c []uint16) (n int64, err error)

ReadUint16Slice reads a slice of uint16 from r and stores the result into c.

func ReadUint32

func ReadUint32(r Reader, c *uint32) (n int64, err error)

ReadUint32 reads a uint32 from r and stores the result into *c.

func ReadUint32Slice

func ReadUint32Slice(r Reader, c []uint32) (n int64, err error)

ReadUint32Slice reads a slice of uint32 from r and stores the result into c.

func ReadUint64

func ReadUint64(r Reader, c *uint64) (n int64, err error)

ReadUint64 reads a uint64 from r and stores the result into c.

func ReadUint64Slice

func ReadUint64Slice(r Reader, c []uint64) (n int64, err error)

ReadUint64Slice reads a slice of uint64 from r and stores the result into c.

func ReadUint8

func ReadUint8(r Reader, c *uint8) (n int64, err error)

ReadUint8 reads a byte from r and stores the result into *c.

func ReadUint8Slice

func ReadUint8Slice(r Reader, c []uint8) (n int64, err error)

ReadUint8Slice reads a slice of byte from r and stores the result into c.

func RequireSerializerCorrect

func RequireSerializerCorrect(t *testing.T, input binarySerializer)

RequireSerializerCorrect tests that:

  • input and output implement TestInterface
  • input.WriteTo(io.Writer) writes a number of bytes on the writer equal to the number of bytes generated by input.MarshalBinary()
  • input.WriteTo buffered bytes are equal to the bytes generated by input.MarshalBinary()
  • output.ReadFrom(io.Reader) reads a number of bytes on the reader equal to the number of bytes written using input.WriteTo(io.Writer)
  • applies require.Equalf between the original and reconstructed object for
  • all the above WriteTo, ReadFrom, MarhsalBinary and UnmarshalBinary do not return an error

func Write

func Write(w Writer, c []byte) (n int64, err error)

Write writes a slice of bytes to w.

func WriteAsUint16

func WriteAsUint16[T any](w Writer, c T) (n int64, err error)

WriteAsUint16 casts &T to an *uint16 and writes it to w. User must ensure that T can be stored in an uint16.

func WriteAsUint16Slice

func WriteAsUint16Slice[T any](w Writer, c []T) (n int64, err error)

WriteAsUint16Slice casts &[]T into *[]uint16 and writes it to w. User must ensure that T can be stored in an uint16.

func WriteAsUint32

func WriteAsUint32[T any](w Writer, c T) (n int64, err error)

WriteAsUint32 casts &T to an *uint32 and writes it to w. User must ensure that T can be stored in an uint32.

func WriteAsUint32Slice

func WriteAsUint32Slice[T any](w Writer, c []T) (n int64, err error)

WriteAsUint32Slice casts &[]T into *[]uint32 and writes it to w. User must ensure that T can be stored in an uint32.

func WriteAsUint64

func WriteAsUint64[T any](w Writer, c T) (n int64, err error)

WriteAsUint64 casts &T to an *uint64 and writes it to w. User must ensure that T can be stored in an uint64.

func WriteAsUint64Slice

func WriteAsUint64Slice[T any](w Writer, c []T) (n int64, err error)

WriteAsUint64Slice casts &[]T into *[]uint64 and writes it to w. User must ensure that T can be stored in an uint64.

func WriteAsUint8

func WriteAsUint8[T any](w Writer, c T) (n int64, err error)

WriteAsUint8 casts &T to an *uint8 and writes it to w. User must ensure that T can be stored in an uint8.

func WriteAsUint8Slice

func WriteAsUint8Slice[T any](w Writer, c []T) (n int64, err error)

WriteAsUint8Slice casts &[]T into *[]uint8 and writes it to w. User must ensure that T can be stored in an uint8.

func WriteUint16

func WriteUint16(w Writer, c uint16) (n int64, err error)

WriteUint16 writes a uint16 c to w.

func WriteUint16Slice

func WriteUint16Slice(w Writer, c []uint16) (n int64, err error)

WriteUint16Slice writes a slice of uint16 c to w.

func WriteUint32

func WriteUint32(w Writer, c uint32) (n int64, err error)

WriteUint32 writes a uint32 c into w.

func WriteUint32Slice

func WriteUint32Slice(w Writer, c []uint32) (n int64, err error)

WriteUint32Slice writes a slice of uint32 c into w.

func WriteUint64

func WriteUint64(w Writer, c uint64) (n int64, err error)

WriteUint64 writes a uint64 c into w.

func WriteUint64Slice

func WriteUint64Slice(w Writer, c []uint64) (n int64, err error)

WriteUint64Slice writes a slice of uint64 into w.

func WriteUint8

func WriteUint8(w Writer, c uint8) (n int64, err error)

WriteUint8 writes a byte c to w.

func WriteUint8Slice

func WriteUint8Slice(w Writer, c []uint8) (n int64, err error)

WriteUint8Slice writes a slice of bytes c to w.

Types

type Buffer

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

Buffer is a simple []byte-based buffer that complies to the Writer and Reader interfaces. This type assumes that its backing slice has a fixed size and won't attempt to extend it. Instead, writes beyond capacity will result in an error.

func NewBuffer

func NewBuffer(buff []byte) *Buffer

NewBuffer creates a new Buffer struct with buff as a backing []byte. The read and write offset are initialized at buff[0]. Hence, writing new data will overwrite the content of buff.

func NewBufferSize

func NewBufferSize(size int) *Buffer

NewBufferSize creates a new Buffer with size capacity.

func (*Buffer) Available

func (b *Buffer) Available() int

Available returns the number of bytes available for writes on the buffer.

func (*Buffer) AvailableBuffer

func (b *Buffer) AvailableBuffer() []byte

AvailableBuffer returns an empty buffer with b.Available() capacity, to be directly appended to and passed to a Write call. The buffer is only valid until the next write operation on b.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns the backing slice.

func (*Buffer) Discard

func (b *Buffer) Discard(n int) (discarded int, err error)

Discard skips the next n bytes, returning the number of bytes discarded. If Discard skips fewer than n bytes, it also returns an error.

func (*Buffer) Flush

func (b *Buffer) Flush() (err error)

Flush doesn't do anything on this slice-based buffer.

func (*Buffer) Peek

func (b *Buffer) Peek(n int) ([]byte, error)

Peek returns the next n bytes without advancing the read offset, directly as a reslice of the internal buffer. It returns an error if the number of returned bytes is smaller than n.

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err error)

Read reads len(p) bytes from the read offset of b into p. It returns the number n of bytes read and an error if n < len(p).

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset re-initializes the read and write offsets of b.

func (*Buffer) Size

func (b *Buffer) Size() int

Size returns the size of the buffer available for read.

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

Write writes p into b. It returns the number of bytes written and an error if attempting to write passed the initial capacity of the buffer. Note that the case where p shares the same backing memory as b is optimized.

type Reader

type Reader interface {
	io.Reader
	Size() int
	Peek(n int) ([]byte, error)
	Discard(n int) (discarded int, err error)
}

Reader is an interface for readers that expose their internal buffers. This interface is notably implemented by the bufio.Reader type (see https://pkg.go.dev/bufio#Reader) and by the Buffer type.

type Writer

type Writer interface {
	io.Writer
	Flush() (err error)
	AvailableBuffer() []byte
	Available() int
}

Writer is an interface for writers that expose their internal buffers. This interface is notably implemented by the bufio.Writer type (see https://pkg.go.dev/bufio#Writer) and by the Buffer type.

Jump to

Keyboard shortcuts

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