StreamExtensions Class
Definition
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
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
streamis null.- NotSupportedException
Thrown when
streamdoes 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
streamStreamThe stream to read. Must not be null.
cancellationTokenCancellationTokenA 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
streamis 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
streamStreamThe stream to write to. Must not be null.
bytesReadOnlySpan<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
streamis null.- NotSupportedException
Thrown when
streamdoes 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
streamStreamThe stream to write to. Must not be null.
bytesReadOnlyMemory<byte>The bytes to write.
cancellationTokenCancellationTokenA 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
streamis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |