Table of Contents

StreamExtensions Class

Definition

Namespace
Bodu.Extensions
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
StreamExtensions.ReadAllBytes.cs

Provides whole-payload read and write helpers for Stream - synchronous and asynchronous operations that consume or emit an entire byte buffer in a single call.

public static class StreamExtensions
Inheritance
StreamExtensions
Inherited Members

Remarks

Stream exposes only incremental, partial-progress primitives: a single Read(byte[], int, int) call may return fewer bytes than requested, so reading a stream to its end correctly requires a loop. These helpers encapsulate that loop together with the seekable and non-seekable buffering distinction, so callers can treat a stream as a single byte payload.

When the stream is seekable the exact remaining length is used to allocate a single right-sized buffer; when it is not, the content is accumulated through a growable buffer. Every helper reads from or writes at the current position and leaves the stream open.

Methods

ReadAllBytes(Stream)

Reads stream from its current position to the end and returns the bytes as an array.

public static byte[] ReadAllBytes(this Stream stream)

Parameters

stream Stream

The stream to read. Must not be null.

Returns

byte[]

A new array containing every byte from the current position to the end of stream, or an empty array when no bytes remain.

Remarks

When stream is seekable a single buffer sized to the remaining length is allocated; otherwise the content is accumulated through an intermediate MemoryStream. The read begins at the current position and the stream is left open.

Exceptions

ArgumentNullException

Thrown when stream is null.

NotSupportedException

Thrown when stream does not support reading.

ReadAllBytesAsync(Stream, CancellationToken)

Asynchronously reads stream from its current position to the end and returns the bytes as an array.

public static Task<byte[]> ReadAllBytesAsync(this Stream stream, CancellationToken cancellationToken = default)

Parameters

stream Stream

The stream to read. Must not be null.

cancellationToken CancellationToken

A token used to observe cancellation requests.

Returns

Task<byte[]>

A task that completes with a new array containing every byte from the current position to the end of stream, or an empty array when no bytes remain.

Remarks

Argument validation runs synchronously, so a null stream faults at the call site rather than on the returned task. See ReadAllBytes(Stream) for the buffering strategy.

Exceptions

ArgumentNullException

Thrown when stream is null.

WriteAllBytes(Stream, ReadOnlySpan<byte>)

Writes the entire contents of bytes to stream at its current position.

public static void WriteAllBytes(this Stream stream, ReadOnlySpan<byte> bytes)

Parameters

stream Stream

The stream to write to. Must not be null.

bytes ReadOnlySpan<byte>

The bytes to write.

Remarks

The write begins at the current position; the stream is neither flushed nor closed by this method.

Exceptions

ArgumentNullException

Thrown when stream is null.

NotSupportedException

Thrown when stream does not support writing.

WriteAllBytesAsync(Stream, ReadOnlyMemory<byte>, CancellationToken)

Asynchronously writes the entire contents of bytes to stream at its current position.

public static Task WriteAllBytesAsync(this Stream stream, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default)

Parameters

stream Stream

The stream to write to. Must not be null.

bytes ReadOnlyMemory<byte>

The bytes to write.

cancellationToken CancellationToken

A token used to observe cancellation requests.

Returns

Task

A task that completes when the write has finished.

Remarks

Argument validation runs synchronously, so a null stream faults at the call site rather than on the returned task. The stream is neither flushed nor closed by this method.

Exceptions

ArgumentNullException

Thrown when stream is null.

Applies to

ProductVersions
.NET8, 10