byteshelper

package module
v0.0.0-...-d2dd6c5 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2016 License: Apache-2.0 Imports: 4 Imported by: 0

README

byteshelper

Build Status Coverage Status GoDoc

GoLang "bytes" package helpers.

Documentation

Index

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

func NewBuffer(buf []byte) *Buffer

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

func NewBufferDetail(initCapacity, growFactor int) (b *Buffer)

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

func (b *Buffer) Bytes() []byte

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

func (b *Buffer) Cut(toBytes int) error

Cut cuts buffer to toBytes bytes. Cut to negative number of bytes or to size greater than Len cause error.

func (*Buffer) Grow

func (b *Buffer) Grow(n int)

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

func (b *Buffer) GrowFactor() int

GrowFactor returns effective GrowFactor.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns len of underlying buffer; b.Len() == len(b.Bytes()).

func (*Buffer) Next

func (b *Buffer) Next(n int) []byte

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

func (b *Buffer) Pos() int

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

func (b *Buffer) PosRead() int

PosRead return current read offset (in bytes).

func (*Buffer) PosWrite

func (b *Buffer) PosWrite() int

PosWrite return current write offset (in bytes)

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err error)

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

func (b *Buffer) ReadByte() (c byte, err error)

ReadByte reads and returns the next byte from the buffer. If no byte is available, it returns error io.EOF.

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

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

func (b *Buffer) RemainingBytes() []byte

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

func (b *Buffer) RemainingLen() int

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

func (b *Buffer) Seek(offset int64, whence int) (int64, error)

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

func (b *Buffer) SeekRead(offset int64, whence int) (int64, error)

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

func (b *Buffer) SeekWrite(offset int64, whence int) (int64, error)

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

func (b *Buffer) SetGrowFactor(n int)

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

func (b *Buffer) Write(p []byte) (n int, err error)

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

func (b *Buffer) WriteByte(c byte) error

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.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes data to w until the buffer is drained or an error occurs. The return value n is the number of bytes written; it always fits into an int, but it is int64 to match the io.WriterTo interface. Any error encountered during the write is also returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL