buf

package
v2.20.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2017 License: MIT Imports: 7 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 = 8 * 1024
	// SizeSmall is the size of a small buffer.
	SizeSmall = 2 * 1024
)

Variables

This section is empty.

Functions

func Pipe

func Pipe(timer *signal.ActivityTimer, reader Reader, writer Writer) error

Pipe dumps all payload from reader to writer, until an error occurs. ActivityTimer gets updated as soon as there is a payload.

func PipeUntilEOF

func PipeUntilEOF(timer *signal.ActivityTimer, reader Reader, writer Writer) error

PipeUntilEOF behaves the same as Pipe(). The only difference is PipeUntilEOF returns nil on EOF.

func ToBytesReader

func ToBytesReader(stream Reader) io.Reader

ToBytesReader converts a Reaaer to io.Reader.

func ToBytesWriter

func ToBytesWriter(writer Writer) io.Writer

ToBytesWriter converts a Writer to io.Writer

Types

type Buffer

type Buffer struct {
	// 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 New

func New() *Buffer

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

func NewLocal

func NewLocal(size int) *Buffer

NewLocal creates and returns a buffer with 0 length and given capacity on current thread.

func NewSmall

func NewSmall() *Buffer

NewSmall returns a buffer with 0 length and 2K capacity.

func (*Buffer) Append

func (b *Buffer) Append(data []byte)

Append appends a byte array to the end of the buffer.

func (*Buffer) AppendBytes

func (b *Buffer) AppendBytes(bytes ...byte)

AppendBytes appends one or more bytes to the end of the buffer.

func (*Buffer) AppendSupplier

func (b *Buffer) AppendSupplier(writer Supplier) error

AppendSupplier appends the content of a BytesWriter to the buffer.

func (*Buffer) Byte

func (b *Buffer) Byte(index int) 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 int) []byte

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

func (*Buffer) BytesRange

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

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

func (*Buffer) BytesTo

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

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

func (*Buffer) Clear

func (b *Buffer) Clear()

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

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() int

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) Release

func (b *Buffer) Release()

Release recycles the buffer into an internal buffer pool.

func (*Buffer) Reset

func (b *Buffer) Reset(writer Supplier) error

Reset resets the content of the Buffer with a supplier.

func (*Buffer) SetByte

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

SetByte sets the byte value at index.

func (*Buffer) Slice

func (b *Buffer) Slice(from, to int)

Slice cuts the buffer at the given position.

func (*Buffer) SliceFrom

func (b *Buffer) SliceFrom(from int)

SliceFrom cuts the buffer at the given position.

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.

type BufferPool

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

BufferPool is a Pool that utilizes an internal cache.

func NewBufferPool

func NewBufferPool(bufferSize, poolSize uint32) *BufferPool

NewBufferPool creates a new BufferPool with given buffer size, and internal cache size.

func (*BufferPool) Allocate

func (p *BufferPool) Allocate() *Buffer

Allocate implements Pool.Allocate().

func (*BufferPool) Free

func (p *BufferPool) Free(buffer *Buffer)

Free implements Pool.Free().

type BufferToBytesWriter

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

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

func (*BufferToBytesWriter) Write

func (v *BufferToBytesWriter) Write(buffer *Buffer) error

Write implements Writer.Write(). Write() takes ownership of the given buffer.

type BufferedReader

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

BufferedReader is a reader with internal cache.

func NewBufferedReader

func NewBufferedReader(rawReader io.Reader) *BufferedReader

NewBufferedReader creates a new BufferedReader based on an io.Reader.

func (*BufferedReader) IsBuffered

func (v *BufferedReader) IsBuffered() bool

IsBuffered returns true if the internal cache is effective.

func (*BufferedReader) Read

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

Read implements io.Reader.Read().

func (*BufferedReader) SetBuffered

func (v *BufferedReader) SetBuffered(cached bool)

SetBuffered is to enable or disable internal cache. If cache is disabled, Read() calls will be delegated to the underlying io.Reader directly.

type BufferedWriter

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

BufferedWriter is an io.Writer with internal buffer. It writes to underlying writer when buffer is full or on demand. This type is not thread safe.

func NewBufferedWriter

func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter

NewBufferedWriter creates a new BufferedWriter.

func (*BufferedWriter) Flush

func (v *BufferedWriter) Flush() error

Flush writes all buffered content into underlying writer, if any.

func (*BufferedWriter) IsBuffered

func (v *BufferedWriter) IsBuffered() bool

IsBuffered returns true if this BufferedWriter holds a buffer.

func (*BufferedWriter) ReadFrom

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

ReadFrom implements io.ReaderFrom.ReadFrom().

func (*BufferedWriter) SetBuffered

func (v *BufferedWriter) SetBuffered(cached bool) error

SetBuffered controls whether the BufferedWriter holds a buffer for writing. If not buffered, any write() calls into underlying writer directly.

func (*BufferedWriter) Write

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

type BytesToBufferReader

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

BytesToBufferReader is a Reader that adjusts its reading speed automatically.

func (*BytesToBufferReader) Read

func (v *BytesToBufferReader) Read() (*Buffer, error)

Read implements Reader.Read().

type Pool

type Pool interface {
	// Allocate either returns a unused buffer from the pool, or generates a new one from system.
	Allocate() *Buffer
	// Free recycles the given buffer.
	Free(*Buffer)
}

Pool provides functionality to generate and recycle buffers on demand.

type Reader

type Reader interface {
	// Read reads content from underlying reader, and put it into an alloc.Buffer.
	Read() (*Buffer, error)
}

Reader extends io.Reader with alloc.Buffer.

func NewReader

func NewReader(reader io.Reader) Reader

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

type Supplier

type Supplier func([]byte) (int, error)

Supplier is a writer that writes contents into the given buffer.

func ReadFrom

func ReadFrom(reader io.Reader) Supplier

ReadFrom creates a Supplier to read from a given io.Reader.

func ReadFullFrom

func ReadFullFrom(reader io.Reader, size int) Supplier

ReadFullFrom creates a Supplier to read full buffer from a given io.Reader.

type SyncPool

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

SyncPool is a buffer pool based on sync.Pool

func NewSyncPool

func NewSyncPool(bufferSize uint32) *SyncPool

NewSyncPool creates a SyncPool with given buffer size.

func (*SyncPool) Allocate

func (p *SyncPool) Allocate() *Buffer

Allocate implements Pool.Allocate().

func (*SyncPool) Free

func (p *SyncPool) Free(buffer *Buffer)

Free implements Pool.Free().

type Writer

type Writer interface {
	// Write writes an alloc.Buffer into underlying writer.
	Write(*Buffer) error
}

Writer extends io.Writer with alloc.Buffer.

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