Soenneker.Utils.MemoryStream 4.0.1525

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Soenneker.Utils.MemoryStream --version 4.0.1525
                    
NuGet\Install-Package Soenneker.Utils.MemoryStream -Version 4.0.1525
                    
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="Soenneker.Utils.MemoryStream" Version="4.0.1525" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Utils.MemoryStream" Version="4.0.1525" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Utils.MemoryStream" />
                    
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 Soenneker.Utils.MemoryStream --version 4.0.1525
                    
#r "nuget: Soenneker.Utils.MemoryStream, 4.0.1525"
                    
#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 Soenneker.Utils.MemoryStream@4.0.1525
                    
#: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=Soenneker.Utils.MemoryStream&version=4.0.1525
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Utils.MemoryStream&version=4.0.1525
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image Soenneker.Utils.MemoryStream

An easy modern MemoryStream utility

A library for management and simple access of RecyclableMemoryStreamManager

Installation

dotnet add package Soenneker.Utils.MemoryStream

Registration

using Soenneker.Utils.MemoryStream.Registrars;

services.AddMemoryStreamUtilAsSingleton();

The manager is designed to be shared, so singleton registration is the normal choice. A scoped registrar is also available when the utility itself must follow a scope.

Rent and return a stream

Inject IMemoryStreamUtil, then dispose every returned stream promptly so its buffers return to the manager:

using Soenneker.Utils.MemoryStream.Abstract;
using System.Text;

public sealed class PayloadReader
{
    private readonly IMemoryStreamUtil _memoryStreams;

    public PayloadReader(IMemoryStreamUtil memoryStreams)
    {
        _memoryStreams = memoryStreams;
    }

    public async ValueTask<string> ReadFile(string path, CancellationToken cancellationToken)
    {
        await using FileStream source = File.OpenRead(path);
        await using MemoryStream buffer = await _memoryStreams.Get(cancellationToken);

        await source.CopyToAsync(buffer, cancellationToken);
        buffer.Position = 0;

        using var reader = new StreamReader(buffer, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true);
        return await reader.ReadToEndAsync(cancellationToken);
    }
}

Get()/GetSync() return a fresh empty recyclable MemoryStream. Overloads accepting bytes or text populate the stream and leave its position at zero; strings and character memory are encoded as UTF-8. The asynchronous overloads are useful while the shared manager is being initialized, but the returned stream is still a normal synchronous MemoryStream.

Copy a stream to bytes

byte[] remaining = await memoryStreams.GetBytesFromStream(
    source,
    keepOpen: true,
    cancellationToken);

Conversion begins at the input stream's current position. The default keepOpen: false transfers ownership to the utility and disposes the input even when copying fails; pass true when the caller must continue using it. A MemoryStream is copied without changing its position. Other stream types are consumed through CopyToAsync, so their position advances.

The byte-array result is a separate allocation and remains valid after any source or recyclable stream is disposed. GetManager is exposed for advanced Microsoft.IO configuration or APIs, but ordinary callers should rent streams through the utility.

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.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on Soenneker.Utils.MemoryStream:

Package Downloads
Soenneker.Utils.File

A utility library encapsulating asynchronous file IO operations

Soenneker.Cosmos.Serializer

A fast, lightweight JSON (de)serializer for Azure Cosmos DB

Soenneker.Extensions.HttpContent

A collection of helpful HttpContent extension methods

Soenneker.Blob.Download

A utility library for Azure Blob download operations

Soenneker.Blob.Upload

A utility library for Azure Blob upload operations

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.1526 0 8/31/2026
4.0.1525 51 8/31/2026
4.0.1524 150 8/31/2026
4.0.1523 26 8/31/2026
4.0.1522 110 8/30/2026
4.0.1521 34 8/30/2026
4.0.1520 1,120 8/30/2026
4.0.1519 2,847 8/30/2026
4.0.1518 709 8/30/2026
4.0.1516 2,144 8/30/2026
4.0.1514 1,667 8/29/2026
4.0.1512 5,466 8/29/2026
4.0.1511 34 8/29/2026
4.0.1510 20,771 8/26/2026
4.0.1509 43 8/26/2026
4.0.1508 1,088 8/25/2026
4.0.1507 15,487 8/21/2026
4.0.1506 18,265 8/18/2026
4.0.1505 4,544 8/18/2026
4.0.1504 16,599 8/12/2026
Loading failed

Update dependency Soenneker.Extensions.String to 4.0.734 (#1718)