Documentation
¶
Index ¶
- type Buffer
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Cut(toBytes int) error
- func (b *Buffer) Grow(n int)
- func (b *Buffer) GrowFactor() int
- func (b *Buffer) Len() int
- func (b *Buffer) Next(n int) []byte
- func (b *Buffer) Pos() int
- func (b *Buffer) PosRead() int
- func (b *Buffer) PosWrite() int
- func (b *Buffer) Read(p []byte) (n int, err error)
- func (b *Buffer) ReadByte() (c byte, err error)
- func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
- func (b *Buffer) RemainingBytes() []byte
- func (b *Buffer) RemainingLen() int
- func (b *Buffer) Reset()
- func (b *Buffer) Seek(offset int64, whence int) (int64, error)
- func (b *Buffer) SeekRead(offset int64, whence int) (int64, error)
- func (b *Buffer) SeekWrite(offset int64, whence int) (int64, error)
- func (b *Buffer) SetGrowFactor(n int)
- func (b *Buffer) SetSeekRead()
- func (b *Buffer) SetSeekWrite()
- func (b *Buffer) Write(p []byte) (n int, err error)
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is variable-sized buffer of bytes with Read, Write and Seek methods. Underlying type is slice if bytes. It is possible to specify amount of bytes (named GrowFactor) on which underlying slice will be grown if needed. This capability helps to avoid unnecessary increasing underlying slice by 1 byte multiple times which may cause performance gap. Buffer implement io.ReadSeeker and io.WriteSeeker - behaviour of Seek method can be changed on runtime by SetSeekRead() and SetSeekWrite(). APIs of Buffer and bytes.Buffer are similar, main exception is that Buffer currently does not have Rune-related methods and Unread* methods.
func NewBuffer ¶
NewBuffer create new Buffer using buf as initially underlying slice of bytes. Grow factor will be set to 1. Seek behaviour will be "read".
func NewBufferDetail ¶
NewBufferDetail create new Buffer initially capacity of initCapacity and initially grow factor of growFactor. Seek behaviour will be "read". Passing growFactor < 1 cause panic. TODO what about zero and negative initCapacity
func (*Buffer) Bytes ¶
Bytes returns underlying buffer. If the caller changes the contents of the returned slice, the contents of the buffer will change provided there are no intervening method calls on the Buffer.
func (*Buffer) Cut ¶
Cut cuts buffer to toBytes bytes. Cut to negative number of bytes or to size greater than Len cause error.
func (*Buffer) Grow ¶
Grow grows the buffer's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to the buffer without another allocation. If n is negative, Grow will panic. If the buffer can't grow it will panic with ErrTooLarge.
func (*Buffer) GrowFactor ¶
GrowFactor returns effective GrowFactor.
func (*Buffer) Next ¶
Next returns a slice containing the next n bytes from the buffer, advancing the buffer as if the bytes had been returned by Read. If there are fewer than n bytes in the buffer, Next returns the entire buffer. The slice is only valid until the next some call resize underlying buffer.
func (*Buffer) Pos ¶
Pos return current read or write offset - Pos share his behaviour with Seek. This behaviour can be changed by SetSeekRead and SetSeekWrite methods. Technically Pos return PosRead() or PosWrite() result.
func (*Buffer) Read ¶
Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil.
func (*Buffer) ReadByte ¶
ReadByte reads and returns the next byte from the buffer. If no byte is available, it returns error io.EOF.
func (*Buffer) ReadFrom ¶
ReadFrom reads data from r until EOF and appends it to the buffer, growing the buffer as needed. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned. If the buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (*Buffer) RemainingBytes ¶
RemainingBytes returns a slice of the contents of the unread portion of the buffer. If the caller changes the contents of the returned slice, the contents of the buffer will change provided there are no intervening method calls on the Buffer.
func (*Buffer) RemainingLen ¶
RemainingLen returns the number of bytes of the unread portion of the buffer. Usually b.RemainingLen() == len(b.RemainingBytes()). But it is possible to set read position (using Seek[Read]) to yet not written position of buffer (readPosition > Buffer.len) and in that case len(b.RemainingBytes()) will be 0 and b.RemainingLen() will be negative.
func (*Buffer) Reset ¶
func (b *Buffer) Reset()
Reset resets the buffer to be empty and set read and write positions to zero, but it retains the underlying storage for use by future writes. TODO check capacity
func (*Buffer) Seek ¶
Seek implements the io.Seeker interface. It call SeekRead or SeekWrite depend on seek behaviour. Seek behaviour can be changed by SetSeekRead and SetSeekWrite methods.
func (*Buffer) SeekRead ¶
SeekRead change current read position. It is possible (and even does not error) to set position "into the future" (to position yet not written and even overlap end of underlying slice). offset define offset in bytes (can be negative). whence define offset base and can be 0 (offset from the beginning of buffer), 1 (offset from current read position) and 2 (offset from the end of buffer). Error will be returned if combination of offset and whence cause to negative resulting read position (i.e. SeekRead(-1,0)). Error also happened if resulting read position > MaxInt (but offset and result is of int64 type). Using unknown whence (not 0, 1, or 2) cause to error. SeekRead return new absolute read position (if not error happens) and error.
func (*Buffer) SeekWrite ¶
SeekWrite change current write position. It is analog of SeekRead (see its documentation). Additionally to SeekRead behaviour SeekWrite grow buffer if resulting write position require it.
func (*Buffer) SetGrowFactor ¶
SetGrowFactor change active grow factor. Grow factor is amount of bytes on which underlying slice of bytes will be enlarge each time it is required. Pass n < 1 cause panic.
func (*Buffer) SetSeekRead ¶
func (b *Buffer) SetSeekRead()
SetSeekRead set Seek behaviour. Subsequents call to Seek will seek for read position. SetSeekRead also change Pos behaviour - it will return read position.
func (*Buffer) SetSeekWrite ¶
func (b *Buffer) SetSeekWrite()
SetSeekWrite set Seek behaviour. Subsequents call to Seek will seek for write position. SetSeekWrite also change Pos behaviour - it will return write position.
func (*Buffer) Write ¶
Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with ErrTooLarge.
func (*Buffer) WriteByte ¶
WriteByte appends the byte c to the buffer, growing the buffer as needed. The returned error is always nil, but is included to match bufio.Writer's WriteByte. If the buffer becomes too large, WriteByte will panic with ErrTooLarge.