https://github.com/koykov/bytebuf
Collection of various byte buffer implementations.
https://github.com/koykov/bytebuf
buffer bytebuffer
Last synced: 17 days ago
JSON representation
Collection of various byte buffer implementations.
- Host: GitHub
- URL: https://github.com/koykov/bytebuf
- Owner: koykov
- License: mit
- Created: 2021-09-05T18:17:10.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-11-04T20:50:26.000Z (2 months ago)
- Last Synced: 2025-11-04T22:22:05.593Z (2 months ago)
- Topics: buffer, bytebuffer
- Language: Go
- Homepage:
- Size: 89.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# Byte buffers
Collection of various byte buffer implementations.
## Chain buffer
A wrapper around `[]byte` slice that supports chain write methods. Example of usage:
```go
import "github.com/koykov/bytebuf"
var buf bytebuf.Chain
b := buf.Write([]byte("foo")).
WriteString("bar").
WriteByte('@').
WriteInt(-123).
WriteUint(123).
WriteFloat(3.1415).
WriteBool(true).
WriteRune('X').
WriteFormat("pre%dpost", 15)
WriteX(1.23456).
Bytes()
println(string(b)) // foobar@-1231233.1415trueXpre15post1.23456
```
The same operations may be proceeded using `append()`/`AppendInt()`/... functions around `[]byte` slice, but `Chain`
provides handy API for that.
Chain buffer may be reused using internal pool. To get instance of the buffer from the pool call `AcquireChain` function.
To put buffer back to the pool call `ReleaseChain` function.
## Accumulative buffer
A wrapper around `Chain` buffer with "staked" features. The main idea is to accumulate (bufferize) various data and
provide handy access to chunks of buffered data. Example of usage:
```go
import "github.com/koykov/bytebuf"
var (
buf bytebuf.Chain
msg protobuf.ExampleMessage
)
chunk0 := buf.WriteMarshallerTo(&msg).Bytes()
chunk1 := buf.StakeOut().Write([]byte("foo")).
WriteString("bar").
WriteByte('@').
WriteInt(-123).
WriteUint(123).
WriteFloat(3.1415).
WriteBool(true).
WriteRune('X').
WriteFormat("pre%dpost", 15)
WriteX(1.23456).
StakedBytes()
println(string(chunk0)) // h�����,����?�ihttps
println(string(chunk1)) // foobar@-1231233.1415trueXpre15post1.23456
```
Thus, one buffer may be used to bufferize multiple data and hence reduce pointers in application.
Accumulative buffer may be reused using internal pool. To get instance of the buffer from the pool call `AcquireAccumulative`
function. To put buffer back to the pool call `ReleaseAccumulative` function.
# Pools
Both `Chain` and `Accumulative` buffers are using internal pools to reduce memory allocations.
Usage:
```go
cbuf := bytebuf.AcquireChain()
defer bytebuf.ReleaseChain(cbuf)
cbuf.WriteString("foo")
...
abuf := bytebuf.AcquireAccumulative()
defer bytebuf.ReleaseAccumulative(abuf)
abuf.WriteString("foo")
...
```