buf

package
v0.0.0-...-694aa1c Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package buf provides a light-weight memory allocation mechanism.

Index

Constants

View Source
const (
	// Size of a regular buffer.
	Size = 8192
)

Variables

View Source
var ErrNotTimeoutReader = errors.New("not a TimeoutReader")
View Source
var ErrReadTimeout = errors.New("IO timeout")

ErrReadTimeout is an error that happens with IO timeout.

Functions

func Copy

func Copy(reader Reader, writer Writer, options ...CopyOption) error

Copy dumps all payload from reader to writer or stops when an error occurs. It returns nil when EOF.

func CopyOnceTimeout

func CopyOnceTimeout(reader Reader, writer Writer, timeout time.Duration) error

func IsReadError

func IsReadError(err error) bool

IsReadError returns true if the error in Copy() comes from reading.

func IsWriteError

func IsWriteError(err error) bool

IsWriteError returns true if the error in Copy() comes from writing.

func MergeMulti

func MergeMulti(dest MultiBuffer, src MultiBuffer) (MultiBuffer, MultiBuffer)

MergeMulti merges content from src to dest, and returns the new address of dest and src

func ReadAllToBytes

func ReadAllToBytes(reader io.Reader) ([]byte, error)

ReadAllToBytes reads all content from the reader into a byte array, until EOF.

func SplitFirst

func SplitFirst(mb MultiBuffer) (MultiBuffer, *Buffer)

SplitFirst splits the first Buffer from the beginning of the MultiBuffer.

func SplitMulti

func SplitMulti(mb MultiBuffer, i int) (MultiBuffer, MultiBuffer)

SplitMulti splits the beginning of the MultiBuffer into first one, the index i and after into second one

func SplitSize

func SplitSize(mb MultiBuffer, size int32) (MultiBuffer, MultiBuffer)

SplitSize splits the beginning of the MultiBuffer into another one, for at most size bytes.

func WriteAllBytes

func WriteAllBytes(writer io.Writer, payload []byte, c stats.Counter) error

WriteAllBytes ensures all bytes are written into the given writer.

Types

type Buffer

type Buffer struct {
	UDP *net.Destination
	// contains filtered or unexported fields
}

Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles the buffer into an internal buffer pool, in order to recreate a buffer more quickly.

func FromBytes

func FromBytes(b []byte) *Buffer

FromBytes creates a Buffer with an existed bytearray

func New

func New() *Buffer

New creates a Buffer with 0 length and 8K capacity.

func NewExisted

func NewExisted(b []byte) *Buffer

NewExisted creates a managed, standard size Buffer with an existed bytearray

func NewWithSize

func NewWithSize(size int32) *Buffer

NewWithSize creates a Buffer with 0 length and capacity with at least the given size.

func ReadBuffer

func ReadBuffer(r io.Reader) (*Buffer, error)

ReadBuffer reads a Buffer from the given reader.

func StackNew

func StackNew() Buffer

StackNew creates a new Buffer object on stack. This method is for buffers that is released in the same function.

func (*Buffer) Advance

func (b *Buffer) Advance(from int32)

Advance cuts the buffer at the given position.

func (*Buffer) Byte

func (b *Buffer) Byte(index int32) byte

Byte returns the bytes at index.

func (*Buffer) Bytes

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

Bytes returns the content bytes of this Buffer.

func (*Buffer) BytesFrom

func (b *Buffer) BytesFrom(from int32) []byte

BytesFrom returns a slice of this Buffer starting from the given position.

func (*Buffer) BytesRange

func (b *Buffer) BytesRange(from, to int32) []byte

BytesRange returns a slice of this buffer with given from and to boundary.

func (*Buffer) BytesTo

func (b *Buffer) BytesTo(to int32) []byte

BytesTo returns a slice of this Buffer from start to the given position.

func (*Buffer) Cap

func (b *Buffer) Cap() int32

Cap returns the capacity of the buffer content.

func (*Buffer) Check

func (b *Buffer) Check()

Check makes sure that 0 <= b.start <= b.end.

func (*Buffer) Clear

func (b *Buffer) Clear()

Clear clears the content of the buffer, results an empty buffer with Len() = 0.

func (*Buffer) Extend

func (b *Buffer) Extend(n int32) []byte

Extend increases the buffer size by n bytes, and returns the extended part. It panics if result size is larger than buf.Size.

func (*Buffer) IsEmpty

func (b *Buffer) IsEmpty() bool

IsEmpty returns true if the buffer is empty.

func (*Buffer) IsFull

func (b *Buffer) IsFull() bool

IsFull returns true if the buffer has no more room to grow.

func (*Buffer) Len

func (b *Buffer) Len() int32

Len returns the length of the buffer content.

func (*Buffer) Read

func (b *Buffer) Read(data []byte) (int, error)

Read implements io.Reader.Read().

func (*Buffer) ReadByte

func (b *Buffer) ReadByte() (byte, error)

ReadByte implements io.ByteReader

func (*Buffer) ReadBytes

func (b *Buffer) ReadBytes(length int32) ([]byte, error)

ReadBytes implements bufio.Reader.ReadBytes

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(reader io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom.

func (*Buffer) ReadFullFrom

func (b *Buffer) ReadFullFrom(reader io.Reader, size int32) (int64, error)

ReadFullFrom reads exact size of bytes from given reader, or until error occurs.

func (*Buffer) Release

func (b *Buffer) Release()

Release recycles the buffer into an internal buffer pool.

func (*Buffer) Resize

func (b *Buffer) Resize(from, to int32)

Resize cuts the buffer at the given position.

func (*Buffer) SetByte

func (b *Buffer) SetByte(index int32, value byte)

SetByte sets the byte value at index.

func (*Buffer) String

func (b *Buffer) String() string

String returns the string form of this Buffer.

func (*Buffer) Write

func (b *Buffer) Write(data []byte) (int, error)

Write implements Write method in io.Writer.

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(v byte) error

WriteByte writes a single byte into the buffer.

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (int, error)

WriteString implements io.StringWriter.

type BufferToBytesWriter

type BufferToBytesWriter struct {
	io.Writer
	// contains filtered or unexported fields
}

BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer.

func (*BufferToBytesWriter) ReadFrom

func (w *BufferToBytesWriter) ReadFrom(reader io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom.

func (*BufferToBytesWriter) WriteMultiBuffer

func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error

WriteMultiBuffer implements Writer. This method takes ownership of the given buffer.

type BufferedReader

type BufferedReader struct {
	// Reader is the underlying reader to be read from
	Reader Reader
	// Buffer is the internal buffer to be read from first
	Buffer MultiBuffer
	// Splitter is a function to read bytes from MultiBuffer
	Splitter func(MultiBuffer, []byte) (MultiBuffer, int)
}

BufferedReader is a Reader that keeps its internal buffer.

func (*BufferedReader) BufferedBytes

func (r *BufferedReader) BufferedBytes() int32

BufferedBytes returns the number of bytes that is cached in this reader.

func (*BufferedReader) Close

func (r *BufferedReader) Close() error

Close implements io.Closer.

func (*BufferedReader) Interrupt

func (r *BufferedReader) Interrupt()

Interrupt implements common.Interruptible.

func (*BufferedReader) Read

func (r *BufferedReader) Read(b []byte) (int, error)

Read implements io.Reader. It reads from internal buffer first (if available) and then reads from the underlying reader.

func (*BufferedReader) ReadAtMost

func (r *BufferedReader) ReadAtMost(size int32) (MultiBuffer, error)

ReadAtMost returns a MultiBuffer with at most size.

func (*BufferedReader) ReadByte

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

ReadByte implements io.ByteReader.

func (*BufferedReader) ReadMultiBuffer

func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error)

ReadMultiBuffer implements Reader.

func (*BufferedReader) WriteTo

func (r *BufferedReader) WriteTo(writer io.Writer) (int64, error)

WriteTo implements io.WriterTo.

type BufferedWriter

type BufferedWriter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

BufferedWriter is a Writer with internal buffer.

func NewBufferedWriter

func NewBufferedWriter(writer Writer) *BufferedWriter

NewBufferedWriter creates a new BufferedWriter.

func (*BufferedWriter) Close

func (w *BufferedWriter) Close() error

Close implements io.Closable.

func (*BufferedWriter) Flush

func (w *BufferedWriter) Flush() error

Flush flushes buffered content into underlying writer.

func (*BufferedWriter) ReadFrom

func (w *BufferedWriter) ReadFrom(reader io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom.

func (*BufferedWriter) SetBuffered

func (w *BufferedWriter) SetBuffered(f bool) error

SetBuffered sets whether the internal buffer is used. If set to false, Flush() will be called to clear the buffer.

func (*BufferedWriter) Write

func (w *BufferedWriter) Write(b []byte) (int, error)

Write implements io.Writer.

func (*BufferedWriter) WriteByte

func (w *BufferedWriter) WriteByte(c byte) error

WriteByte implements io.ByteWriter.

func (*BufferedWriter) WriteMultiBuffer

func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error

WriteMultiBuffer implements Writer. It takes ownership of the given MultiBuffer.

type CopyOption

type CopyOption func(*copyHandler)

CopyOption is an option for copying data.

func AddToStatCounter

func AddToStatCounter(sc stats.Counter) CopyOption

AddToStatCounter a CopyOption add to stat counter

func CountSize

func CountSize(sc *SizeCounter) CopyOption

CountSize is a CopyOption that sums the total size of data copied into the given SizeCounter.

func UpdateActivity

func UpdateActivity(timer signal.ActivityUpdater) CopyOption

UpdateActivity is a CopyOption to update activity on each data copy operation.

type EndpointOverrideReader

type EndpointOverrideReader struct {
	Reader
	Dest         net.Address
	OriginalDest net.Address
}

func (*EndpointOverrideReader) ReadMultiBuffer

func (r *EndpointOverrideReader) ReadMultiBuffer() (MultiBuffer, error)

type EndpointOverrideWriter

type EndpointOverrideWriter struct {
	Writer
	Dest         net.Address
	OriginalDest net.Address
}

func (*EndpointOverrideWriter) WriteMultiBuffer

func (w *EndpointOverrideWriter) WriteMultiBuffer(mb MultiBuffer) error

type MultiBuffer

type MultiBuffer []*Buffer

MultiBuffer is a list of Buffers. The order of Buffer matters.

func Compact

func Compact(mb MultiBuffer) MultiBuffer

Compact returns another MultiBuffer by merging all content of the given one together.

func MergeBytes

func MergeBytes(dest MultiBuffer, src []byte) MultiBuffer

MergeBytes merges the given bytes into MultiBuffer and return the new address of the merged MultiBuffer.

func ReadFrom

func ReadFrom(reader io.Reader) (MultiBuffer, error)

ReadFrom reads all content from reader until EOF.

func ReleaseMulti

func ReleaseMulti(mb MultiBuffer) MultiBuffer

ReleaseMulti releases all content of the MultiBuffer, and returns an empty MultiBuffer.

func SplitBytes

func SplitBytes(mb MultiBuffer, b []byte) (MultiBuffer, int)

SplitBytes splits the given amount of bytes from the beginning of the MultiBuffer. It returns the new address of MultiBuffer leftover, and number of bytes written into the input byte slice.

func SplitFirstBytes

func SplitFirstBytes(mb MultiBuffer, p []byte) (MultiBuffer, int)

SplitFirstBytes splits the first buffer from MultiBuffer, and then copy its content into the given slice.

func WriteMultiBuffer

func WriteMultiBuffer(writer io.Writer, mb MultiBuffer) (MultiBuffer, error)

WriteMultiBuffer writes all buffers from the MultiBuffer to the Writer one by one, and return error if any, with leftover MultiBuffer.

func (MultiBuffer) Copy

func (mb MultiBuffer) Copy(b []byte) int

Copy copied the beginning part of the MultiBuffer into the given byte array.

func (MultiBuffer) IsEmpty

func (mb MultiBuffer) IsEmpty() bool

IsEmpty returns true if the MultiBuffer has no content.

func (MultiBuffer) Len

func (mb MultiBuffer) Len() int32

Len returns the total number of bytes in the MultiBuffer.

func (MultiBuffer) String

func (mb MultiBuffer) String() string

String returns the content of the MultiBuffer in string.

type MultiBufferContainer

type MultiBufferContainer struct {
	MultiBuffer
}

MultiBufferContainer is a ReadWriteCloser wrapper over MultiBuffer.

func (*MultiBufferContainer) Close

func (c *MultiBufferContainer) Close() error

Close implements io.Closer.

func (*MultiBufferContainer) Read

func (c *MultiBufferContainer) Read(b []byte) (int, error)

Read implements io.Reader.

func (*MultiBufferContainer) ReadMultiBuffer

func (c *MultiBufferContainer) ReadMultiBuffer() (MultiBuffer, error)

ReadMultiBuffer implements Reader.

func (*MultiBufferContainer) Write

func (c *MultiBufferContainer) Write(b []byte) (int, error)

Write implements io.Writer.

func (*MultiBufferContainer) WriteMultiBuffer

func (c *MultiBufferContainer) WriteMultiBuffer(b MultiBuffer) error

WriteMultiBuffer implements Writer.

type PacketReader

type PacketReader struct {
	io.Reader
}

PacketReader is a Reader that read one Buffer every time.

func (*PacketReader) ReadMultiBuffer

func (r *PacketReader) ReadMultiBuffer() (MultiBuffer, error)

ReadMultiBuffer implements Reader.

type ReadVReader

type ReadVReader struct {
	io.Reader
	// contains filtered or unexported fields
}

ReadVReader is a Reader that uses readv(2) syscall to read data.

func NewReadVReader

func NewReadVReader(reader io.Reader, rawConn syscall.RawConn, counter stats.Counter) *ReadVReader

NewReadVReader creates a new ReadVReader.

func (*ReadVReader) ReadMultiBuffer

func (r *ReadVReader) ReadMultiBuffer() (MultiBuffer, error)

ReadMultiBuffer implements Reader.

type Reader

type Reader interface {
	// ReadMultiBuffer reads content from underlying reader, and put it into a MultiBuffer.
	ReadMultiBuffer() (MultiBuffer, error)
}

Reader extends io.Reader with MultiBuffer.

func NewPacketReader

func NewPacketReader(reader io.Reader) Reader

NewPacketReader creates a new PacketReader based on the given reader.

func NewReader

func NewReader(reader io.Reader) Reader

NewReader creates a new Reader. The Reader instance doesn't take the ownership of reader.

type SequentialWriter

type SequentialWriter struct {
	io.Writer
}

SequentialWriter is a Writer that writes MultiBuffer sequentially into the underlying io.Writer.

func (*SequentialWriter) WriteMultiBuffer

func (w *SequentialWriter) WriteMultiBuffer(mb MultiBuffer) error

WriteMultiBuffer implements Writer.

type SingleReader

type SingleReader struct {
	io.Reader
}

SingleReader is a Reader that read one Buffer every time.

func (*SingleReader) ReadMultiBuffer

func (r *SingleReader) ReadMultiBuffer() (MultiBuffer, error)

ReadMultiBuffer implements Reader.

type SizeCounter

type SizeCounter struct {
	Size int64
}

SizeCounter is for counting bytes copied by Copy().

type TimeoutReader

type TimeoutReader interface {
	ReadMultiBufferTimeout(time.Duration) (MultiBuffer, error)
}

TimeoutReader is a reader that returns error if Read() operation takes longer than the given timeout.

type Writer

type Writer interface {
	// WriteMultiBuffer writes a MultiBuffer into underlying writer.
	WriteMultiBuffer(MultiBuffer) error
}

Writer extends io.Writer with MultiBuffer.

var (
	// Discard is a Writer that swallows all contents written in.
	Discard Writer = noOpWriter(0)

	// DiscardBytes is an io.Writer that swallows all contents written in.
	DiscardBytes io.Writer = noOpWriter(0)
)

func NewWriter

func NewWriter(writer io.Writer) Writer

NewWriter creates a new Writer.

Jump to

Keyboard shortcuts

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