A Go package providing a resizable buffer implementation with convenient methods for writing and reading various data types.
The safebuffer package provides a ResizableBuffer type that allows for efficient buffer operations with automatic resizing. It supports writing various data types (integers, floats, strings, bytes) and includes both append and prepend operations.
All write operations return the buffer, allowing for convenient method chaining. Here are some examples:
buf := NewResizableBuffer(nil).
CopyString("Data: ").
Uint32(42, true).
CopyString(", ").
Float64(3.14159, true).
CRLF()// Create an empty buffer
buf := NewResizableBuffer(nil)
// Create a buffer with a pre-made buffer
initialSlice := []byte{1, 2, 3}
buf := NewResizableBuffer(initialSlice)// Write bytes
buf.CopyBytes([]byte{1, 2, 3})
// Write string
buf.CopyString("Hello, World!")
// Write single byte
buf.Byte(0xFF)
// Write CRLF (carriage return + line feed)
buf.CRLF()// Write uint16 (little endian)
buf.Uint16(1234, true)
// Write uint32 (big endian)
buf.Uint32(5678, false)
// Write uint64 (little endian)
buf.Uint64(9012, true)
// Write int16 (little endian)
buf.Int16(-1234, true)
// Write int32 (big endian)
buf.Int32(-5678, false)
// Write int64 (little endian)
buf.Int64(-9012, true)// Write float32 (little endian)
buf.Float32(3.14, true)
// Write float64 (big endian)
buf.Float64(3.14159, false)// Prepend bytes
buf.PrependBytes([]byte{1, 2, 3})
// Prepend string
buf.PrependString("Prefix")
// Prepend single byte
buf.PrependByte(0xFF)
// Prepend uint16 (little endian)
buf.PrependUint16(1234, true)
// Prepend uint32 (big endian)
buf.PrependUint32(5678, false)
// Prepend uint64 (little endian)
buf.PrependUint64(9012, true)
// Prepend int16 (little endian)
buf.PrependInt16(-1234, true)
// Prepend int32 (big endian)
buf.PrependInt32(-5678, false)
// Prepend int64 (little endian)
buf.PrependInt64(-9012, true)
// Prepend float32 (little endian)
buf.PrependFloat32(3.14, true)
// Prepend float64 (big endian)
buf.PrependFloat64(3.14159, false)// Get the current buffer contents
data := buf.Bytes()
// Get the current length
length := buf.Len()
// Reset the buffer (optionally zero out the contents)
buf.Reset(true) // true to zero out, false to keep contents
// Read from an io.Reader into the buffer
chunk, err := buf.ReadInto(reader, maxSize)
// Create a sub-buffer
subBuf := buf.SubBuffer(length) // length < 0 for remaining bufferNewResizableBuffer(b []byte) *ResizableBuffer- Creates a new resizable buffer, optionally with initial data
CopyBytes(p []byte) *ResizableBuffer- Copies bytes into the bufferCopyString(p string) *ResizableBuffer- Copies a string into the bufferByte(bt byte) *ResizableBuffer- Writes a single byteCRLF() *ResizableBuffer- Writes CRLF (carriage return + line feed)
Uint16(v uint16, littleEndian bool) *ResizableBuffer- Writes uint16Uint32(v uint32, littleEndian bool) *ResizableBuffer- Writes uint32Uint64(v uint64, littleEndian bool) *ResizableBuffer- Writes uint64Int16(v int16, littleEndian bool) *ResizableBuffer- Writes int16Int32(v int32, littleEndian bool) *ResizableBuffer- Writes int32Int64(v int64, littleEndian bool) *ResizableBuffer- Writes int64
Float32(v float32, littleEndian bool) *ResizableBuffer- Writes float32Float64(v float64, littleEndian bool) *ResizableBuffer- Writes float64
PrependBytes(v []byte) *ResizableBuffer- Prepends bytesPrependString(v string) *ResizableBuffer- Prepends a stringPrependByte(v byte) *ResizableBuffer- Prepends a bytePrependUint16(v uint16, littleEndian bool) *ResizableBuffer- Prepends uint16PrependUint32(v uint32, littleEndian bool) *ResizableBuffer- Prepends uint32PrependUint64(v uint64, littleEndian bool) *ResizableBuffer- Prepends uint64PrependInt16(v int16, littleEndian bool) *ResizableBuffer- Prepends int16PrependInt32(v int32, littleEndian bool) *ResizableBuffer- Prepends int32PrependInt64(v int64, littleEndian bool) *ResizableBuffer- Prepends int64PrependFloat32(v float32, littleEndian bool) *ResizableBuffer- Prepends float32PrependFloat64(v float64, littleEndian bool) *ResizableBuffer- Prepends float64
Bytes() []byte- Returns the current buffer contentsLen() int- Returns the current buffer lengthReset(zeroOut bool) *ResizableBuffer- Resets the buffer, optionally zeroing out contentsReadInto(r io.Reader, maxSize int) ([]byte, error)- Reads from an io.Reader into the bufferSubBuffer(length int) *ResizableBuffer- Creates a sub-buffer view of the current buffer
- The buffer automatically resizes when needed
- All write operations return the buffer for method chaining
- The buffer is not thread-safe
- Bytes returned by
Bytes()andReadInto()are only valid until the nextReset()call SubBuffer()creates a view into the current buffer, not a copy