Documentation ¶
Overview ¶
Package buf provides a light-weight memory allocation mechanism.
Index ¶
- Constants
- Variables
- func Copy(reader Reader, writer Writer, options ...CopyOption) error
- func ReadAllToBytes(reader io.Reader) ([]byte, error)
- 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 BufferToBytesWriter
- type BufferedReader
- func (r *BufferedReader) IsBuffered() bool
- func (r *BufferedReader) Read(b []byte) (int, error)
- func (r *BufferedReader) ReadAtMost(size int) (MultiBuffer, error)
- func (r *BufferedReader) ReadByte() (byte, error)
- func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error)
- func (r *BufferedReader) SetBuffered(f bool)
- func (r *BufferedReader) WriteTo(writer io.Writer) (int64, error)
- type BufferedWriter
- func (w *BufferedWriter) Flush() error
- func (w *BufferedWriter) ReadFrom(reader io.Reader) (int64, error)
- func (w *BufferedWriter) SetBuffered(f bool) error
- func (w *BufferedWriter) Write(b []byte) (int, error)
- func (w *BufferedWriter) WriteByte(c byte) error
- func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error
- 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 Pool
- type Reader
- type SizeCounter
- type Supplier
- type SyncPool
- type TimeoutReader
- type Writer
Constants ¶
const (
// Size of a regular buffer.
Size = 2 * 1024
)
Variables ¶
var ErrReadTimeout = newError("IO timeout")
ErrReadTimeout is an error that happens with IO timeout.
Functions ¶
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 BufferToBytesWriter ¶
BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer.
func NewBufferToBytesWriter ¶
func NewBufferToBytesWriter(writer io.Writer) *BufferToBytesWriter
NewBufferToBytesWriter returns a new BufferToBytesWriter.
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 {
// contains filtered or unexported fields
}
BufferedReader is a Reader that keeps its internal buffer.
func NewBufferedReader ¶
func NewBufferedReader(reader Reader) *BufferedReader
NewBufferedReader returns a new BufferedReader.
func (*BufferedReader) IsBuffered ¶
func (r *BufferedReader) IsBuffered() bool
IsBuffered returns true if internal buffer is used.
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 int) (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) SetBuffered ¶
func (r *BufferedReader) SetBuffered(f bool)
SetBuffered sets whether to keep the interal buffer.
type BufferedWriter ¶
type BufferedWriter struct {
// 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) 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 BytesToBufferReader ¶
BytesToBufferReader is a Reader that adjusts its reading speed automatically.
func (*BytesToBufferReader) ReadMultiBuffer ¶
func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error)
ReadMultiBuffer implements Reader.
type CopyOption ¶
type CopyOption func(*copyHandler)
CopyOption is an option for copying data.
func CountSize ¶
func CountSize(sc *SizeCounter) CopyOption
CountSize is a CopyOption that sums the total size of data copied into the given SizeCounter.
func IgnoreReaderError ¶
func IgnoreReaderError() CopyOption
IgnoreReaderError is a CopyOption that ignores errors from reader. Copy will continue in such case.
func IgnoreWriterError ¶
func IgnoreWriterError() CopyOption
IgnoreWriterError is a CopyOption that ignores errors from writer. Copy will continue in such case.
func UpdateActivity ¶
func UpdateActivity(timer signal.ActivityUpdater) CopyOption
UpdateActivity is a CopyOption to update activity on each data copy operation.
type MultiBuffer ¶
type MultiBuffer []*Buffer
MultiBuffer is a list of Buffers. The order of Buffer matters.
func NewMultiBufferCap ¶
func NewMultiBufferCap(capacity int) MultiBuffer
NewMultiBufferCap creates a new MultiBuffer instance.
func NewMultiBufferValue ¶
func NewMultiBufferValue(b ...*Buffer) MultiBuffer
NewMultiBufferValue wraps a list of Buffers into MultiBuffer.
func ReadAllToMultiBuffer ¶
func ReadAllToMultiBuffer(reader io.Reader) (MultiBuffer, error)
ReadAllToMultiBuffer reads all content from the reader into a MultiBuffer, until EOF.
func (*MultiBuffer) Append ¶
func (mb *MultiBuffer) Append(buf *Buffer)
Append appends buffer to the end of this MultiBuffer
func (*MultiBuffer) AppendMulti ¶
func (mb *MultiBuffer) AppendMulti(buf MultiBuffer)
AppendMulti appends a MultiBuffer to the end of this one.
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 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) Read ¶
func (mb *MultiBuffer) Read(b []byte) (int, error)
Read implements io.Reader.
func (*MultiBuffer) Release ¶
func (mb *MultiBuffer) Release()
Release releases all Buffers in the MultiBuffer.
func (*MultiBuffer) SliceBySize ¶
func (mb *MultiBuffer) SliceBySize(size int) MultiBuffer
SliceBySize splits the beginning of this MultiBuffer into another one, for at most size bytes.
func (*MultiBuffer) SplitFirst ¶
func (mb *MultiBuffer) SplitFirst() *Buffer
SplitFirst splits out the first Buffer in this MultiBuffer.
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.
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 { // ReadMultiBuffer reads content from underlying reader, and put it into a MultiBuffer. ReadMultiBuffer() (MultiBuffer, error) }
Reader extends io.Reader with MultiBuffer.
func NewBytesToBufferReader ¶
NewBytesToBufferReader returns a new BytesToBufferReader.
type SizeCounter ¶
type SizeCounter struct {
Size int64
}
SizeCounter is for counting bytes copied by Copy().
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)
}
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.
func NewSequentialWriter ¶
NewSequentialWriter returns a Writer that write Buffers in a MultiBuffer sequentially.