Table of Contents

Class StreamPipeReader

Namespace
Nerdbank.Streams
Assembly
Nerdbank.Streams.dll

A PipeReader that reads from an underlying Stream exactly when told to do so rather than constantly reading from the stream and buffering up the results.

public class StreamPipeReader : PipeReader
Inheritance
StreamPipeReader
Inherited Members
Extension Methods

Constructors

StreamPipeReader(Stream)

Initializes a new instance of the StreamPipeReader class.

public StreamPipeReader(Stream stream)

Parameters

stream Stream

The stream to read from.

Remarks

The stream will be disposed of when the reader is completed.

StreamPipeReader(Stream, int, bool)

Initializes a new instance of the StreamPipeReader class.

public StreamPipeReader(Stream stream, int bufferSize, bool leaveOpen)

Parameters

stream Stream

The stream to read from.

bufferSize int

A hint at the size of messages that are commonly transferred. Use 0 for a commonly reasonable default.

leaveOpen bool

true to leave the underlying stream open after calling Complete(Exception); false to close the stream.

Methods

AdvanceTo(SequencePosition)

Moves forward the pipeline's read cursor to after the consumed data, marking the data as processed.

public override void AdvanceTo(SequencePosition consumed)

Parameters

consumed SequencePosition

Marks the extent of the data that has been successfully processed.

AdvanceTo(SequencePosition, SequencePosition)

Moves forward the pipeline's read cursor to after the consumed data, marking the data as processed, read and examined.

public override void AdvanceTo(SequencePosition consumed, SequencePosition examined)

Parameters

consumed SequencePosition

Marks the extent of the data that has been successfully processed.

examined SequencePosition

Marks the extent of the data that has been read and examined.

CancelPendingRead()

Cancels the pending ReadAsync(CancellationToken) operation without causing it to throw and without completing the PipeReader. If there is no pending operation, this cancels the next operation.

public override void CancelPendingRead()

Complete(Exception?)

Signals to the producer that the consumer is done reading.

public override void Complete(Exception? exception = null)

Parameters

exception Exception

Optional Exception indicating a failure that's causing the pipeline to complete.

OnWriterCompleted(Action<Exception?, object?>, object?)

Registers a callback that executes when the PipeWriter side of the pipe is completed.

[Obsolete]
public override void OnWriterCompleted(Action<Exception?, object?> callback, object? state)

Parameters

callback Action<Exception, object>

The callback to register.

state object

The state object to pass to callback when it's invoked.

Read()

Reads a sequence of bytes from the from the buffer or underlying Stream using synchronous APIs.

public ReadResult Read()

Returns

ReadResult

The read buffer.

ReadAsync(CancellationToken)

Asynchronously reads a sequence of bytes from the current PipeReader.

public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default value is default.

Returns

ValueTask<ReadResult>

A ValueTask<TResult> representing the asynchronous read operation.

TryRead(out ReadResult)

Attempts to synchronously read data the PipeReader.

public override bool TryRead(out ReadResult result)

Parameters

result ReadResult

When this method returns true, this value is set to a ReadResult instance that represents the result of the read call; otherwise, this value is set to default.

Returns

bool

true if data was available, or if the call was canceled or the writer was completed; otherwise, false.