Documentation ¶
Overview ¶
Package buf provides a light-weight memory allocation mechanism.
Index ¶
- Constants
- Variables
- func Copy(reader Reader, writer Writer, options ...CopyOption) error
- func ToBytesReader(stream Reader) io.Reader
- func ToBytesWriter(writer Writer) io.Writer
- type Buffer
- func (b *Buffer) Append(data []byte) int
- func (b *Buffer) AppendBytes(bytes ...byte) int
- func (b *Buffer) AppendSupplier(writer Supplier) error
- func (b *Buffer) Byte(index int) byte
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) BytesFrom(from int) []byte
- func (b *Buffer) BytesRange(from, to int) []byte
- func (b *Buffer) BytesTo(to int) []byte
- func (b *Buffer) Clear()
- func (b *Buffer) IsEmpty() bool
- func (b *Buffer) IsFull() bool
- func (b *Buffer) Len() int
- func (b *Buffer) Read(data []byte) (int, error)
- func (b *Buffer) Release()
- func (b *Buffer) Reset(writer Supplier) error
- func (b *Buffer) SetByte(index int, value byte)
- func (b *Buffer) Slice(from, to int)
- func (b *Buffer) SliceFrom(from int)
- func (b *Buffer) String() string
- func (b *Buffer) Write(data []byte) (int, error)
- type BufferPool
- type BufferToBytesWriter
- type BufferedReader
- type BufferedWriter
- type BytesToBufferReader
- type CopyOption
- type MultiBuffer
- func (mb *MultiBuffer) Append(buf *Buffer)
- func (mb *MultiBuffer) AppendMulti(buf MultiBuffer)
- func (mb MultiBuffer) Copy(b []byte) int
- func (mb MultiBuffer) IsEmpty() bool
- func (mb MultiBuffer) Len() int
- func (mb *MultiBuffer) Read(b []byte) (int, error)
- func (mb MultiBuffer) Release()
- func (mb *MultiBuffer) SliceBySize(size int) MultiBuffer
- func (mb *MultiBuffer) SplitFirst() *Buffer
- func (mb MultiBuffer) ToNetBuffers() net.Buffers
- func (mb *MultiBuffer) Write(b []byte)
- type MultiBufferReader
- type MultiBufferWriter
- type Pool
- type Reader
- type Supplier
- type SyncPool
- type TimeoutReader
- type Writer
Constants ¶
const (
// Size of a regular buffer.
Size = 2 * 1024
)
Variables ¶
var ErrReadTimeout = newError("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. ActivityTimer gets updated as soon as there is a payload.
func ToBytesReader ¶
ToBytesReader converts a Reaaer to io.Reader.
func ToBytesWriter ¶
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 NewLocal ¶
NewLocal creates and returns a buffer with 0 length and given capacity on current thread.
func (*Buffer) AppendBytes ¶
AppendBytes appends one or more bytes to the end of the buffer.
func (*Buffer) AppendSupplier ¶
AppendSupplier appends the content of a BytesWriter to the buffer.
func (*Buffer) BytesFrom ¶
BytesFrom returns a slice of this Buffer starting from the given position.
func (*Buffer) BytesRange ¶
BytesRange returns a slice of this buffer with given from and to bounary.
func (*Buffer) Clear ¶
func (b *Buffer) Clear()
Clear clears the content of the buffer, results an empty buffer with Len() = 0.
func (*Buffer) Release ¶
func (b *Buffer) Release()
Release recycles the buffer into an internal buffer pool.
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().
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 (w *BufferToBytesWriter) Write(mb MultiBuffer) 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 (r *BufferedReader) IsBuffered() bool
IsBuffered returns true if the internal cache is effective.
func (*BufferedReader) Read ¶
func (r *BufferedReader) Read(b []byte) (int, error)
Read implements io.Reader.Read().
func (*BufferedReader) SetBuffered ¶
func (r *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(writer io.Writer) *BufferedWriter
NewBufferedWriter creates a new BufferedWriter.
func NewBufferedWriterSize ¶
func NewBufferedWriterSize(writer io.Writer, size uint32) *BufferedWriter
func (*BufferedWriter) Flush ¶
func (w *BufferedWriter) Flush() error
Flush writes all buffered content into underlying writer, if any.
func (*BufferedWriter) IsBuffered ¶
func (w *BufferedWriter) IsBuffered() bool
IsBuffered returns true if this BufferedWriter holds a buffer.
func (*BufferedWriter) SetBuffered ¶
func (w *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.
type BytesToBufferReader ¶
type BytesToBufferReader struct {
// contains filtered or unexported fields
}
BytesToBufferReader is a Reader that adjusts its reading speed automatically.
func (*BytesToBufferReader) Read ¶
func (r *BytesToBufferReader) Read() (MultiBuffer, error)
Read implements Reader.Read().
type CopyOption ¶
type CopyOption func(*copyHandler)
func IgnoreReaderError ¶
func IgnoreReaderError() CopyOption
func IgnoreWriterError ¶
func IgnoreWriterError() CopyOption
func UpdateActivity ¶
func UpdateActivity(timer signal.ActivityUpdater) CopyOption
type MultiBuffer ¶
type MultiBuffer []*Buffer
MultiBuffer is a list of Buffers. The order of Buffer matters.
func NewMultiBuffer ¶
func NewMultiBuffer() MultiBuffer
NewMultiBuffer creates a new MultiBuffer instance.
func NewMultiBufferValue ¶
func NewMultiBufferValue(b ...*Buffer) MultiBuffer
NewMultiBufferValue wraps a list of Buffers into MultiBuffer.
func (*MultiBuffer) Append ¶
func (mb *MultiBuffer) Append(buf *Buffer)
func (*MultiBuffer) AppendMulti ¶
func (mb *MultiBuffer) AppendMulti(buf MultiBuffer)
func (MultiBuffer) Copy ¶
func (mb MultiBuffer) Copy(b []byte) int
func (MultiBuffer) IsEmpty ¶
func (mb MultiBuffer) IsEmpty() bool
IsEmpty return true if the MultiBuffer has no content.
func (MultiBuffer) Len ¶
func (mb MultiBuffer) Len() int
Len returns the total number of bytes in the MultiBuffer.
func (MultiBuffer) Release ¶
func (mb MultiBuffer) Release()
Release releases all Buffers in the MultiBuffer.
func (*MultiBuffer) SliceBySize ¶
func (mb *MultiBuffer) SliceBySize(size int) MultiBuffer
func (*MultiBuffer) SplitFirst ¶
func (mb *MultiBuffer) SplitFirst() *Buffer
func (MultiBuffer) ToNetBuffers ¶
func (mb MultiBuffer) ToNetBuffers() net.Buffers
ToNetBuffers converts this MultiBuffer to net.Buffers. The return net.Buffers points to the same content of the MultiBuffer.
func (*MultiBuffer) Write ¶
func (mb *MultiBuffer) Write(b []byte)
type MultiBufferReader ¶
type MultiBufferReader interface {
ReadMultiBuffer() (MultiBuffer, error)
}
type MultiBufferWriter ¶
type MultiBufferWriter interface {
WriteMultiBuffer(MultiBuffer) error
}
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() (MultiBuffer, error) }
Reader extends io.Reader with alloc.Buffer.
func NewMergingReader ¶
type Supplier ¶
Supplier is a writer that writes contents into the given buffer.
func ReadAtLeastFrom ¶
ReadAtLeastFrom create a Supplier to read at least size bytes from the given io.Reader.
type SyncPool ¶
type SyncPool struct {
// contains filtered or unexported fields
}
SyncPool is a buffer pool based on sync.Pool
func NewSyncPool ¶
NewSyncPool creates a SyncPool with given buffer size.
type TimeoutReader ¶
type TimeoutReader interface {
ReadTimeout(time.Duration) (MultiBuffer, error)
}
type Writer ¶
type Writer interface { // Write writes an alloc.Buffer into underlying writer. Write(MultiBuffer) error }
Writer extends io.Writer with alloc.Buffer.
var (
Discard Writer = noOpWriter{}
)