Show / Hide Table of Contents

Class PoolingBufferedStream

Represents alternative implementation of BufferedStream that supports memory pooling.

Inheritance
object
MarshalByRefObject
Stream
ModernStream
PoolingBufferedStream
Implements
IDisposable
IAsyncDisposable
IFlushable
IAsyncBinaryReader
IAsyncBinaryWriter
ISupplier<ReadOnlyMemory<byte>, CancellationToken, ValueTask>
IFunctional
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.CopyTo(Stream)
Stream.CopyToAsync(Stream)
Stream.CopyToAsync(Stream, int)
Stream.CopyToAsync(Stream, CancellationToken)
Stream.Dispose()
Stream.Close()
Stream.FlushAsync()
Stream.ReadAsync(byte[], int, int)
Stream.ReadExactlyAsync(Memory<byte>, CancellationToken)
Stream.ReadExactlyAsync(byte[], int, int, CancellationToken)
Stream.ReadAtLeastAsync(Memory<byte>, int, bool, CancellationToken)
Stream.WriteAsync(byte[], int, int)
Stream.ReadExactly(Span<byte>)
Stream.ReadExactly(byte[], int, int)
Stream.ReadAtLeast(Span<byte>, int, bool)
Stream.Synchronized(Stream)
MarshalByRefObject.GetLifetimeService()
MarshalByRefObject.InitializeLifetimeService()
object.GetType()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.IO
Assembly: DotNext.IO.dll
Syntax
public sealed class PoolingBufferedStream : ModernStream, IDisposable, IAsyncDisposable, IFlushable, IAsyncBinaryReader, IAsyncBinaryWriter, ISupplier<ReadOnlyMemory<byte>, CancellationToken, ValueTask>, IFunctional
Remarks

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

Constructors

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

View Source

Allocator

Gets or sets buffer allocator.

Declaration
public MemoryAllocator<byte> Allocator { get; init; }
Property Value
Type Description
MemoryAllocator<byte>
View Source

BaseStream

Gets the base stream.

Declaration
public Stream BaseStream { get; }
Property Value
Type Description
Stream
View Source

CanRead

Declaration
public override bool CanRead { get; }
Property Value
Type Description
bool
Overrides
Stream.CanRead
View Source

CanSeek

Declaration
public override bool CanSeek { get; }
Property Value
Type Description
bool
Overrides
Stream.CanSeek
View Source

CanTimeout

Declaration
public override bool CanTimeout { get; }
Property Value
Type Description
bool
Overrides
Stream.CanTimeout
View Source

CanWrite

Declaration
public override bool CanWrite { get; }
Property Value
Type Description
bool
Overrides
Stream.CanWrite
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
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
View Source

Length

Declaration
public override long Length { get; }
Property Value
Type Description
long
Overrides
Stream.Length
View Source

MaxBufferSize

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

Declaration
public int MaxBufferSize { get; init; }
Property Value
Type Description
int
View Source

Position

Declaration
public override long Position { get; set; }
Property Value
Type Description
long
Overrides
Stream.Position
View Source

ReadTimeout

Declaration
public override int ReadTimeout { get; set; }
Property Value
Type Description
int
Overrides
Stream.ReadTimeout
View Source

WriteTimeout

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

Methods

View Source

CopyTo(Stream, int)

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

CopyToAsync(Stream, int, CancellationToken)

Declaration
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken token)
Parameters
Type Name Description
Stream destination
int bufferSize
CancellationToken token
Returns
Type Description
Task
Overrides
Stream.CopyToAsync(Stream, int, CancellationToken)
View Source

Dispose(bool)

Declaration
protected override void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing
Overrides
Stream.Dispose(bool)
View Source

DisposeAsync()

Declaration
public override ValueTask DisposeAsync()
Returns
Type Description
ValueTask
Overrides
Stream.DisposeAsync()
View Source

~PoolingBufferedStream()

Declaration
protected ~PoolingBufferedStream()
View Source

Flush()

Declaration
public override void Flush()
Overrides
Stream.Flush()
View Source

FlushAsync(CancellationToken)

Declaration
public override Task FlushAsync(CancellationToken token)
Parameters
Type Name Description
CancellationToken token
Returns
Type Description
Task
Overrides
Stream.FlushAsync(CancellationToken)
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.

View Source

Read(int)

Marks the specified number of bytes in the internal buffer as read.

Declaration
public void Read(int count)
Parameters
Type Name Description
int count

The number of bytes read.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is larger than the available bytes to read.

ObjectDisposedException

The stream is disposed.

InvalidOperationException

The underlying write buffer is not empty.

See Also
TryGetReadBuffer(int, out ReadOnlyMemory<byte>)
View Source

Read(Span<byte>)

Declaration
public override int Read(Span<byte> data)
Parameters
Type Name Description
Span<byte> data
Returns
Type Description
int
Overrides
ModernStream.Read(Span<byte>)
View Source

ReadAsync(Memory<byte>, CancellationToken)

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>
Overrides
ModernStream.ReadAsync(Memory<byte>, CancellationToken)
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.

View Source

Reset()

Resets the internal buffer.

Declaration
public void Reset()
View Source

Seek(long, SeekOrigin)

Declaration
public override long Seek(long offset, SeekOrigin origin)
Parameters
Type Name Description
long offset
SeekOrigin origin
Returns
Type Description
long
Overrides
Stream.Seek(long, SeekOrigin)
View Source

SetLength(long)

Declaration
public override void SetLength(long value)
Parameters
Type Name Description
long value
Overrides
Stream.SetLength(long)
View Source

TryGetReadBuffer(int, out ReadOnlyMemory<byte>)

Tries to get the read buffer

Declaration
public bool TryGetReadBuffer(int minimumSize, out ReadOnlyMemory<byte> buffer)
Parameters
Type Name Description
int minimumSize

The expected number of available bytes to read in the underlying buffer.

ReadOnlyMemory<byte> buffer

The readable buffer.

Returns
Type Description
bool

true if the underlying buffer is at least of size minimumSize; otherwise, false.

Remarks

Use Read(int) to mark the number of bytes read.

View Source

TryGetWriteBuffer(int, out Memory<byte>)

Tries to get the underlying buffer for write.

Declaration
public bool TryGetWriteBuffer(int minimumSize, out Memory<byte> buffer)
Parameters
Type Name Description
int minimumSize

The minimum size of the requested buffer.

Memory<byte> buffer

The writable buffer.

Returns
Type Description
bool

true if the underlying buffer is at least of size minimumSize; otherwise, false.

Remarks

Use Write(int) to commit the written bytes.

View Source

Write()

Writes the buffered data to the underlying stream.

Declaration
public void Write()
View Source

Write(int)

Marks the specified number of bytes in the internal buffer as written.

Declaration
public void Write(int count)
Parameters
Type Name Description
int count

The number of bytes to commit.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is larger than the available internal buffer.

ObjectDisposedException

The stream is disposed.

InvalidOperationException

The underlying read buffer is not empty.

See Also
TryGetWriteBuffer(int, out Memory<byte>)
View Source

Write(ReadOnlySpan<byte>)

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

WriteAsync(ReadOnlyMemory<byte>, CancellationToken)

Declaration
public override ValueTask WriteAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
Parameters
Type Name Description
ReadOnlyMemory<byte> data
CancellationToken token
Returns
Type Description
ValueTask
Overrides
ModernStream.WriteAsync(ReadOnlyMemory<byte>, CancellationToken)
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

IDisposable
IAsyncDisposable
IFlushable
IAsyncBinaryReader
IAsyncBinaryWriter
ISupplier<T1, T2, TResult>
IFunctional

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, params ReadOnlySpan<T>)
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, Func<ReadOnlySpan<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.Slice(Stream, long, long)
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)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾