StreamExtensions Class

Provides extension methods for Stream.

public static class StreamExtensions

Methods

NameDescription
ReadToEnd(Stream)Reads all remaining bytes from the stream.
ReadToEndAsync(Stream, CancellationToken)Asynchronously reads all remaining bytes from the stream.
ToMemoryStreamAsync(Stream, CancellationToken)Asynchronously copies the stream to a new MemoryStream and returns it positioned at the beginning.
TryReadAll(Stream, byte[], int, int)Attempts to read all requested bytes from the stream into the buffer.
TryReadAll(Stream, Span<byte>)Attempts to read all requested bytes from the stream into the buffer span.
TryReadAllAsync(Stream, byte[], int, int, CancellationToken)Asynchronously attempts to read all requested bytes from the stream into the buffer.
TryReadAllAsync(Stream, Memory<byte>, CancellationToken)Asynchronously attempts to read all requested bytes from the stream into the buffer memory.

ReadToEnd(Stream)

StreamExtensions.cs

Reads all remaining bytes from the stream.

public static byte[] ReadToEnd(this Stream stream)

Parameters

stream Stream

Returns

byte[]

ReadToEndAsync(Stream, CancellationToken)

StreamExtensions.cs

Asynchronously reads all remaining bytes from the stream.

net10.0

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

net11.0

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

Parameters

stream Stream
cancellationToken CancellationToken

Returns

Task<byte[]>

ToMemoryStreamAsync(Stream, CancellationToken)

StreamExtensions.cs

Asynchronously copies the stream to a new MemoryStream and returns it positioned at the beginning.

net10.0

public static Task<MemoryStream> ToMemoryStreamAsync(this Stream stream, CancellationToken cancellationToken = default)

net11.0

public static Task<MemoryStream> ToMemoryStreamAsync(this Stream stream, CancellationToken cancellationToken = default)

Parameters

stream Stream
cancellationToken CancellationToken

Returns

Task<MemoryStream>

TryReadAll overloads

TryReadAll(Stream, byte[], int, int)

StreamExtensions.cs

Attempts to read all requested bytes from the stream into the buffer.

public static int TryReadAll(this Stream stream, byte[] buffer, int offset, int count)
Parameters
stream Stream
buffer byte[]
offset int
count int
Returns

int

TryReadAll(Stream, Span<byte>)

StreamExtensions.cs

Attempts to read all requested bytes from the stream into the buffer span.

public static int TryReadAll(this Stream stream, Span<byte> buffer)
Parameters
stream Stream
buffer Span<byte>
Returns

int

TryReadAllAsync overloads

TryReadAllAsync(Stream, byte[], int, int, CancellationToken)

StreamExtensions.cs

Asynchronously attempts to read all requested bytes from the stream into the buffer.

public static Task<int> TryReadAllAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken = default)
Parameters
stream Stream
buffer byte[]
offset int
count int
cancellationToken CancellationToken
Returns

Task<int>

TryReadAllAsync(Stream, Memory<byte>, CancellationToken)

StreamExtensions.cs

Asynchronously attempts to read all requested bytes from the stream into the buffer memory.

net10.0

public static Task<int> TryReadAllAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default)

net11.0

public static Task<int> TryReadAllAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default)
Parameters
stream Stream
buffer Memory<byte>
cancellationToken CancellationToken
Returns

Task<int>