Table of Contents

Class BufferTextWriter

Namespace
Nerdbank.Streams
Assembly
Nerdbank.Streams.dll

A TextWriter that writes to a reassignable instance of IBufferWriter<T>.

public class BufferTextWriter : TextWriter, IAsyncDisposable, IDisposable
Inheritance
BufferTextWriter
Implements
Inherited Members

Remarks

Using this is much more memory efficient than a StreamWriter when writing to many different IBufferWriter<T> because the same writer, with all its buffers, can be reused.

Constructors

BufferTextWriter()

Initializes a new instance of the BufferTextWriter class.

public BufferTextWriter()

Remarks

When using this constructor, call Initialize(IBufferWriter<byte>, Encoding) to associate the instance with the initial writer to use before using any write or flush methods.

BufferTextWriter(IBufferWriter<byte>, Encoding)

Initializes a new instance of the BufferTextWriter class.

public BufferTextWriter(IBufferWriter<byte> bufferWriter, Encoding encoding)

Parameters

bufferWriter IBufferWriter<byte>

The buffer writer to write to.

encoding Encoding

The encoding to use.

Properties

Encoding

When overridden in a derived class, returns the character encoding in which the output is written.

public override Encoding Encoding { get; }

Property Value

Encoding

The character encoding in which the output is written.

Methods

Dispose(bool)

Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Flush()

Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

public override void Flush()

FlushAsync()

Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

public override Task FlushAsync()

Returns

Task

A task that represents the asynchronous flush operation.

Exceptions

ObjectDisposedException

The text writer is disposed.

InvalidOperationException

The writer is currently in use by a previous write operation.

Initialize(IBufferWriter<byte>, Encoding)

Prepares for writing to the specified buffer.

public void Initialize(IBufferWriter<byte> bufferWriter, Encoding encoding)

Parameters

bufferWriter IBufferWriter<byte>

The buffer writer to write to.

encoding Encoding

The encoding to use.

Reset()

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

public void Reset()

Write(char)

Writes a character to the text stream.

public override void Write(char value)

Parameters

value char

The character to write to the text stream.

Exceptions

ObjectDisposedException

The TextWriter is closed.

IOException

An I/O error occurs.

Write(char[], int, int)

Writes a subarray of characters to the text stream.

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

Parameters

buffer char[]

The character array to write data from.

index int

The character position in the buffer at which to start retrieving data.

count int

The number of characters to write.

Exceptions

ArgumentException

The buffer length minus index is less than count.

ArgumentNullException

The buffer parameter is null.

ArgumentOutOfRangeException

index or count is negative.

ObjectDisposedException

The TextWriter is closed.

IOException

An I/O error occurs.

Write(ReadOnlySpan<char>)

Writes a character span to the text stream.

public override void Write(ReadOnlySpan<char> buffer)

Parameters

buffer ReadOnlySpan<char>

The character span to write.

Write(string?)

Writes a string to the text stream.

public override void Write(string? value)

Parameters

value string

The string to write.

Exceptions

ObjectDisposedException

The TextWriter is closed.

IOException

An I/O error occurs.

WriteLine(ReadOnlySpan<char>)

Writes the text representation of a character span to the text stream, followed by a line terminator.

public override void WriteLine(ReadOnlySpan<char> buffer)

Parameters

buffer ReadOnlySpan<char>

The char span value to write to the text stream.