Documentation ¶
Index ¶
- type Reader
- func (r *Reader) Align(width int)
- func (r *Reader) CheckRemaining(needed int) error
- func (r *Reader) ReadByte() byte
- func (r *Reader) ReadRemaining() []byte
- func (r *Reader) ReadSlice(n int) []byte
- func (r *Reader) ReadString(n int) string
- func (r *Reader) ReadUint16() uint16
- func (r *Reader) ReadUint24() uint32
- func (r *Reader) ReadUint32() uint32
- func (r *Reader) ReadUint64() uint64
- func (r *Reader) Remaining() int
- func (r *Reader) Skip(n int)
- type SharedBuffer
- type Writer
- func (w *Writer) Align(width int)
- func (w *Writer) Bytes() []byte
- func (w *Writer) Capacity() int
- func (w *Writer) CheckCapacity(needed int) error
- func (w *Writer) Length() int
- func (w *Writer) Reset()
- func (w *Writer) Rewind(n int)
- func (w *Writer) WriteByte(v byte)
- func (w *Writer) WriteSlice(p []byte) error
- func (w *Writer) WriteString(s string) error
- func (w *Writer) WriteUint16(v uint16)
- func (w *Writer) WriteUint24(v uint32)
- func (w *Writer) WriteUint32(v uint32)
- func (w *Writer) WriteUint64(v uint64)
- func (w *Writer) ZeroPad(n int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
func (*Reader) Align ¶
Discard bytes up to the next multiple of width, e.g. Align(4) skips ahead until the next aligned 4-byte boundary.
func (*Reader) CheckRemaining ¶
func (*Reader) ReadRemaining ¶
func (*Reader) ReadString ¶
func (*Reader) ReadUint16 ¶
func (*Reader) ReadUint24 ¶
func (*Reader) ReadUint32 ¶
func (*Reader) ReadUint64 ¶
type SharedBuffer ¶
type SharedBuffer struct {
// contains filtered or unexported fields
}
A SharedBuffer represents a read-only byte buffer that may be accessed concurrently from multiple goroutines. When a SharedBuffer is passed to a consumer function, the consumer should process the bytes and Release() the buffer as quickly as possible. If the bytes cannot be processed quickly, the consumer should make a copy, Release(), then continue processing its local copy.
Sharing is managed by reference counting. Hold() increments the reference count by 1, Release() decrements it by 1. The done function is called when the count reaches 0.
Example usage:
func consumer(buf *SharedBuffer) { defer buf.Release() // Ensure the shared buffer will be released. data := buf.Bytes() // Process data... } func producer() { data := generateData() doneCh := make(chan struct{}) buf := NewSharedBuffer(data, len(consumers), func() { close(doneCh) }) for _, consume := consumers { go consume(buf) } // Wait for all consumers to finish. <-doneCh }
The goal is to avoid extraneous allocations/copies when a potentially large byte buffer needs to be consumed by multiple goroutines.
func NewSharedBuffer ¶
func NewSharedBuffer(data []byte, count int, done func()) *SharedBuffer
func (*SharedBuffer) Bytes ¶
func (buf *SharedBuffer) Bytes() []byte
Bytes returns the underlying byte buffer.
func (*SharedBuffer) Release ¶
func (buf *SharedBuffer) Release()
Decrements the hold count. When the hold count reaches zero, the underlying byte buffer will be released.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
func NewWriterSize ¶
func (*Writer) Align ¶
Pad with zeros up to the next multiple of width, e.g. Align(4) adds zero bytes until the next 4-byte boundary.
func (*Writer) CheckCapacity ¶
func (*Writer) WriteSlice ¶
Write the given bytes, if there is enough room.