Table of Contents

Class SequenceTextReader

Namespace
Nerdbank.Streams
Assembly
Nerdbank.Streams.dll

A TextReader that reads from a reassignable instance of ReadOnlySequence<T>.

public class SequenceTextReader : TextReader, IDisposable
Inheritance
SequenceTextReader
Implements
Inherited Members

Remarks

Using this is much more memory efficient than a StreamReader when reading from many different ReadOnlySequence<T> instances because the same reader, with all its buffers, can be reused.

Constructors

SequenceTextReader()

Initializes a new instance of the SequenceTextReader class without associating it with an initial ReadOnlySequence<T>.

public SequenceTextReader()

Remarks

When using this constructor, call Initialize(ReadOnlySequence<byte>, Encoding) to associate the instance with the initial byte sequence to be read.

SequenceTextReader(ReadOnlySequence<byte>, Encoding)

Initializes a new instance of the SequenceTextReader class.

public SequenceTextReader(ReadOnlySequence<byte> sequence, Encoding encoding)

Parameters

sequence ReadOnlySequence<byte>

The sequence to read from.

encoding Encoding

The encoding to use.

Methods

Initialize(ReadOnlySequence<byte>, Encoding)

Initializes or reinitializes this instance to read from a given ReadOnlySequence<T>.

public void Initialize(ReadOnlySequence<byte> sequence, Encoding encoding)

Parameters

sequence ReadOnlySequence<byte>

The sequence to read from.

encoding Encoding

The encoding to use.

Peek()

Reads the next character without changing the state of the reader or the character source. Returns the next available character without actually reading it from the reader.

public override int Peek()

Returns

int

An integer representing the next character to be read, or -1 if no more characters are available or the reader does not support seeking.

Exceptions

ObjectDisposedException

The TextReader is closed.

IOException

An I/O error occurs.

Read()

Reads the next character from the text reader and advances the character position by one character.

public override int Read()

Returns

int

The next character from the text reader, or -1 if no more characters are available. The default implementation returns -1.

Exceptions

ObjectDisposedException

The TextReader is closed.

IOException

An I/O error occurs.

Read(char[], int, int)

Reads a specified maximum number of characters from the current reader and writes the data to a buffer, beginning at the specified index.

public override int Read(char[] buffer, int index, int count)

Parameters

buffer char[]

When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

index int

The position in buffer at which to begin writing.

count int

The maximum number of characters to read. If the end of the reader is reached before the specified number of characters is read into the buffer, the method returns.

Returns

int

The number of characters that have been read. The number will be less than or equal to count, depending on whether the data is available within the reader. This method returns 0 (zero) if it is called when no more characters are left to read.

Exceptions

ArgumentNullException

buffer is null.

ArgumentException

The buffer length minus index is less than count.

ArgumentOutOfRangeException

index or count is negative.

ObjectDisposedException

The TextReader is closed.

IOException

An I/O error occurs.

Read(Span<char>)

Reads the characters from the current reader and writes the data to the specified buffer.

public override int Read(Span<char> buffer)

Parameters

buffer Span<char>

When this method returns, contains the specified span of characters replaced by the characters read from the current source.

Returns

int

The number of characters that have been read. The number will be less than or equal to the buffer length, depending on whether the data is available within the reader. This method returns 0 (zero) if it is called when no more characters are left to read.

Exceptions

ArgumentNullException

buffer is null.

IOException

The number of characters read from the stream is larger than the length of the buffer.

ReadAsync(char[], int, int)

Reads a specified maximum number of characters from the current text reader asynchronously and writes the data to a buffer, beginning at the specified index.

public override Task<int> ReadAsync(char[] buffer, int index, int count)

Parameters

buffer char[]

When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

index int

The position in buffer at which to begin writing.

count int

The maximum number of characters to read. If the end of the text is reached before the specified number of characters is read into the buffer, the current method returns.

Returns

Task<int>

A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the text has been reached.

Exceptions

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

index or count is negative.

ArgumentException

The sum of index and count is larger than the buffer length.

ObjectDisposedException

The text reader has been disposed.

InvalidOperationException

The reader is currently in use by a previous read operation.

ReadAsync(Memory<char>, CancellationToken)

Asynchronously reads the characters from the current stream into a memory block.

public override ValueTask<int> ReadAsync(Memory<char> buffer, CancellationToken cancellationToken = default)

Parameters

buffer Memory<char>

When this method returns, contains the specified memory block of characters replaced by the characters read from the current source.

cancellationToken CancellationToken

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

Returns

ValueTask<int>

A value task that represents the asynchronous read operation. The value of the type parameter contains the number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to the buffer length, depending on whether the data is available within the stream.

ReadBlockAsync(char[], int, int)

Reads a specified maximum number of characters from the current text reader asynchronously and writes the data to a buffer, beginning at the specified index.

public override Task<int> ReadBlockAsync(char[] buffer, int index, int count)

Parameters

buffer char[]

When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

index int

The position in buffer at which to begin writing.

count int

The maximum number of characters to read. If the end of the text is reached before the specified number of characters is read into the buffer, the current method returns.

Returns

Task<int>

A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the text has been reached.

Exceptions

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

index or count is negative.

ArgumentException

The sum of index and count is larger than the buffer length.

ObjectDisposedException

The text reader has been disposed.

InvalidOperationException

The reader is currently in use by a previous read operation.

ReadBlockAsync(Memory<char>, CancellationToken)

Asynchronously reads the characters from the current stream and writes the data to a buffer.

public override ValueTask<int> ReadBlockAsync(Memory<char> buffer, CancellationToken cancellationToken = default)

Parameters

buffer Memory<char>

When this method returns, contains the specified memory block of characters replaced by the characters read from the current source.

cancellationToken CancellationToken

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

Returns

ValueTask<int>

A value task that represents the asynchronous read operation. The value of the type parameter contains the total number of characters read into the buffer. The result value can be less than the number of characters requested if the number of characters currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

ReadLineAsync()

Reads a line of characters asynchronously and returns the data as a string.

public override Task<string?> ReadLineAsync()

Returns

Task<string>

A task that represents the asynchronous read operation. The value of the TResult parameter contains the next line from the text reader, or is null if all of the characters have been read.

Exceptions

ArgumentOutOfRangeException

The number of characters in the next line is larger than Int32.MaxValue.

ObjectDisposedException

The text reader has been disposed.

InvalidOperationException

The reader is currently in use by a previous read operation.

ReadToEndAsync()

Reads all characters from the current position to the end of the text reader asynchronously and returns them as one string.

public override Task<string> ReadToEndAsync()

Returns

Task<string>

A task that represents the asynchronous read operation. The value of the TResult parameter contains a string with the characters from the current position to the end of the text reader.

Exceptions

ArgumentOutOfRangeException

The number of characters is larger than Int32.MaxValue.

ObjectDisposedException

The text reader has been disposed.

InvalidOperationException

The reader is currently in use by a previous read operation.

Reset()

Clears references to the ReadOnlySequence<T> set by a prior call to Initialize(ReadOnlySequence<byte>, Encoding).

public void Reset()