Documentation ¶
Index ¶
- Constants
- Variables
- func CeilToPowerOfTwo(n int) int
- func StringToBytes(s string) (b []byte)
- type Buffer
- func (rb *Buffer) Available() int
- func (rb *Buffer) Buffered() int
- func (rb *Buffer) Bytes() []byte
- func (rb *Buffer) Cap() int
- func (rb *Buffer) CopyFromSocket(fd int) (n int, err error)
- func (rb *Buffer) Discard(n int) (discarded int, err error)
- func (rb *Buffer) IsEmpty() bool
- func (rb *Buffer) IsFull() bool
- func (rb *Buffer) Len() int
- func (rb *Buffer) Peek(n int) (head []byte, tail []byte)
- func (rb *Buffer) Read(p []byte) (n int, err error)
- func (rb *Buffer) ReadByte() (b byte, err error)
- func (rb *Buffer) ReadFrom(r io.Reader) (n int64, err error)
- func (rb *Buffer) Reset()
- func (rb *Buffer) Rewind() (n int)
- func (rb *Buffer) Write(p []byte) (n int, err error)
- func (rb *Buffer) WriteByte(c byte) error
- func (rb *Buffer) WriteString(s string) (int, error)
- func (rb *Buffer) WriteTo(w io.Writer) (int64, error)
Constants ¶
const ( // MinRead is the minimum slice size passed to a Read call by // Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond // what is required to hold the contents of r, ReadFrom will not grow the // underlying buffer. MinRead = 512 // DefaultBufferSize is the first-time allocation on a ring-buffer. DefaultBufferSize = 1024 // 1KB )
Variables ¶
var ErrIsEmpty = errors.New("ring-buffer is empty")
ErrIsEmpty will be returned when trying to read an empty ring-buffer.
Functions ¶
func CeilToPowerOfTwo ¶
CeilToPowerOfTwo returns n if it is a power-of-two, otherwise the next-highest power-of-two.
func StringToBytes ¶
StringToBytes converts string to a byte slice without memory allocation.
Note it may break if the implementation of string or slice header changes in the future go versions.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a circular buffer that implement io.ReaderWriter interface.
func (*Buffer) Bytes ¶
Bytes returns all available read bytes. It does not move the read pointer and only copy the available data.
func (*Buffer) CopyFromSocket ¶
CopyFromSocket copies data from a socket fd into ring-buffer.
func (*Buffer) Peek ¶
Peek returns the next n bytes without advancing the read pointer, it returns all bytes when n <= 0.
func (*Buffer) Read ¶
Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more. When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.
func (*Buffer) Rewind ¶
Rewind moves the data from its tail to head and rewind its pointers of read and write.
func (*Buffer) Write ¶
Write writes len(p) bytes from p to the underlying buf. It returns the number of bytes written from p (n == len(p) > 0) and any error encountered that caused the write to stop early. If the length of p is greater than the writable capacity of this ring-buffer, it will allocate more memory to this ring-buffer. Write must not modify the slice data, even temporarily.
func (*Buffer) WriteString ¶
WriteString writes the contents of the string s to buffer, which accepts a slice of bytes.