Table of Contents

Class StreamExtensions

Namespace
Nerdbank.Streams
Assembly
Nerdbank.Streams.dll

Stream extension methods.

public static class StreamExtensions
Inheritance
StreamExtensions
Inherited Members

Methods

AsStream(IBufferWriter<byte>)

Creates a writable Stream that can be used to add to a IBufferWriter<T> of byte.

public static Stream AsStream(this IBufferWriter<byte> writer)

Parameters

writer IBufferWriter<byte>

The buffer writer the stream should write to.

Returns

Stream

A Stream.

AsStream(ReadOnlySequence<byte>)

Exposes a ReadOnlySequence<T> of byte as a Stream.

public static Stream AsStream(this ReadOnlySequence<byte> readOnlySequence)

Parameters

readOnlySequence ReadOnlySequence<byte>

The sequence of bytes to expose as a stream.

Returns

Stream

The readable stream.

AsStream(ReadOnlySequence<byte>, Action<object?>?, object?)

Exposes a ReadOnlySequence<T> of byte as a Stream.

public static Stream AsStream(this ReadOnlySequence<byte> readOnlySequence, Action<object?>? disposeAction, object? disposeActionArg)

Parameters

readOnlySequence ReadOnlySequence<byte>

The sequence of bytes to expose as a stream.

disposeAction Action<object>

A delegate to invoke when the returned stream is disposed. This might be useful to recycle the buffers backing the readOnlySequence.

disposeActionArg object

The argument to pass to disposeAction.

Returns

Stream

The readable stream.

AsStream(WebSocket)

Exposes a WebSocket as a Stream.

public static Stream AsStream(this WebSocket webSocket)

Parameters

webSocket WebSocket

The WebSocket to use as a transport for the returned Stream.

Returns

Stream

A bidirectional Stream.

ReadBlockAsync(Stream, Memory<byte>, CancellationToken)

Fills a given buffer with bytes from the specified Stream.

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

Parameters

stream Stream

The stream to read from.

buffer Memory<byte>

The buffer to fill from the stream.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<int>

A task that represents the asynchronous read operation. Its resulting value contains the total number of bytes read into the buffer. The result value can be less than the length of the given buffer if the end of the stream has been reached.

Remarks

The returned task does not complete until either the buffer is filled or the end of the stream has been reached.

Exceptions

OperationCanceledException

May be thrown if cancellationToken is canceled before reading has completed.

ReadBlockOrThrowAsync(Stream, Memory<byte>, CancellationToken)

Fills a given buffer with bytes from the specified Stream or throws if the end of the stream is reached first.

public static ValueTask ReadBlockOrThrowAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default)

Parameters

stream Stream

The stream to read from.

buffer Memory<byte>

The buffer to fill from the stream.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask

A task that represents the asynchronous read operation.

Remarks

The returned task does not complete until either the buffer is filled or the end of the stream has been reached.

Exceptions

OperationCanceledException

May be thrown if cancellationToken is canceled before reading has completed.

EndOfStreamException

Thrown if the end of the stream is encountered before filling the buffer.

ReadSlice(Stream, long)

Creates a Stream that can read no more than a given number of bytes from an underlying stream.

public static Stream ReadSlice(this Stream stream, long length)

Parameters

stream Stream

The stream to read from.

length long

The number of bytes to read from the parent stream.

Returns

Stream

A stream that ends after length bytes are read.

ReadSubstream(Stream)

Create a new StreamReader that will read a sequence previously written to this stream using WriteSubstream(Stream, int).

public static Stream ReadSubstream(this Stream stream)

Parameters

stream Stream

The underlying stream to read from.

Returns

Stream

A stream that will read just to the end of the substream and then end.

WriteSubstream(Stream, int)

Create a new StreamWriter that can be used to write a byte sequence of undetermined length to some underlying Stream, such that it can later be read back as if it were a Stream of its own that ends at the end of this particular sequence.

public static Substream WriteSubstream(this Stream stream, int minimumBufferSize = 4096)

Parameters

stream Stream

The underlying stream to write to.

minimumBufferSize int

The buffer size to use.

Returns

Substream

The new Stream.

Remarks

Write to the returned Stream until the sub-stream is complete. Call DisposeAsync(CancellationToken) when done and resume writing to the parent stream as needed.