Documentation ¶
Overview ¶
Package buf provides a light-weight memory allocation mechanism.
Index ¶
- Constants
- Variables
- func Copy(reader Reader, writer Writer, options ...CopyOption) error
- func CopyOnceTimeout(reader Reader, writer Writer, timeout time.Duration) error
- func IsReadError(err error) bool
- func IsWriteError(err error) bool
- func ReadAllToBytes(reader io.Reader) ([]byte, error)
- func WriteAllBytes(writer io.Writer, payload []byte) error
- type Buffer
- func (b *Buffer) Advance(from int32)
- func (b *Buffer) Byte(index int32) byte
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) BytesFrom(from int32) []byte
- func (b *Buffer) BytesRange(from, to int32) []byte
- func (b *Buffer) BytesTo(to int32) []byte
- func (b *Buffer) Clear()
- func (b *Buffer) Extend(n int32) []byte
- func (b *Buffer) IsEmpty() bool
- func (b *Buffer) IsFull() bool
- func (b *Buffer) Len() int32
- func (b *Buffer) Read(data []byte) (int, error)
- func (b *Buffer) ReadFrom(reader io.Reader) (int64, error)
- func (b *Buffer) ReadFullFrom(reader io.Reader, size int32) (int64, error)
- func (b *Buffer) Release()
- func (b *Buffer) Resize(from, to int32)
- func (b *Buffer) SetByte(index int32, value byte)
- func (b *Buffer) String() string
- func (b *Buffer) Write(data []byte) (int, error)
- func (b *Buffer) WriteBytes(bytes ...byte) (int, error)
- func (b *Buffer) WriteString(s string) (int, error)
- type BufferToBytesWriter
- type BufferedReader
- func (r *BufferedReader) BufferedBytes() int32
- func (r *BufferedReader) Close() error
- func (r *BufferedReader) Read(b []byte) (int, error)
- func (r *BufferedReader) ReadAtMost(size int32) (MultiBuffer, error)
- func (r *BufferedReader) ReadByte() (byte, error)
- func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error)
- func (r *BufferedReader) WriteTo(writer io.Writer) (int64, error)
- type BufferedWriter
- func (w *BufferedWriter) Close() error
- 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 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() int32
- func (mb *MultiBuffer) Read(b []byte) (int, error)
- func (mb *MultiBuffer) ReadFrom(reader io.Reader) (int64, error)
- func (mb *MultiBuffer) Release()
- func (mb *MultiBuffer) SliceBySize(size int32) MultiBuffer
- func (mb *MultiBuffer) SplitFirst() *Buffer
- func (mb MultiBuffer) String() string
- func (mb *MultiBuffer) Write(b []byte) (int, error)
- func (mb *MultiBuffer) WriteMultiBuffer(b MultiBuffer) error
- func (mb *MultiBuffer) WriteTo(writer io.Writer) (int64, error)
- type ReadVReader
- type Reader
- type SequentialWriter
- type SingleReader
- type SizeCounter
- type TimeoutReader
- type Writer
Constants ¶
const (
// Size of a regular buffer.
Size = 2048
)
Variables ¶
var ErrNotTimeoutReader = newError("not a TimeoutReader")
var ErrReadTimeout = newError("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 ReadAllToBytes ¶
ReadAllToBytes reads all content from the reader into a byte array, until EOF.
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 (*Buffer) Advance ¶
Advance cuts the buffer at the given position.
func (*Buffer) Bytes ¶
Bytes returns the content bytes of this 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 boundary.
func (*Buffer) BytesTo ¶
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) Extend ¶
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 ¶
IsEmpty returns true if the buffer is empty.
func (*Buffer) IsFull ¶
IsFull returns true if the buffer has no more room to grow.
func (*Buffer) Read ¶
Read implements io.Reader.Read().
func (*Buffer) ReadFrom ¶
ReadFrom implements io.ReaderFrom.
func (*Buffer) ReadFullFrom ¶
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 ¶
Resize cuts the buffer at the given position.
func (*Buffer) SetByte ¶
SetByte sets the byte value at index.
func (*Buffer) String ¶
String returns the string form of this Buffer.
func (*Buffer) Write ¶
Write implements Write method in io.Writer.
func (*Buffer) WriteBytes ¶
WriteBytes appends one or more bytes to the end of the buffer.
type BufferToBytesWriter ¶
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 }
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) 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.
type BufferedWriter ¶
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 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 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 int32) 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() int32
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) ReadFrom ¶
func (mb *MultiBuffer) ReadFrom(reader io.Reader) (int64, error)
ReadFrom implements io.ReaderFrom.
func (*MultiBuffer) Release ¶
func (mb *MultiBuffer) Release()
Release releases all Buffers in the MultiBuffer.
func (*MultiBuffer) SliceBySize ¶
func (mb *MultiBuffer) SliceBySize(size int32) 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) String ¶
func (mb MultiBuffer) String() string
func (*MultiBuffer) Write ¶
func (mb *MultiBuffer) Write(b []byte) (int, error)
Write implements io.Writer.
func (*MultiBuffer) WriteMultiBuffer ¶
func (mb *MultiBuffer) WriteMultiBuffer(b MultiBuffer) error
WriteMultiBuffer implements Writer.
type 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.
type SequentialWriter ¶
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 ¶
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.