BinaryBuffers 5.1.0

dotnet add package BinaryBuffers --version 5.1.0
                    
NuGet\Install-Package BinaryBuffers -Version 5.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BinaryBuffers" Version="5.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BinaryBuffers" Version="5.1.0" />
                    
Directory.Packages.props
<PackageReference Include="BinaryBuffers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BinaryBuffers --version 5.1.0
                    
#r "nuget: BinaryBuffers, 5.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BinaryBuffers@5.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BinaryBuffers&version=5.1.0
                    
Install as a Cake Addin
#tool nuget:?package=BinaryBuffers&version=5.1.0
                    
Install as a Cake Tool

BinaryBuffers

logo

NuGet

BinaryBuffers offers a highly performant implementation of BinaryReader and BinaryWriter, working directly on a byte array or ReadOnlyMemory<byte>, thus eliminating the need for an intermediate Stream object.

How to use

The library provides three types:

  • BinaryBufferReader — reads primitive data types from a byte[].
  • BinaryBufferWriter — writes primitive data types to a byte[].
  • BinaryBufferMemoryReader — reads primitive data types from a ReadOnlyMemory<byte>.
// Provide a buffer to the reader/writer
var buffer = new byte[100];

// Write to the buffer
var writer = new BinaryBufferWriter(buffer);

writer.Write(2026);
writer.Write(8.11);

// Read from the buffer
var reader = new BinaryBufferReader(buffer);

var year = reader.ReadInt32();
var time = reader.ReadDouble();

Bulk reading

When you need to read a run of values of the same type — e.g. a block of ints — ReadInto<T> reads them all in a single bounds-checked bulk copy instead of one call per element. On little-endian platforms it compiles down to a single memcpy, which is dramatically faster than calling ReadInt32() in a loop (see Benchmarks):

var reader = new BinaryBufferReader(buffer);

var values = new int[128];
reader.ReadInto<int>(values);   // reads 128 consecutive Int32s in one shot

There is also a generic single-value Read<T>() for any unmanaged type:

var year = reader.Read<int>();
var time = reader.Read<double>();

Both APIs are intended for primitive numeric types. On big-endian platforms each value is byte-swapped, which is correct for integer and floating-point types but not for composite layouts such as decimal (use the dedicated ReadDecimal() for those).

Reading from a ReadOnlyMemory<byte>

When your data lives in a ReadOnlyMemory<byte>, use BinaryBufferMemoryReader:

ReadOnlyMemory<byte> memory = buffer;

var reader = new BinaryBufferMemoryReader(memory);

var year = reader.ReadInt32();
var time = reader.ReadDouble();

Constructor overloads

BinaryBufferReader and BinaryBufferWriter can both be constrained to a sub-region of the underlying array by supplying an offset and length:

// Read/write within the boundaries [offset, offset + length)
var reader = new BinaryBufferReader(buffer, offset: 10, length: 40);
var writer = new BinaryBufferWriter(buffer, offset: 10, length: 40);

BinaryBufferReader additionally accepts an ArraySegment<byte>, which carries its own offset and count:

var segment = new ArraySegment<byte>(buffer, 10, 40);
var reader = new BinaryBufferReader(segment);

BinaryBufferMemoryReader is constructed from a ReadOnlyMemory<byte>; slice the memory beforehand to read from a sub-region:

var reader = new BinaryBufferMemoryReader(memory.Slice(10, 40));

Benchmarks

Benchmarks show an average of 80% improvement in reading and 84% in writing.

Performance tests were executed using .NET 10 running on a machine with a 16-core CPU.

BinaryBufferReader

Method Mean Error StdDev Ratio
BinaryReader_ReadInt 16.633 ms 0.0406 ms 0.0380 ms baseline
BufferReader_ReadInt 3.607 ms 0.0020 ms 0.0019 ms -78%
BinaryReader_ReadDecimal 14.333 ms 0.0129 ms 0.0121 ms baseline
BufferReader_ReadDecimal 2.966 ms 0.0318 ms 0.0297 ms -79%
BinaryReader_ReadFloat 11.666 ms 0.0177 ms 0.0166 ms baseline
BufferReader_ReadFloat 2.012 ms 0.0011 ms 0.0011 ms -83%

Reading a block of 128 Int32s, bulk ReadInto<int> vs. a per-element ReadInt32() loop:

Method Mean Ratio
PerElement_ReadInt32_Loop 12,017.4 us baseline
Bulk_ReadInto 701.0 us -94%

BinaryBufferWriter

Method Mean Error StdDev Ratio
BinaryWriter_WriteInt 39.373 ms 0.0547 ms 0.0511 ms baseline
BufferWriter_WriteInt 5.602 ms 0.0375 ms 0.0350 ms -86%
BinaryWriter_WriteDecimal 27.106 ms 0.0171 ms 0.0160 ms baseline
BufferWriter_WriteDecimal 4.421 ms 0.0058 ms 0.0052 ms -84%
BinaryWriter_WriteFloat 20.406 ms 0.0205 ms 0.0192 ms baseline
BufferWriter_WriteFloat 3.379 ms 0.0083 ms 0.0078 ms -83%
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BinaryBuffers:

Package Downloads
LiteCDF

A high performance compound document format (CDF) reader.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.1.0 63 5/29/2026
5.0.0 54 5/29/2026
4.1.0 182 12/11/2025
4.0.0 319 11/16/2025
3.0.1 323 6/13/2024
2.0.2 1,276 6/19/2022