Show / Hide Table of Contents

Class PipeExtensions

Represents extension method for parsing data stored in pipe.

Inheritance
object
PipeExtensions
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.IO.Pipelines
Assembly: DotNext.IO.dll
Syntax
public static class PipeExtensions

Methods

View Source

AsStream(IDuplexPipe, bool, bool)

Creates a duplex stream suitable for reading and writing from the duplex pipe.

Declaration
public static Stream AsStream(this IDuplexPipe pipe, bool leaveInputOpen = false, bool leaveOutputOpen = false)
Parameters
Type Name Description
IDuplexPipe pipe

The duplex pipe.

bool leaveInputOpen

true to leave Input available for reads; otherwise, false.

bool leaveOutputOpen

true to leave Output available for writes; otherwise, false.

Returns
Type Description
Stream

The stream that can be used to read from and write to the pipe.

Exceptions
Type Condition
ArgumentNullException

pipe is null.

View Source

CopyFromAsync(PipeWriter, Stream, long, CancellationToken)

Copies the specified number of bytes from source stream.

Declaration
public static ValueTask CopyFromAsync(this PipeWriter destination, Stream source, long count, CancellationToken token = default)
Parameters
Type Name Description
PipeWriter destination

The pipe to write into.

Stream source

The source stream.

long count

The number of bytes to be copied.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is negative.

ArgumentNullException

source is null.

EndOfStreamException

source has not enough data to read.

View Source

CopyToAsync<TConsumer>(PipeReader, TConsumer, long, CancellationToken)

Reads the entire content using the specified consumer.

Declaration
public static ValueTask CopyToAsync<TConsumer>(this PipeReader reader, TConsumer consumer, long count, CancellationToken token = default) where TConsumer : ISupplier<ReadOnlyMemory<byte>, CancellationToken, ValueTask>
Parameters
Type Name Description
PipeReader reader

The pipe to read from.

TConsumer consumer

The content reader.

long count

The number of bytes to copy.

CancellationToken token

The token that can be used to cancel operation.

Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Type Parameters
Name Description
TConsumer

The type of the consumer.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

CopyToAsync<TConsumer>(PipeReader, TConsumer, CancellationToken)

Reads the entire content using the specified consumer.

Declaration
public static ValueTask CopyToAsync<TConsumer>(this PipeReader reader, TConsumer consumer, CancellationToken token = default) where TConsumer : ISupplier<ReadOnlyMemory<byte>, CancellationToken, ValueTask>
Parameters
Type Name Description
PipeReader reader

The pipe to read from.

TConsumer consumer

The content reader.

CancellationToken token

The token that can be used to cancel operation.

Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Type Parameters
Name Description
TConsumer

The type of the consumer.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

Create(PipeReader)

Creates default implementation of binary reader for the specified pipe reader.

Declaration
public static IAsyncBinaryReader Create(PipeReader reader)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

Returns
Type Description
IAsyncBinaryReader

The binary reader.

Remarks

It is recommended to use extension methods from PipeExtensions class for decoding data from the stream. This method is intended for situation when you need an object implementing IAsyncBinaryReader interface.

Exceptions
Type Condition
ArgumentNullException

reader is null.

View Source

Create(PipeWriter, long)

Creates default implementation of binary writer for the pipe.

Declaration
public static IAsyncBinaryWriter Create(PipeWriter output, long bufferSize = 0)
Parameters
Type Name Description
PipeWriter output

The stream instance.

long bufferSize

The maximum numbers of bytes that can be buffered in the memory without flushing.

Returns
Type Description
IAsyncBinaryWriter

The binary writer.

Exceptions
Type Condition
ArgumentNullException

output is null.

ArgumentOutOfRangeException

bufferSize or is less than or equal to zero.

View Source

DecodeAsync(PipeReader, DecodingContext, LengthFormat, MemoryAllocator<char>?, CancellationToken)

Decodes string asynchronously from pipe.

Declaration
public static ValueTask<MemoryOwner<char>> DecodeAsync(this PipeReader reader, DecodingContext context, LengthFormat lengthFormat, MemoryAllocator<char>? allocator = null, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

DecodingContext context

The text decoding context.

LengthFormat lengthFormat

Represents string length encoding format.

MemoryAllocator<char> allocator

The allocator of the buffer of characters.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<MemoryOwner<char>>

The buffer of characters.

Exceptions
Type Condition
EndOfStreamException

reader doesn't contain the necessary number of bytes to restore string.

OperationCanceledException

The operation has been canceled.

ArgumentOutOfRangeException

lengthFormat is invalid.

View Source

DecodeAsync(PipeReader, DecodingContext, LengthFormat, Memory<char>, CancellationToken)

Decodes the sequence of characters.

Declaration
public static IAsyncEnumerable<ReadOnlyMemory<char>> DecodeAsync(this PipeReader reader, DecodingContext context, LengthFormat lengthFormat, Memory<char> buffer, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

DecodingContext context

The decoding context containing string characters encoding.

LengthFormat lengthFormat

The format of the string length encoded in the stream.

Memory<char> buffer

The buffer of characters.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
IAsyncEnumerable<ReadOnlyMemory<char>>

The enumerator of characters.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

EndOfStreamException

The underlying source doesn't contain necessary amount of bytes to decode the value.

ArgumentOutOfRangeException

lengthFormat is invalid.

View Source

ParseAsync<T>(PipeReader, LengthFormat, NumberStyles, IFormatProvider?, CancellationToken)

Parses the numeric value from UTF-8 encoded characters.

Declaration
public static ValueTask<T> ParseAsync<T>(this PipeReader reader, LengthFormat lengthFormat, NumberStyles style, IFormatProvider? provider = null, CancellationToken token = default) where T : INumberBase<T>
Parameters
Type Name Description
PipeReader reader

The pipe reader.

LengthFormat lengthFormat

The format of the string length (in bytes) encoded in the stream.

NumberStyles style

A combination of number styles.

IFormatProvider provider

Culture-specific formatting information.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<T>

The result of parsing.

Type Parameters
Name Description
T

The numeric type.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

EndOfStreamException

The underlying source doesn't contain necessary amount of bytes to decode the value.

ArgumentOutOfRangeException

lengthFormat is invalid.

View Source

ParseAsync<T>(PipeReader, LengthFormat, IFormatProvider?, CancellationToken)

Parses the sequence of characters encoded as UTF-8.

Declaration
public static ValueTask<T> ParseAsync<T>(this PipeReader reader, LengthFormat lengthFormat, IFormatProvider? provider = null, CancellationToken token = default) where T : IUtf8SpanParsable<T>
Parameters
Type Name Description
PipeReader reader

The pipe reader.

LengthFormat lengthFormat

The format of the string length (in bytes) encoded in the stream.

IFormatProvider provider

Culture-specific formatting information.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<T>

The result of parsing.

Type Parameters
Name Description
T

The type that supports parsing from UTF-8.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

EndOfStreamException

The underlying source doesn't contain necessary amount of bytes to decode the value.

ArgumentOutOfRangeException

lengthFormat is invalid.

View Source

ParseAsync<TArg, TResult>(PipeReader, TArg, Func<ReadOnlySpan<char>, TArg, TResult>, DecodingContext, LengthFormat, MemoryAllocator<char>?, CancellationToken)

Parses the sequence of characters.

Declaration
public static ValueTask<TResult> ParseAsync<TArg, TResult>(this PipeReader reader, TArg arg, Func<ReadOnlySpan<char>, TArg, TResult> parser, DecodingContext context, LengthFormat lengthFormat, MemoryAllocator<char>? allocator = null, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

TArg arg

The argument to be passed to parser.

Func<ReadOnlySpan<char>, TArg, TResult> parser

The parser of characters.

DecodingContext context

The decoding context containing string characters encoding.

LengthFormat lengthFormat

The format of the string length (in bytes) encoded in the stream.

MemoryAllocator<char> allocator

The allocator of internal buffer.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<TResult>

The parsed value.

Type Parameters
Name Description
TArg

The type of the argument to be passed to parser.

TResult

The type of the result.

Exceptions
Type Condition
ArgumentNullException

parser is null.

OperationCanceledException

The operation has been canceled.

EndOfStreamException

The underlying source doesn't contain necessary amount of bytes to decode the value.

ArgumentOutOfRangeException

lengthFormat is invalid.

View Source

ReadAllAsync(PipeReader, CancellationToken)

Reads all chunks of data from the pipe.

Declaration
public static IAsyncEnumerable<ReadOnlyMemory<byte>> ReadAllAsync(this PipeReader reader, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
IAsyncEnumerable<ReadOnlyMemory<byte>>

A sequence of data chunks.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

ReadAsync(PipeReader, LengthFormat, MemoryAllocator<byte>?, CancellationToken)

Reads length-prefixed block of bytes.

Declaration
public static ValueTask<MemoryOwner<byte>> ReadAsync(this PipeReader reader, LengthFormat lengthFormat, MemoryAllocator<byte>? allocator = null, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

LengthFormat lengthFormat

The format of the block length encoded in the underlying pipe.

MemoryAllocator<byte> allocator

The memory allocator used to place the decoded block of bytes.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<MemoryOwner<byte>>

The decoded block of bytes.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

EndOfStreamException

Reader doesn't have enough data.

View Source

ReadAsync(PipeReader, Memory<byte>, CancellationToken)

Reads the block of memory.

Declaration
public static ValueTask<int> ReadAsync(this PipeReader reader, Memory<byte> output, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

Memory<byte> output

The block of memory to fill from the pipe.

CancellationToken token

The token that can be used to cancel operation.

Returns
Type Description
ValueTask<int>

The actual number of copied bytes.

View Source

ReadAsync<T>(PipeReader, CancellationToken)

Decodes the value of binary formattable type.

Declaration
public static ValueTask<T> ReadAsync<T>(this PipeReader reader, CancellationToken token = default) where T : IBinaryFormattable<T>
Parameters
Type Name Description
PipeReader reader

The pipe reader.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<T>

The decoded value.

Type Parameters
Name Description
T

The type of the result.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

EndOfStreamException

The underlying source doesn't contain necessary amount of bytes to decode the value.

View Source

ReadAtLeastAsync(PipeReader, Memory<byte>, int, CancellationToken)

Reads at least the specified number of bytes.

Declaration
public static ValueTask<int> ReadAtLeastAsync(this PipeReader reader, Memory<byte> destination, int minimumSize, CancellationToken token)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

Memory<byte> destination

The buffer to write into.

int minimumSize

The minimum number of bytes to read.

CancellationToken token

The token that can be used to cancel operation.

Returns
Type Description
ValueTask<int>

The actual number of bytes written to destination.

Exceptions
Type Condition
ArgumentOutOfRangeException

minimumSize is negative or greater than the length of destination.

OperationCanceledException

The operation has been canceled.

EndOfStreamException

Reader doesn't have enough data.

View Source

ReadBigEndianAsync<T>(PipeReader, CancellationToken)

Reads integer encoded in big-endian format.

Declaration
public static ValueTask<T> ReadBigEndianAsync<T>(this PipeReader reader, CancellationToken token = default) where T : IBinaryInteger<T>
Parameters
Type Name Description
PipeReader reader

The pipe reader.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<T>

The integer value.

Type Parameters
Name Description
T

The integer type.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

EndOfStreamException

The underlying source doesn't contain necessary amount of bytes to decode the value.

View Source

ReadExactlyAsync(PipeReader, long, CancellationToken)

Reads exactly the specified amount of bytes as a sequence of chunks.

Declaration
public static IAsyncEnumerable<ReadOnlyMemory<byte>> ReadExactlyAsync(this PipeReader reader, long length, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

long length

The numbers of bytes to read.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
IAsyncEnumerable<ReadOnlyMemory<byte>>

A collection of chunks.

Exceptions
Type Condition
EndOfStreamException

Reader doesn't have enough data to skip.

OperationCanceledException

The operation has been canceled.

ArgumentOutOfRangeException

length is

View Source

ReadExactlyAsync(PipeReader, Memory<byte>, CancellationToken)

Reads the block of memory.

Declaration
public static ValueTask ReadExactlyAsync(this PipeReader reader, Memory<byte> output, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

Memory<byte> output

The block of memory to fill from the pipe.

CancellationToken token

The token that can be used to cancel operation.

Returns
Type Description
ValueTask

The task representing asynchronous state of the operation.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

EndOfStreamException

Reader doesn't have enough data.

View Source

ReadLittleEndianAsync<T>(PipeReader, CancellationToken)

Reads integer encoded in little-endian format.

Declaration
public static ValueTask<T> ReadLittleEndianAsync<T>(this PipeReader reader, CancellationToken token = default) where T : IBinaryInteger<T>
Parameters
Type Name Description
PipeReader reader

The pipe reader.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<T>

The integer value.

Type Parameters
Name Description
T

The integer type.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

EndOfStreamException

The underlying source doesn't contain necessary amount of bytes to decode the value.

View Source

ReadUtf8Async(PipeReader, IBufferWriter<char>, CancellationToken)

Decodes null-terminated UTF-8 encoded string.

Declaration
public static ValueTask ReadUtf8Async(this PipeReader reader, IBufferWriter<char> output, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

IBufferWriter<char> output

The output buffer for decoded characters.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Remarks

This method returns when writer side completed or null char reached.

Exceptions
Type Condition
ArgumentNullException

reader is null; or output is null.

OperationCanceledException

The operation has been canceled.

View Source

SkipAsync(PipeReader, long, CancellationToken)

Drops the specified number of bytes from the pipe.

Declaration
public static ValueTask SkipAsync(this PipeReader reader, long length, CancellationToken token = default)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

long length

The number of bytes to skip.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Exceptions
Type Condition
ArgumentOutOfRangeException

length is less than zero.

OperationCanceledException

The operation has been canceled.

EndOfStreamException

Reader doesn't have enough data to skip.

View Source

TryReadExactly(PipeReader, long, out ReadResult)

Attempts to read block of data synchronously.

Declaration
public static bool TryReadExactly(this PipeReader reader, long length, out ReadResult result)
Parameters
Type Name Description
PipeReader reader

The pipe reader.

long length

The length of the block to consume, in bytes.

ReadResult result

The requested block of data which length is equal to length in case of success; otherwise, empty block.

Returns
Type Description
bool

true if the block of requested length is obtained successfully; otherwise, false.

Remarks

This method doesn't advance the reader position.

View Source

WriteAsync(PipeWriter, ReadOnlySequence<byte>, CancellationToken)

Writes sequence of bytes to the underlying stream asynchronously.

Declaration
public static ValueTask WriteAsync(this PipeWriter writer, ReadOnlySequence<byte> sequence, CancellationToken token = default)
Parameters
Type Name Description
PipeWriter writer

The pipe to write into.

ReadOnlySequence<byte> sequence

The sequence of bytes.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾