Package bb (bb.Buffer) provides a flexible, efficient I/O buffer implementation with advanced manipulation capabilities. It combines features of bytes.
Buffer, bytes.Reader, and adds powerful seeking, slicing, and streaming operations.
- ๐ฆ Dynamic buffer with configurable initial size
- ๐ Seekable - move read position forward/backward
- ๐ Multiple write methods - bytes, strings, runes, and direct writes
- ๐ Multiple read methods - bytes, strings, runes, delimited reads
- โ๏ธ Buffer slicing - fork sub-buffers without copying
- ๐งฎ Checksum calculation - built-in CRC32 support
- ๐ Stream operations - ReadFrom/WriteTo with chunked transfers
- ๐ฏ Position management - discard, resume, and truncate operations
- ๐งต Thread-safe design for single goroutine usage
go get go.osspkg.com/bbpackage main
import (
"fmt"
"go.osspkg.com/bb"
)
func main() {
// Create a new buffer
buf := bb.New(1024)
// Write data
buf.WriteString("Hello, ")
buf.WriteByte('W')
buf.WriteRune('o')
buf.Write([]byte("rld!"))
// Read data
data := make([]byte, 13)
buf.Read(data)
fmt.Println(string(data)) // Output: Hello, World!
// Reset and reuse
buf.Reset()
fmt.Println(buf.Len()) // Output: 0
}// Create empty buffer with default size (1024)
buf := bb.New(0)
// Create buffer with custom size
buf := bb.New(4096)
// Create buffer from existing bytes
data := []byte("existing data")
buf := bb.FromBytes(data)
// Buffer is ready to use
fmt.Printf("Size: %d, Position: %d\n", buf.Size(), buf.Index())buf := bb.New(1024)
// Write various types
buf.Write([]byte("bytes "))
buf.WriteString("string ")
buf.WriteByte('B')
buf.WriteRune('็')
// Write at specific position
buf.WriteAt([]byte("INSERT"), 5)
// Stream from reader
reader := strings.NewReader("stream data")
buf.ReadFrom(reader)buf := bb.FromBytes([]byte("Hello\nWorld\nGo"))
// Read bytes
b, _ := buf.ReadByte() // 'H'
// Read until delimiter
line, _ := buf.ReadBytes('\n') // "ello\n"
// Position now at 'W'
// Read rune
r, size, _ := buf.ReadRune() // 'W', 1
// Read string
str, _ := buf.ReadString('\n') // "orld\n"
// Read next field
field, _ := buf.NextField("Go") // returns data before "Go"buf := bb.FromBytes([]byte("Hello World"))
// Get current position
pos := buf.Index() // 0
// Read some data
buf.ReadByte() // Reads 'H', position = 1
// Seek to different positions
buf.Seek(0, bb.SeekStart) // Go to beginning
buf.Seek(-1, bb.SeekEnd) // Go to last byte
buf.Seek(5, bb.SeekCurr) // Move forward 5 bytes
// Discard bytes (move forward)
discarded := buf.Discard(10) // Returns actual bytes discarded
// Resume bytes (move backward)
resumed := buf.Resume(5) // Returns actual bytes resumedoriginal := bb.FromBytes([]byte("Hello World Buffer"))
// Create sub-buffer (data is copied)
sub := original.Fork(6, 5) // "World"
// Original remains unchanged
fmt.Println(original.String()) // "Hello World Buffer"
fmt.Println(sub.String()) // "World"buf := bb.FromBytes([]byte("Important data"))
// Calculate CRC32 checksum of entire buffer
sum := buf.Checksum(0, buf.Size())
// Calculate checksum of specific region
partial := buf.Checksum(5, 10)buf := bb.FromBytes([]byte("Hello ไธ็"))
// Truncate to 8 bytes (aware of UTF-8 boundaries)
buf.Truncate(8) // Safely truncates at rune boundaries
fmt.Println(buf.String()) // "Hello ไธ" (not corrupted)buf := bb.New(1024)
// Read from reader with custom chunk size
n, err := buf.ReadFromN(reader, 4096)
// Write to writer with custom chunk size
n, err := buf.WriteToN(writer, 1024)
// Default chunk size (MaxUint16)
buf.ReadFrom(reader)
buf.WriteTo(writer)type Buffer struct { ... }New(size int) *Buffer- Create new bufferFromBytes(b []byte) *Buffer- Create from existing bytes
Reset()- Reset buffer (with capacity optimization)Bytes() []byte- Get all bytesString() string- Get as stringSize() int- Total buffer sizeLen() int- Remaining bytes to readIndex() int- Current read positionTruncate(n int)- Truncate with UTF-8 awareness
Write(p []byte) (int, error)WriteString(s string) (int, error)WriteByte(b byte) errorWriteRune(r rune) (int, error)WriteAt(b []byte, off int64) (int, error)
Read(p []byte) (int, error)ReadAt(p []byte, off int64) (int, error)ReadByte() (byte, error)ReadRune() (rune, int, error)ReadBytes(delim byte) ([]byte, error)ReadString(delim byte) (string, error)ReadNextBytes(delim []byte) ([]byte, error)ReadNextString(delim string) (string, error)NextField(sep string) ([]byte, error)
Seek(offset int64, whence int) (int64, error)Discard(n int) intResume(n int) intNext(n int) []byte
Fork(offset, length int) *BufferChecksum(offset, length int) uint32
ReadFrom(r io.Reader) (int64, error)ReadFromN(r io.Reader, size int) (int64, error)WriteTo(w io.Writer) (int64, error)WriteToN(w io.Writer, size int) (int64, error)
SeekStart // Seek from start
SeekCurr // Seek from current position
SeekEnd // Seek from end- The buffer automatically grows as needed
- Reset with capacity optimization prevents excessive allocations
- UTF-8 aware truncation prevents character corruption
- Fork creates a copy - use for sub-buffers when needed
- Checksum uses optimized CRC32 table
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.