Class PoolingBufferedStream
Represents alternative implementation of BufferedStream that supports memory pooling.
Implements
Inherited Members
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 SourcePoolingBufferedStream(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 |
Remarks
The stream implements lazy buffer pattern. It means that the stream releases the buffer when there is no buffered data.
Properties
View SourceAllocator
Gets or sets buffer allocator.
Declaration
public MemoryAllocator<byte> Allocator { get; init; }
Property Value
| Type | Description |
|---|---|
| MemoryAllocator<byte> |
BaseStream
Gets the base stream.
Declaration
public Stream BaseStream { get; }
Property Value
| Type | Description |
|---|---|
| Stream |
CanRead
Declaration
public override bool CanRead { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Overrides
View SourceCanSeek
Declaration
public override bool CanSeek { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Overrides
View SourceCanTimeout
Declaration
public override bool CanTimeout { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Overrides
View SourceCanWrite
Declaration
public override bool CanWrite { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Overrides
View SourceHasBufferedDataToRead
Gets a value indicating that the stream has data in read buffer.
Declaration
public bool HasBufferedDataToRead { get; }
Property Value
| Type | Description |
|---|---|
| bool |
HasBufferedDataToWrite
Gets a value indicating that the stream has buffered data in write buffer.
Declaration
public bool HasBufferedDataToWrite { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Length
Declaration
public override long Length { get; }
Property Value
| Type | Description |
|---|---|
| long |
Overrides
View SourceMaxBufferSize
Gets the maximum size of the internal buffer, in bytes.
Declaration
public int MaxBufferSize { get; init; }
Property Value
| Type | Description |
|---|---|
| int |
Position
Declaration
public override long Position { get; set; }
Property Value
| Type | Description |
|---|---|
| long |
Overrides
View SourceReadTimeout
Declaration
public override int ReadTimeout { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
View SourceWriteTimeout
Declaration
public override int WriteTimeout { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
Methods
View SourceCopyTo(Stream, int)
Declaration
public override void CopyTo(Stream destination, int bufferSize)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | destination | |
| int | bufferSize |
Overrides
View SourceCopyToAsync(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
View SourceDispose(bool)
Declaration
protected override void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing |
Overrides
View SourceDisposeAsync()
Declaration
public override ValueTask DisposeAsync()
Returns
| Type | Description |
|---|---|
| ValueTask |
Overrides
View Source~PoolingBufferedStream()
Declaration
protected ~PoolingBufferedStream()
Flush()
Declaration
public override void Flush()
Overrides
View SourceFlushAsync(CancellationToken)
Declaration
public override Task FlushAsync(CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token |
Returns
| Type | Description |
|---|---|
| Task |
Overrides
View SourceRead()
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. |
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 |
|
| ObjectDisposedException | The stream is disposed. |
| InvalidOperationException | The underlying write buffer is not empty. |
See Also
View SourceRead(Span<byte>)
Declaration
public override int Read(Span<byte> data)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<byte> | data |
Returns
| Type | Description |
|---|---|
| int |
Overrides
View SourceReadAsync(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
View SourceReadAsync(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. |
Reset()
Resets the internal buffer.
Declaration
public void Reset()
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
View SourceSetLength(long)
Declaration
public override void SetLength(long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | value |
Overrides
View SourceTryGetReadBuffer(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 |
Remarks
Use Read(int) to mark the number of bytes read.
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 |
Remarks
Use Write(int) to commit the written bytes.
Write()
Writes the buffered data to the underlying stream.
Declaration
public void Write()
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 |
|
| ObjectDisposedException | The stream is disposed. |
| InvalidOperationException | The underlying read buffer is not empty. |
See Also
View SourceWrite(ReadOnlySpan<byte>)
Declaration
public override void Write(ReadOnlySpan<byte> data)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | data |
Overrides
View SourceWriteAsync(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
View SourceWriteAsync(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. |