Bodu.Buffers Namespace
- Package
-
Bodu.Core 1.0.1
Purpose
Bodu.Buffers ships the pooled buffer infrastructure used by the rest of the Bodu solution and exposed for consumer use. Reach for this namespace when you need to assemble a byte or character span without allocating an array per resize step.
Key types
- PooledBufferBuilder<T> -
ArrayPool<T>-backed builder for assembling spans without allocation. Returns the rented array to the pool onDispose. Writes go throughAppend(T),AppendRange(ReadOnlySpan<T>)(withReadOnlyMemory<T>/IEnumerable<T>overloads),AddMany, or theIBufferWriter<T>pairGetSpan(sizeHint)/GetMemory(sizeHint)+Advance(count); reads go throughWrittenSpan/WrittenMemory,CopyTo/TryCopyTo, orToArrayAndDispose(), which hands back a right-sized array and returns the rental in one step.
Example
using Bodu.Buffers;
using var builder = new PooledBufferBuilder<byte>(initialCapacity: 256);
builder.Append(0xDE);
builder.Append(0xAD);
ReadOnlySpan<byte> tail = stackalloc byte[] { 0xBE, 0xEF };
builder.AppendRange(tail);
// IBufferWriter<T> style: reserve, fill, commit.
Span<byte> slot = builder.GetSpan(4);
BinaryPrimitives.WriteUInt32LittleEndian(slot, 42);
builder.Advance(4);
ReadOnlySpan<byte> written = builder.WrittenSpan;
// builder rents from ArrayPool<byte>.Shared; Dispose returns the buffer.
// Alternatively, builder.ToArrayAndDispose() copies out the written bytes and disposes in one call.
Notes
- Disposable. The builder rents from
ArrayPool<T>.Shared. Dispose at the end of use;usingis the idiomatic pattern. - Single owner. The builder is not thread-safe - pool-backed buffers are owned by a single writer.
- See also: the Bodu.Core introduction, SegmentedBuffer<T> for streaming buffers where the total length is not known up front.
Classes
- PooledBufferBuilder<T>
Provides an efficient way to accumulate elements into a pooled buffer, with automatic resizing and fast-path optimizations for collection-based and span-based sources.