Show / Hide Table of Contents

Class PoolingBufferedStream

Represents alternative implementation of BufferedStream that supports memory pooling.

Inheritance
object
MarshalByRefObject
Stream
ModernStream
PoolingBufferedStream
Implements
IAsyncDisposable
IFlushable
IBufferedWriter
IBufferWriter<byte>
IBufferedReader
IBufferedChannel
IResettable
IDisposable
Inherited Members
ModernStream.Read(byte[], int, int)
ModernStream.ReadAsync(byte[], int, int, CancellationToken)
ModernStream.BeginRead(byte[], int, int, AsyncCallback, object)
ModernStream.EndRead(IAsyncResult)
ModernStream.ReadByte()
ModernStream.Write(byte[], int, int)
ModernStream.WriteByte(byte)
ModernStream.WriteAsync(byte[], int, int, CancellationToken)
ModernStream.BeginWrite(byte[], int, int, AsyncCallback, object)
ModernStream.EndWrite(IAsyncResult)
Stream.Null
Stream.Close()
Stream.CopyTo(Stream)
Stream.CopyToAsync(Stream)
Stream.CopyToAsync(Stream, int)
Stream.CopyToAsync(Stream, CancellationToken)
Stream.Dispose()
Stream.FlushAsync()
Stream.ReadAsync(byte[], int, int)
Stream.ReadAtLeast(Span<byte>, int, bool)
Stream.ReadAtLeastAsync(Memory<byte>, int, bool, CancellationToken)
Stream.ReadExactly(byte[], int, int)
Stream.ReadExactly(Span<byte>)
Stream.ReadExactlyAsync(byte[], int, int, CancellationToken)
Stream.ReadExactlyAsync(Memory<byte>, CancellationToken)
Stream.Synchronized(Stream)
Stream.WriteAsync(byte[], int, int)
MarshalByRefObject.GetLifetimeService()
MarshalByRefObject.InitializeLifetimeService()
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext.IO
Assembly: DotNext.IO.dll
Syntax
public sealed class PoolingBufferedStream : ModernStream, IAsyncDisposable, IFlushable, IBufferedWriter, IBufferWriter<byte>, IBufferedReader, IBufferedChannel, IResettable, IDisposable
Remarks

The stream implements lazy buffer pattern. It means that the stream releases the buffer when there is no buffered data.

Constructors

| Edit this page View Source

PoolingBufferedStream(Stream, bool)

Represents alternative implementation of BufferedStream that supports memory pooling.

Declaration
public PoolingBufferedStream(Stream stream, bool leaveOpen = false)
Parameters
Type Name Description
Stream stream

The underlying stream to be buffered.

bool leaveOpen

true to leave stream open after the object is disposed; otherwise, false.

Remarks

The stream implements lazy buffer pattern. It means that the stream releases the buffer when there is no buffered data.

Properties

| Edit this page View Source

Allocator

Gets or sets buffer allocator.

Declaration
public MemoryAllocator<byte>? Allocator { get; init; }
Property Value
Type Description
MemoryAllocator<byte>
| Edit this page View Source

BaseStream

Gets the base stream.

Declaration
public Stream BaseStream { get; }
Property Value
Type Description
Stream
| Edit this page View Source

CanRead

When overridden in a derived class, gets a value indicating whether the current stream supports reading.

Declaration
public override bool CanRead { get; }
Property Value
Type Description
bool

true if the stream supports reading; otherwise, false.

Overrides
Stream.CanRead
| Edit this page View Source

CanSeek

When overridden in a derived class, gets a value indicating whether the current stream supports seeking.

Declaration
public override bool CanSeek { get; }
Property Value
Type Description
bool

true if the stream supports seeking; otherwise, false.

Overrides
Stream.CanSeek
| Edit this page View Source

CanTimeout

Gets a value that determines whether the current stream can time out.

Declaration
public override bool CanTimeout { get; }
Property Value
Type Description
bool

A value that determines whether the current stream can time out.

Overrides
Stream.CanTimeout
| Edit this page View Source

CanWrite

When overridden in a derived class, gets a value indicating whether the current stream supports writing.

Declaration
public override bool CanWrite { get; }
Property Value
Type Description
bool

true if the stream supports writing; otherwise, false.

Overrides
Stream.CanWrite
| Edit this page View Source

HasBufferedDataToRead

Gets a value indicating that the stream has data in read buffer.

Declaration
public bool HasBufferedDataToRead { get; }
Property Value
Type Description
bool
| Edit this page View Source

HasBufferedDataToWrite

Gets a value indicating that the stream has buffered data in write buffer.

Declaration
public bool HasBufferedDataToWrite { get; }
Property Value
Type Description
bool
| Edit this page View Source

Length

When overridden in a derived class, gets the length in bytes of the stream.

Declaration
public override long Length { get; }
Property Value
Type Description
long

A long value representing the length of the stream in bytes.

Overrides
Stream.Length
Exceptions
Type Condition
NotSupportedException

A class derived from Stream does not support seeking and the length is unknown.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

MaxBufferSize

Gets the maximum size of the internal buffer, in bytes.

Declaration
public int MaxBufferSize { get; init; }
Property Value
Type Description
int
| Edit this page View Source

Position

When overridden in a derived class, gets or sets the position within the current stream.

Declaration
public override long Position { get; set; }
Property Value
Type Description
long

The current position within the stream.

Overrides
Stream.Position
Exceptions
Type Condition
IOException

An I/O error occurs.

NotSupportedException

The stream does not support seeking.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

ReadTimeout

Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.

Declaration
public override int ReadTimeout { get; set; }
Property Value
Type Description
int

A value, in milliseconds, that determines how long the stream will attempt to read before timing out.

Overrides
Stream.ReadTimeout
Exceptions
Type Condition
InvalidOperationException

The ReadTimeout method always throws an InvalidOperationException.

| Edit this page View Source

WriteTimeout

Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.

Declaration
public override int WriteTimeout { get; set; }
Property Value
Type Description
int

A value, in milliseconds, that determines how long the stream will attempt to write before timing out.

Overrides
Stream.WriteTimeout
Exceptions
Type Condition
InvalidOperationException

The WriteTimeout method always throws an InvalidOperationException.

Methods

| Edit this page View Source

CopyTo(Stream, int)

Reads the bytes from the current stream and writes them to another stream, using a specified buffer size. Both streams positions are advanced by the number of bytes copied.

Declaration
public override void CopyTo(Stream destination, int bufferSize)
Parameters
Type Name Description
Stream destination

The stream to which the contents of the current stream will be copied.

int bufferSize

The size of the buffer. This value must be greater than zero. The default size is 81920.

Overrides
Stream.CopyTo(Stream, int)
Exceptions
Type Condition
ArgumentNullException

destination is null.

ArgumentOutOfRangeException

bufferSize is negative or zero.

NotSupportedException

The current stream does not support reading.

-or-

destination does not support writing.

ObjectDisposedException

Either the current stream or destination were closed before the CopyTo(Stream) method was called.

IOException

An I/O error occurred.

| Edit this page View Source

CopyToAsync(Stream, int, CancellationToken)

Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token. Both streams positions are advanced by the number of bytes copied.

Declaration
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken token)
Parameters
Type Name Description
Stream destination

The stream to which the contents of the current stream will be copied.

int bufferSize

The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.

CancellationToken token
Returns
Type Description
Task

A task that represents the asynchronous copy operation.

Overrides
Stream.CopyToAsync(Stream, int, CancellationToken)
Exceptions
Type Condition
ArgumentNullException

destination is null.

ArgumentOutOfRangeException

buffersize is negative or zero.

ObjectDisposedException

Either the current stream or the destination stream is disposed.

NotSupportedException

The current stream does not support reading, or the destination stream does not support writing.

| Edit this page View Source

Dispose(bool)

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

Declaration
protected override void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing

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

Overrides
Stream.Dispose(bool)
| Edit this page View Source

DisposeAsync()

Asynchronously releases the unmanaged resources used by the Stream.

Declaration
public override ValueTask DisposeAsync()
Returns
Type Description
ValueTask

A task that represents the asynchronous dispose operation.

Overrides
Stream.DisposeAsync()
| Edit this page View Source

~PoolingBufferedStream()

Declaration
protected ~PoolingBufferedStream()
| Edit this page View Source

Flush()

When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

Declaration
public override void Flush()
Overrides
Stream.Flush()
Exceptions
Type Condition
IOException

An I/O error occurs.

| Edit this page View Source

FlushAsync(CancellationToken)

Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.

Declaration
public override Task FlushAsync(CancellationToken token)
Parameters
Type Name Description
CancellationToken token
Returns
Type Description
Task

A task that represents the asynchronous flush operation.

Overrides
Stream.FlushAsync(CancellationToken)
Exceptions
Type Condition
ObjectDisposedException

The stream has been disposed.

| Edit this page View Source

Read()

Populates the internal buffer from the underlying stream.

Declaration
public bool Read()
Returns
Type Description
bool

true if

Exceptions
Type Condition
ObjectDisposedException

The stream is disposed.

InternalBufferOverflowException

The internal buffer is full.

| Edit this page View Source

Read(Span<byte>)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

Declaration
public override int Read(Span<byte> data)
Parameters
Type Name Description
Span<byte> data
Returns
Type Description
int

The total number of bytes read into the buffer. This can be less than the size of the buffer if that many bytes are not currently available, or zero (0) if the buffer's length is zero or the end of the stream has been reached.

Overrides
ModernStream.Read(Span<byte>)
| Edit this page View Source

ReadAsync(Memory<byte>, CancellationToken)

Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

Declaration
public override ValueTask<int> ReadAsync(Memory<byte> data, CancellationToken token = default)
Parameters
Type Name Description
Memory<byte> data
CancellationToken token
Returns
Type Description
ValueTask<int>

A task that represents the asynchronous read operation. The value of its Result property contains the total number of bytes read into the buffer. The result value can be less than the length of the buffer if that many bytes are not currently available, or it can be 0 (zero) if the length of the buffer is 0 or if the end of the stream has been reached.

Overrides
ModernStream.ReadAsync(Memory<byte>, CancellationToken)
| Edit this page View Source

ReadAsync(CancellationToken)

Fetches the internal buffer from the underlying stream.

Declaration
public ValueTask<bool> ReadAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask<bool>

true if the data has been copied from the file to the internal buffer; false if no more data to read.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

ObjectDisposedException

The stream is disposed.

InternalBufferOverflowException

The internal buffer is full.

| Edit this page View Source

Reset()

Resets the internal buffer.

Declaration
public void Reset()
| Edit this page View Source

Seek(long, SeekOrigin)

When overridden in a derived class, sets the position within the current stream.

Declaration
public override long Seek(long offset, SeekOrigin origin)
Parameters
Type Name Description
long offset

A byte offset relative to the origin parameter.

SeekOrigin origin

A value of type SeekOrigin indicating the reference point used to obtain the new position.

Returns
Type Description
long

The new position within the current stream.

Overrides
Stream.Seek(long, SeekOrigin)
Exceptions
Type Condition
IOException

An I/O error occurs.

NotSupportedException

The stream does not support seeking, such as if the stream is constructed from a pipe or console output.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

SetLength(long)

When overridden in a derived class, sets the length of the current stream.

Declaration
public override void SetLength(long value)
Parameters
Type Name Description
long value

The desired length of the current stream in bytes.

Overrides
Stream.SetLength(long)
Exceptions
Type Condition
IOException

An I/O error occurs.

NotSupportedException

The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

Write()

Writes the buffered data to the underlying stream.

Declaration
public void Write()
| Edit this page View Source

Write(ReadOnlySpan<byte>)

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

Declaration
public override void Write(ReadOnlySpan<byte> data)
Parameters
Type Name Description
ReadOnlySpan<byte> data
Overrides
ModernStream.Write(ReadOnlySpan<byte>)
| Edit this page View Source

WriteAsync(ReadOnlyMemory<byte>, CancellationToken)

Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

Declaration
public override ValueTask WriteAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
Parameters
Type Name Description
ReadOnlyMemory<byte> data
CancellationToken token
Returns
Type Description
ValueTask

A task that represents the asynchronous write operation.

Overrides
ModernStream.WriteAsync(ReadOnlyMemory<byte>, CancellationToken)
| Edit this page View Source

WriteAsync(CancellationToken)

Writes the buffered data to the underlying stream.

Declaration
public ValueTask WriteAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
ValueTask

The task representing asynchronous execution of the operation.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

ObjectDisposedException

The stream is disposed.

Implements

IAsyncDisposable
IFlushable
IBufferedWriter
IBufferWriter<T>
IBufferedReader
IBufferedChannel
IResettable
IDisposable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
StreamSource.AsStream<TWriter>(TWriter, Action<TWriter>?, Func<TWriter, CancellationToken, Task>?)
TextStreamExtensions.AsTextWriter<TWriter>(TWriter, Encoding, IFormatProvider?, Action<TWriter>?, Func<TWriter, CancellationToken, Task>?)
ExpressionBuilder.Const<T>(T)
BufferWriter.Encode(IBufferWriter<byte>, ReadOnlySpan<char>, in EncodingContext, LengthFormat?)
BufferWriter.Format<T>(IBufferWriter<byte>, T, in EncodingContext, LengthFormat?, ReadOnlySpan<char>, IFormatProvider?, MemoryAllocator<char>?)
BufferWriter.Format<T>(IBufferWriter<byte>, T, LengthFormat?, ReadOnlySpan<char>, IFormatProvider?)
BufferWriter.Interpolate(IBufferWriter<byte>, in EncodingContext, Span<char>, in EncodingInterpolatedStringHandler)
BufferWriter.Interpolate(IBufferWriter<byte>, in EncodingContext, Span<char>, IFormatProvider?, in EncodingInterpolatedStringHandler)
BufferWriter.Write<T>(IBufferWriter<T>, in ReadOnlySequence<T>)
ByteBuffer.Format<T>(IBufferWriter<byte>, T, ReadOnlySpan<char>, IFormatProvider?)
ByteBuffer.Write(IBufferWriter<byte>, in BigInteger, bool, bool)
ByteBuffer.WriteBigEndian<T>(IBufferWriter<byte>, T)
ByteBuffer.WriteLittleEndian<T>(IBufferWriter<byte>, T)
ByteBuffer.Write<T>(IBufferWriter<byte>, T)
StreamExtensions.Combine(Stream, ReadOnlySpan<Stream>)
StreamExtensions.CopyToAsync(Stream, IBufferWriter<byte>, int, CancellationToken)
StreamExtensions.CopyToAsync(Stream, IBufferWriter<byte>, long, int, CancellationToken)
StreamExtensions.CopyToAsync(Stream, Stream, long, Memory<byte>, CancellationToken)
StreamExtensions.CopyToAsync(Stream, Stream, Memory<byte>, CancellationToken)
StreamExtensions.CopyToAsync<TConsumer>(Stream, TConsumer, long, Memory<byte>, CancellationToken)
StreamExtensions.CopyToAsync<TConsumer>(Stream, TConsumer, Memory<byte>, CancellationToken)
StreamExtensions.DecodeAsync(Stream, DecodingContext, LengthFormat, Memory<byte>, MemoryAllocator<char>?, CancellationToken)
StreamExtensions.DecodeAsync(Stream, DecodingContext, LengthFormat, Memory<char>, Memory<byte>, CancellationToken)
StreamExtensions.EncodeAsync(Stream, ReadOnlyMemory<char>, EncodingContext, LengthFormat?, Memory<byte>, CancellationToken)
StreamExtensions.FormatAsync<T>(Stream, T, EncodingContext, LengthFormat?, Memory<byte>, string?, IFormatProvider?, MemoryAllocator<char>?, CancellationToken)
StreamExtensions.FormatAsync<T>(Stream, T, LengthFormat?, Memory<byte>, string?, IFormatProvider?, CancellationToken)
StreamExtensions.ParseAsync<T>(Stream, LengthFormat, Memory<byte>, NumberStyles, IFormatProvider?, CancellationToken)
StreamExtensions.ParseAsync<T>(Stream, LengthFormat, Memory<byte>, IFormatProvider?, CancellationToken)
StreamExtensions.ParseAsync<TArg, TResult>(Stream, TArg, ReadOnlySpanFunc<char, TArg, TResult>, DecodingContext, LengthFormat, Memory<byte>, MemoryAllocator<char>?, CancellationToken)
StreamExtensions.ReadAllAsync(Stream, int, MemoryAllocator<byte>?, CancellationToken)
StreamExtensions.ReadAsync<T>(Stream, Memory<byte>, CancellationToken)
StreamExtensions.ReadBigEndianAsync<T>(Stream, Memory<byte>, CancellationToken)
StreamExtensions.ReadBlockAsync(Stream, LengthFormat, MemoryAllocator<byte>?, CancellationToken)
StreamExtensions.ReadExactlyAsync(Stream, long, int, MemoryAllocator<byte>?, CancellationToken)
StreamExtensions.ReadLittleEndianAsync<T>(Stream, Memory<byte>, CancellationToken)
StreamExtensions.ReadUtf8(Stream, Span<byte>, IBufferWriter<char>)
StreamExtensions.ReadUtf8Async(Stream, Memory<byte>, IBufferWriter<char>, CancellationToken)
StreamExtensions.ReadUtf8Async<TArg>(Stream, Memory<byte>, Memory<char>, Func<ReadOnlyMemory<char>, TArg, CancellationToken, ValueTask>, TArg, CancellationToken)
StreamExtensions.ReadUtf8<TArg>(Stream, Span<byte>, Span<char>, ReadOnlySpanAction<char, TArg>, TArg)
StreamExtensions.WriteAsync(Stream, ReadOnlySequence<byte>, CancellationToken)
StreamExtensions.WriteAsync(Stream, ReadOnlyMemory<byte>, LengthFormat, Memory<byte>, CancellationToken)
StreamExtensions.WriteAsync<T>(Stream, T, Memory<byte>, CancellationToken)
StreamExtensions.WriteBigEndianAsync<T>(Stream, T, Memory<byte>, CancellationToken)
StreamExtensions.WriteLittleEndianAsync<T>(Stream, T, Memory<byte>, CancellationToken)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾