Krypton.Buffers 1.1.0

There is a newer version of this package available.
See the version list below for details.
Install-Package Krypton.Buffers -Version 1.1.0
dotnet add package Krypton.Buffers --version 1.1.0
<PackageReference Include="Krypton.Buffers" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Krypton.Buffers --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Krypton.Buffers, 1.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Krypton.Buffers as a Cake Addin
#addin nuget:?package=Krypton.Buffers&version=1.1.0

// Install Krypton.Buffers as a Cake Tool
#tool nuget:?package=Krypton.Buffers&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Krypton.Buffers

Types

The library ships with four types:

  • 2 Readers: SpanBufferReader and MemoryBufferReader (both sharing the same API)
  • 2 Writers: GrowingSpanBuffer and GrowingMemoryBuffer (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)
  • String8 (string in utf8)
  • Bytes (ReadOnlySpan<byte>/ReadOnlyMemory<byte>)
  • Guid (System.Guid)

There is a corresponding Read/Write method for each. Data is written in little endian

Features

Safe Allocation Free Buffer Writing with GrowingSpanBuffer

Example 1:

var buf = new GrowingSpanBuffer(stackalloc byte[64]); // initial buffer exists on the stack
buf.WriteUInt64(0);
buf.WriteString8("test");
Socket.Write(buf.Data);

Example 2:

var buf = new GrowingSpanBuffer(stackalloc byte[8]); // initial buffer exists on the stack
buf.WriteUInt64(0);
buf.WriteUInt64(0); // we resize on the heap here
Socket.Write(buf.Data);

Writer Bookmarks

Bookmarks are used for reserving a set number of bytes and writing to them later

Example:

var buf = new GrowingSpanBuffer(stackalloc byte[64]);

// strs is an IEnumerable<string>. Lets write the count after we enumerate through it
ushort count = 0;
var countBookmark = buf.ReserveBookmark(sizeof(ushort));
foreach (var str in strs)
{
    buf.WriteString8(str);
    count += 1;
}

// Now we can write the count
buf.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 buf = new SpanBufferReader(data);

// Read 6 uint32s from the buffer
ReadOnlySpan<uint> ids = buf.ReadUInt32Slice(6);
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Krypton.Buffers:

Package Downloads
BeatTogether.Core.Messaging

Package Description

PocketSocket

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.3 96 3/5/2021
2.0.2 850 12/7/2020
2.0.1 158 12/2/2020
2.0.0 120 12/2/2020
1.1.0 655 5/6/2020
1.0.1 211 5/1/2020
1.0.0 182 5/1/2020