Krypton.Buffers
2.0.0
See the version list below for details.
dotnet add package Krypton.Buffers --version 2.0.0
NuGet\Install-Package Krypton.Buffers -Version 2.0.0
<PackageReference Include="Krypton.Buffers" Version="2.0.0" />
paket add Krypton.Buffers --version 2.0.0
#r "nuget: Krypton.Buffers, 2.0.0"
// Install Krypton.Buffers as a Cake Addin #addin nuget:?package=Krypton.Buffers&version=2.0.0 // Install Krypton.Buffers as a Cake Tool #tool nuget:?package=Krypton.Buffers&version=2.0.0
Krypton.Buffers
Types
The library ships with four types:
- 2 Readers: SpanBufferReader and MemoryBufferReader (both sharing the same API)
- 2 Writers: SpanBufferWriter and MemoryBufferWriter (both sharing the same API)
Currently the readers/writers support the following types:
- Bool (bool stored as a byte)
- Int8 (byte)
- UInt8 (sbyte)
- Int16 (short)
- UInt16 (ushort)
- Int32 (int)
- UInt32 (uint)
- Int64 (long)
- UInt64 (ulong)
- Float32 (float)
- Float64 (double)
- String (any encoding, UTF8 and UTF16 helpers, shown in examples)
- Bytes (ReadOnlySpan<byte>/ReadOnlyMemory<byte>)
- Guid (System.Guid)
There is a corresponding Read/Write method for each. Data is written in little endian
Buffer Options
By default all buffers are set to resize. The default pooling strategy is no pooling at all, each time the writer needs to resize a new byte array is allocated. If you want to write to a fixed size buffer without any resizing you can do so.
try
{
using var bufferWriter = new SpanBufferWriter(someFixedSizeBuffer, resize: false);
bufferWriter.WriteUTF8String("I hope there is enough space for this");
}
catch (OutOfSpaceException)
{
// Looks like there wasnt...
}
There is also an exception you can handle when reading from a buffer
try
{
var bufferReader = new SpanBufferReader(someBuffer);
var str = bufferReader.ReadString(Encoding.Unicode);
var randomBytes = bufferReader.ReadBytes(462); // I hope there are 462 bytes to read
}
catch (EndOfBufferException)
{
// Looks like there wasnt...
}
There is more info on the pooling strategies below.
Features
Safe Allocation Free Buffer Writing with SpanBufferWriter
Example 1:
using var bufferWriter = new SpanBufferWriter(stackalloc byte[64]); // initial buffer exists on the stack
bufferWriter.WriteUInt64(0);
bufferWriter.WriteUTF8String("test");
Socket.Write(bufferWriter.Data);
Example 2:
using var bufferWriter = new SpanBufferWriter(stackalloc byte[8]); // initial buffer exists on the stack
bufferWriter.WriteUInt64(0);
bufferWriter.WriteUInt64(0); // we resize on the heap here
Socket.Write(bufferWriter); // implicit ReadOnlySpan<byte> cast
Writer Bookmarks
Bookmarks are used for reserving a set number of bytes and writing to them later
Example:
using var bufferWriter = new SpanBufferWriter(stackalloc byte[64]);
// strs is an IEnumerable<string>. Lets write the count after we enumerate through it
ushort count = 0;
var countBookmark = bufferWriter.ReserveBookmark(sizeof(ushort));
foreach (var str in strs)
{
bufferWriter.WriteString(str, Encoding.Unicode);
count += 1;
}
// Now we can write the count
bufferWriter.WriteBookmark(countBookmark, count, BinaryPrimitives.WriteUInt16LittleEndian);
Reading Int Slices
The readers support reading slices of the following types:
- Int16
- UInt16
- Int32
- UInt32
- Int64
- UInt64
There is a Read{type}Slice method for each.
This method is allocation free on little endian machines.
Example:
var bufferReader = new SpanBufferReader(data);
// Read 6 uint32s from the buffer
ReadOnlySpan<uint> ids = bufferReader.ReadUInt32Slice(6);
Configurable Array Pooling
Here is an example implementation of a pooling strategy that will use ArrayPool whenever the buffer writer needs to resize.
public class ExamplePoolingStrategy : IPoolingStrategy
{
public const int GrowthFactor = 2;
public static readonly IPoolingStrategy Instance = new ExamplePoolingStrategy();
private readonly ArrayPool<byte> _arrayPool = = ArrayPool<byte>.Create();
private ExamplePoolingStrategy()
{
}
// This gets called whenever the writer needs to resize
public byte[] Resize(int size, int neededSize)
{
var newLength = size * GrowthFactor;
while (neededSize > newLength)
newLength *= GrowthFactor;
return _arrayPool.Rent(newLength);
}
// This gets called whenever the writer is done with the array it rented
public void Free(byte[] rented)
{
_arrayPool.Return(rented);
}
}
using var bufferWriter = new SpanBufferWriter(stackalloc byte[4], poolingStrategy: ExamplePoolingStrategy.Instance);
bufferWriter.WriteUInt32(4); // this gets written to the initial buffer that exists on the stack
bufferWriter.WriteUTF16String("hello heap!"); // the buffer resizes using ArrayPool and the data gets relocated on to the heap
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Krypton.Buffers:
Package | Downloads |
---|---|
BeatTogether.Core.Messaging
Package Description |
|
BeatTogether.LiteNetLib
Package Description |
|
PocketSocket
Package Description |
|
DcSharp
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.