Class CustomBufferedStream
A custom network stream inherited from stream with an underlying read buffer supporting both read/write of UTF-8 encoded string or raw bytes asynchronously from last read position.
Inherited Members
Namespace: StreamExtended.Network
Assembly: StreamExtended.dll
Syntax
public class CustomBufferedStream : Stream, IAsyncDisposable, IDisposable, ICustomStreamReader, IPeekStream
Constructors
| Edit this page View SourceCustomBufferedStream(Stream, IBufferPool, int, bool)
Initializes a new instance of the CustomBufferedStream class.
Declaration
public CustomBufferedStream(Stream baseStream, IBufferPool bufferPool, int bufferSize, bool leaveOpen = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | baseStream | The base stream. |
| IBufferPool | bufferPool | Bufferpool. |
| int | bufferSize | Size of the buffer. |
| bool | leaveOpen | true to leave the stream open after disposing the CustomBufferedStream object; otherwise, false. |
Properties
| Edit this page View SourceAvailable
Gets the available data size.
Declaration
public int Available { get; }
Property Value
| Type | Description |
|---|---|
| int |
BufferSize
Declaration
public int BufferSize { get; }
Property Value
| Type | Description |
|---|---|
| int |
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 |
Overrides
| Edit this page View SourceCanSeek
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 |
Overrides
| Edit this page View SourceCanTimeout
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
| Edit this page View SourceCanWrite
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 |
Overrides
| Edit this page View SourceDataAvailable
Gets a value indicating whether data is available.
Declaration
public bool DataAvailable { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsClosed
Declaration
public bool IsClosed { get; }
Property Value
| Type | Description |
|---|---|
| bool |
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
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | A class derived from |
| ObjectDisposedException | Methods were called after the stream was closed. |
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
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. |
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
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | The ReadTimeout method always throws an InvalidOperationException. |
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
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | The WriteTimeout method always throws an InvalidOperationException. |
Methods
| Edit this page View SourceCopyToAsync(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 cancellationToken)
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 | cancellationToken | The token to monitor for cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task | A task that represents the asynchronous copy operation. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
| 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. |
| OperationCanceledException | The cancellation token was canceled. This exception is stored into the returned task. |
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
| Edit this page View SourceFillBuffer()
Fills the buffer.
Declaration
public bool FillBuffer()
Returns
| Type | Description |
|---|---|
| bool |
FillBufferAsync(CancellationToken)
Fills the buffer asynchronous.
Declaration
public Task<bool> FillBufferAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<bool> |
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
Exceptions
| Type | Condition |
|---|---|
| IOException | An I/O error occurs. |
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 cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | The token to monitor for cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task | A task that represents the asynchronous flush operation. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The stream has been disposed. |
| OperationCanceledException | The cancellation token was canceled. This exception is stored into the returned task. |
OnDataRead(byte[], int, int)
Declaration
protected virtual void OnDataRead(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | |
| int | offset | |
| int | count |
OnDataWrite(byte[], int, int)
Declaration
protected virtual void OnDataWrite(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | |
| int | offset | |
| int | count |
PeekByteAsync(int, CancellationToken)
Peeks a byte asynchronous.
Declaration
public Task<int> PeekByteAsync(int index, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<int> |
PeekByteFromBuffer(int)
Peeks a byte from buffer.
Declaration
public byte PeekByteFromBuffer(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index |
Returns
| Type | Description |
|---|---|
| byte |
PeekBytesAsync(byte[], int, int, int, CancellationToken)
Peeks bytes into the supplied buffer (IPeekStream).
Declaration
public ValueTask<int> PeekBytesAsync(byte[] buffer, int offset, int index, int count, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | |
| int | offset | |
| int | index | |
| int | count | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| ValueTask<int> |
PeekBytesAsync(int, int, CancellationToken)
Peeks bytes asynchronous.
Declaration
public Task<byte[]?> PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | |
| int | size | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<byte[]> |
Read(byte[], int, int)
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(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | An array of bytes. When this method returns, the buffer contains the specified byte array with the values between |
| int | offset | The zero-based byte offset in |
| int | count | The maximum number of bytes to be read from the current stream. |
Returns
| Type | Description |
|---|---|
| int | The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if |
Overrides
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The sum of |
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
| IOException | An I/O error occurs. |
| NotSupportedException | The stream does not support reading. |
| ObjectDisposedException | Methods were called after the stream was closed. |
ReadAndIgnoreAllLinesAsync(CancellationToken)
Read until the last new line, ignores the result
Declaration
public Task ReadAndIgnoreAllLinesAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
ReadAsync(byte[], int, int, 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 Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | The buffer to write the data into. |
| int | offset | The byte offset in |
| int | count | The maximum number of bytes to read. |
| CancellationToken | cancellationToken | The token to monitor for cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task<int> | A task that represents the asynchronous read operation. The value of the |
Overrides
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
| ArgumentException | The sum of |
| NotSupportedException | The stream does not support reading. |
| ObjectDisposedException | The stream has been disposed. |
| InvalidOperationException | The stream is currently in use by a previous read operation. |
| OperationCanceledException | The cancellation token was canceled. This exception is stored into the returned task. |
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> buffer, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Memory<byte> | buffer | The region of memory to write the data into. |
| CancellationToken | cancellationToken | The token to monitor for cancellation requests. The default value is None. |
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
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The cancellation token was canceled. This exception is stored into the returned task. |
ReadByte()
Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
Declaration
public override int ReadByte()
Returns
| Type | Description |
|---|---|
| int | The unsigned byte cast to an int, or -1 if at the end of the stream. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | The stream does not support reading. |
| ObjectDisposedException | Methods were called after the stream was closed. |
ReadByteFromBuffer()
Reads a byte from buffer.
Declaration
public byte ReadByteFromBuffer()
Returns
| Type | Description |
|---|---|
| byte |
ReadLineAsync(CancellationToken)
Read a line from the byte stream
Declaration
public Task<string?> ReadLineAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<string> |
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 |
| 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
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. |
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
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. |
Write(byte[], int, int)
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(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | An array of bytes. This method copies |
| int | offset | The zero-based byte offset in |
| int | count | The number of bytes to be written to the current stream. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The sum of |
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
| IOException | An I/O error occurred, such as the specified file cannot be found. |
| NotSupportedException | The stream does not support writing. |
| ObjectDisposedException | Write(byte[], int, int) was called after the stream was closed. |
WriteAsync(byte[], int, int, 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 Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | The buffer to write data from. |
| int | offset | The zero-based byte offset in |
| int | count | The maximum number of bytes to write. |
| CancellationToken | cancellationToken | The token to monitor for cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| Task | A task that represents the asynchronous write operation. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
| ArgumentException | The sum of |
| NotSupportedException | The stream does not support writing. |
| ObjectDisposedException | The stream has been disposed. |
| InvalidOperationException | The stream is currently in use by a previous write operation. |
| OperationCanceledException | The cancellation token was canceled. This exception is stored into the returned task. |
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> buffer, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlyMemory<byte> | buffer | The region of memory to write data from. |
| CancellationToken | cancellationToken | The token to monitor for cancellation requests. The default value is None. |
Returns
| Type | Description |
|---|---|
| ValueTask | A task that represents the asynchronous write operation. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The cancellation token was canceled. This exception is stored into the returned task. |
WriteByte(byte)
Writes a byte to the current position in the stream and advances the position within the stream by one byte.
Declaration
public override void WriteByte(byte value)
Parameters
| Type | Name | Description |
|---|---|---|
| byte | value | The byte to write to the stream. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| IOException | An I/O error occurs. |
| NotSupportedException | The stream does not support writing, or the stream is already closed. |
| ObjectDisposedException | Methods were called after the stream was closed. |
Events
| Edit this page View SourceDataRead
Declaration
public event EventHandler<DataEventArgs>? DataRead
Event Type
| Type | Description |
|---|---|
| EventHandler<DataEventArgs> |
DataWrite
Declaration
public event EventHandler<DataEventArgs>? DataWrite
Event Type
| Type | Description |
|---|---|
| EventHandler<DataEventArgs> |