Class ReadBufferedStream
Provides a stream that buffers small reads of another stream, to read it in fewer, larger reads; writes go to the source as they are.
public class ReadBufferedStream : StreamDecoratorAsync, IAsyncDisposable, IDisposable, IDataStream
- Inheritance
-
ReadBufferedStream
- Implements
- Inherited Members
- Extension Methods
Remarks
A read larger than the buffer, with the buffer empty, goes to the source directly. A read takes from the buffer while it holds data, and refills it only once it is empty, so a read never waits for more data while some is buffered.
It is made for a source whose reads and writes go their own ways, as a network stream's do. Over one that reads and writes at one position, as a file does, a write lands after what the buffer has read ahead.
Constructors
ReadBufferedStream(Stream, bool, int, ReadOnlySpan<byte>)
Initializes a new instance of the ReadBufferedStream class.
public ReadBufferedStream(Stream sourceStream, bool leaveOpen, int bufferSize = 1024, ReadOnlySpan<byte> initData = default)
Parameters
sourceStreamStreamThe source stream.
leaveOpenbooltrue to leave the source open when this stream is disposed.
bufferSizeintThe buffer's size, in bytes. The default is 1024.
initDataReadOnlySpan<byte>Data read before, which reads return first, or empty.
Exceptions
- ArgumentOutOfRangeException
bufferSizeis not above zero, orinitDatais longer than it.
ReadBufferedStream(Stream, bool, ReadOnlySpan<byte>)
Initializes a new instance of the ReadBufferedStream class whose buffer starts with data already read from the source, and is that long.
public ReadBufferedStream(Stream sourceStream, bool leaveOpen, ReadOnlySpan<byte> initData)
Parameters
sourceStreamStreamThe source stream.
leaveOpenbooltrue to leave the source open when this stream is disposed.
initDataReadOnlySpan<byte>The data read before, which reads return first.
Exceptions
- ArgumentOutOfRangeException
initDatais empty.
Properties
AllowBufferRefill
Gets or sets a value indicating whether an empty buffer is filled again; otherwise, once it is empty, reads go to the source directly. The default is true.
public bool AllowBufferRefill { get; set; }
Property Value
CanSeek
When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
public override bool CanSeek { get; }
Property Value
Remarks
It is false here.
DataAvailable
Gets a value indicating whether data is waiting to be read: the source's, when it is an IDataStream or a NetworkStream; otherwise, null.
public override bool? DataAvailable { get; }
Property Value
- bool?
Remarks
It is true while the buffer holds data; otherwise, the source tells.
Position
When overridden in a derived class, gets or sets the position within the current stream.
public override long Position { get; set; }
Property Value
- long
The current position within the stream.
Remarks
The source's.
Exceptions
- IOException
An I/O error occurs.
- NotSupportedException
The stream does not support seeking.
- ObjectDisposedException
Methods were called after the stream was closed.
- NotSupportedException
Always.
Methods
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.
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
Parameters
bufferMemory<byte>The region of memory to write the data into.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests. The default value is None.
Returns
- 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.
Remarks
The source's.
Exceptions
- OperationCanceledException
The cancellation token was canceled. This exception is stored into the returned task.