Skip to content

Commit 8a2db99

Browse files
committed
add CreateArrayBufferWriter methods to BufferFactory for non-pooled buffer writers
1 parent de6b7c2 commit 8a2db99

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

NCode.Buffers/BufferFactory.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,132 @@ public static Sequence<T> CreatePooledBufferWriter<T>(bool isSensitive, int mini
324324
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="minimumSpanLength"/> is negative.</exception>
325325
public static Sequence<byte> CreatePooledBufferWriter(bool isSensitive, int minimumSpanLength = 0)
326326
=> CreatePooledBufferWriter<byte>(isSensitive, minimumSpanLength);
327+
328+
/// <summary>
329+
/// Creates a new <see cref="ArrayBufferWriter{T}"/> with the default initial capacity.
330+
/// </summary>
331+
/// <typeparam name="T">The type of elements in the buffer. Must be an unmanaged value type.</typeparam>
332+
/// <returns>
333+
/// A new <see cref="ArrayBufferWriter{T}"/> instance that implements <see cref="IBufferWriter{T}"/>.
334+
/// </returns>
335+
/// <remarks>
336+
/// <para>
337+
/// The returned <see cref="ArrayBufferWriter{T}"/> is a simple, non-pooled buffer writer that uses
338+
/// a single contiguous array that grows as needed. Unlike <see cref="CreatePooledBufferWriter{T}"/>,
339+
/// this method does not provide secure memory handling.
340+
/// </para>
341+
/// <para>
342+
/// Use this method when you need a simple buffer writer for non-sensitive data and do not require
343+
/// the overhead of memory pooling.
344+
/// </para>
345+
/// </remarks>
346+
/// <example>
347+
/// <code>
348+
/// using var writer = BufferFactory.CreateArrayBufferWriter&lt;int&gt;();
349+
/// var span = writer.GetSpan(10);
350+
/// // Write data to span
351+
/// writer.Advance(10);
352+
/// // Access the written data
353+
/// ReadOnlySpan&lt;int&gt; data = writer.WrittenSpan;
354+
/// </code>
355+
/// </example>
356+
public static ArrayBufferWriter<T> CreateArrayBufferWriter<T>()
357+
where T : struct
358+
=> new();
359+
360+
/// <summary>
361+
/// Creates a new <see cref="ArrayBufferWriter{T}"/> with the specified initial capacity.
362+
/// </summary>
363+
/// <typeparam name="T">The type of elements in the buffer. Must be an unmanaged value type.</typeparam>
364+
/// <param name="initialCapacity">The initial capacity of the underlying buffer.</param>
365+
/// <returns>
366+
/// A new <see cref="ArrayBufferWriter{T}"/> instance that implements <see cref="IBufferWriter{T}"/>.
367+
/// </returns>
368+
/// <remarks>
369+
/// <para>
370+
/// The returned <see cref="ArrayBufferWriter{T}"/> is a simple, non-pooled buffer writer that uses
371+
/// a single contiguous array. Specifying an appropriate initial capacity can help reduce reallocations
372+
/// when the expected data size is known in advance.
373+
/// </para>
374+
/// <para>
375+
/// Unlike <see cref="CreatePooledBufferWriter{T}"/>, this method does not provide secure memory handling.
376+
/// Use this method when you need a simple buffer writer for non-sensitive data.
377+
/// </para>
378+
/// </remarks>
379+
/// <example>
380+
/// <code>
381+
/// using var writer = BufferFactory.CreateArrayBufferWriter&lt;int&gt;(1024);
382+
/// var span = writer.GetSpan(100);
383+
/// // Write data to span
384+
/// writer.Advance(100);
385+
/// // Access the written data
386+
/// ReadOnlySpan&lt;int&gt; data = writer.WrittenSpan;
387+
/// </code>
388+
/// </example>
389+
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="initialCapacity"/> is not positive.</exception>
390+
public static ArrayBufferWriter<T> CreateArrayBufferWriter<T>(int initialCapacity)
391+
where T : struct
392+
=> new(initialCapacity);
393+
394+
/// <summary>
395+
/// Creates a new <see cref="ArrayBufferWriter{T}"/> for bytes with the default initial capacity.
396+
/// </summary>
397+
/// <returns>
398+
/// A new <see cref="ArrayBufferWriter{T}"/> instance that implements <see cref="IBufferWriter{T}"/>.
399+
/// </returns>
400+
/// <remarks>
401+
/// <para>
402+
/// This is a convenience overload for the common case of working with byte buffers.
403+
/// The returned <see cref="ArrayBufferWriter{T}"/> is a simple, non-pooled buffer writer that uses
404+
/// a single contiguous array that grows as needed.
405+
/// </para>
406+
/// <para>
407+
/// Unlike <see cref="CreatePooledBufferWriter(bool, int)"/>, this method does not provide secure memory handling.
408+
/// Use this method when you need a simple buffer writer for non-sensitive byte data.
409+
/// </para>
410+
/// </remarks>
411+
/// <example>
412+
/// <code>
413+
/// using var writer = BufferFactory.CreateArrayBufferWriter();
414+
/// var span = writer.GetSpan(100);
415+
/// // Write byte data to span
416+
/// writer.Advance(100);
417+
/// // Access the written data
418+
/// ReadOnlySpan&lt;byte&gt; data = writer.WrittenSpan;
419+
/// </code>
420+
/// </example>
421+
public static ArrayBufferWriter<byte> CreateArrayBufferWriter()
422+
=> new();
423+
424+
/// <summary>
425+
/// Creates a new <see cref="ArrayBufferWriter{T}"/> for bytes with the specified initial capacity.
426+
/// </summary>
427+
/// <param name="initialCapacity">The initial capacity of the underlying buffer.</param>
428+
/// <returns>
429+
/// A new <see cref="ArrayBufferWriter{T}"/> instance that implements <see cref="IBufferWriter{T}"/>.
430+
/// </returns>
431+
/// <remarks>
432+
/// <para>
433+
/// This is a convenience overload for the common case of working with byte buffers.
434+
/// Specifying an appropriate initial capacity can help reduce reallocations when the expected
435+
/// data size is known in advance.
436+
/// </para>
437+
/// <para>
438+
/// Unlike <see cref="CreatePooledBufferWriter(bool, int)"/>, this method does not provide secure memory handling.
439+
/// Use this method when you need a simple buffer writer for non-sensitive byte data.
440+
/// </para>
441+
/// </remarks>
442+
/// <example>
443+
/// <code>
444+
/// using var writer = BufferFactory.CreateArrayBufferWriter(1024);
445+
/// var span = writer.GetSpan(100);
446+
/// // Write byte data to span
447+
/// writer.Advance(100);
448+
/// // Access the written data
449+
/// ReadOnlySpan&lt;byte&gt; data = writer.WrittenSpan;
450+
/// </code>
451+
/// </example>
452+
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="initialCapacity"/> is not positive.</exception>
453+
public static ArrayBufferWriter<byte> CreateArrayBufferWriter(int initialCapacity)
454+
=> new(initialCapacity);
327455
}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ A unified API for renting and creating secure memory buffers:
2626
- **Rent buffers** - Rent pooled buffers with optional secure zeroing on return
2727
- **Create pinned arrays** - Allocate GC-pinned arrays that are zeroed on disposal
2828
- **Create pooled buffer writers** - Build sequences incrementally with automatic secure disposal
29+
- **Create array buffer writers** - Simple, non-pooled buffer writers for general-purpose use
2930

3031
```csharp
3132
// Rent a sensitive buffer (pinned + zeroed on dispose)
@@ -40,6 +41,9 @@ Span<byte> pinnedBuffer = lifetime;
4041
using var writer = BufferFactory.CreatePooledBufferWriter<byte>(isSensitive: true);
4142
var span = writer.GetSpan(100);
4243
// Write data, then call writer.Advance(bytesWritten)
44+
45+
// Create a simple array buffer writer (non-sensitive data)
46+
var arrayWriter = BufferFactory.CreateArrayBufferWriter<byte>(1024);
4347
```
4448

4549
#### SecureMemoryPool&lt;T&gt;
@@ -226,3 +230,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
226230

227231
## Release Notes
228232
* v4.0.0 - Consolidated from other multiple projects.
233+
* v4.1.0 - Added `CreateArrayBufferWriter` methods to `BufferFactory` for simple, non-pooled buffer writers.

0 commit comments

Comments
 (0)