Table of Contents

Method ToMemory

Namespace
KZDev.PerfUtils
Assembly
KZDev.PerfUtils.dll

ToMemory()

Creates a pooled copy of the stream contents as an IMemoryOwner<T> of byte.

public virtual IMemoryOwner<byte> ToMemory()

Returns

IMemoryOwner<byte>

An IMemoryOwner<T> whose Memory spans the stream bytes.

Remarks

This overload uses Shared. Use ToMemory(MemoryPool<byte>) when you need to account rentals against a specific pool.

The returned owner materializes a contiguous copy of the stream from the beginning through Length, independent of Position, matching ToArray() behavior for content and observable size limits.

When Length is zero, the returned owner is a shared singleton with zero-length Memory; its Dispose() is a no-op and does not rent from any pool.

When the payload is non-empty, you must dispose the owner to return the rented buffer to the pool it came from. The owner's Memory exposes exactly the stream length (there is no visible trailing pool slack in that view).

After disposal, behavior matches ToArray() for the same stream mode: fixed-mode streams can still produce an owner when the underlying buffer remains available; dynamic-mode streams throw ObjectDisposedException.

This method is not thread-safe with respect to concurrent use of this stream. The returned IMemoryOwner<T> is not thread-safe either. Use Memory and Dispose() from a single thread (typical using pattern).

ToMemory(MemoryPool<byte>)

Creates a pooled copy of the stream contents using the specified MemoryPool<T>.

public virtual IMemoryOwner<byte> ToMemory(MemoryPool<byte> memoryPool)

Parameters

memoryPool MemoryPool<byte>

The pool used to rent the contiguous backing buffer (when Length is non-zero).

Returns

IMemoryOwner<byte>

An IMemoryOwner<T> whose Memory spans the stream bytes.

Remarks

The returned owner materializes a contiguous copy of the stream from the beginning through Length, independent of Position, matching ToArray() behavior for content and observable size limits.

When Length is zero, the returned owner is a shared singleton with zero-length Memory; its Dispose() is a no-op and does not rent from any pool.

When the payload is non-empty, you must dispose the owner to return the rented buffer to the pool it came from. The owner's Memory exposes exactly the stream length (there is no visible trailing pool slack in that view).

After disposal, behavior matches ToArray() for the same stream mode: fixed-mode streams can still produce an owner when the underlying buffer remains available; dynamic-mode streams throw ObjectDisposedException.

This method is not thread-safe with respect to concurrent use of this stream. The returned IMemoryOwner<T> is not thread-safe either. Use Memory and Dispose() from a single thread (typical using pattern).

Exceptions

ArgumentNullException

memoryPool is null.