Show / Hide Table of Contents

Class ModernStream

Represents a modern base class for custom streams that requires only necessary abstract methods to work correctly.

Inheritance
object
MarshalByRefObject
Stream
ModernStream
FileBufferingWriter
PoolingBufferedStream
RandomAccessStream
Implements
IAsyncDisposable
IDisposable
IFlushable
Inherited Members
Stream.Null
Stream.Close()
Stream.CopyTo(Stream)
Stream.CopyTo(Stream, int)
Stream.CopyToAsync(Stream)
Stream.CopyToAsync(Stream, int)
Stream.CopyToAsync(Stream, int, CancellationToken)
Stream.CopyToAsync(Stream, CancellationToken)
Stream.CreateWaitHandle()
Stream.Dispose()
Stream.Dispose(bool)
Stream.DisposeAsync()
Stream.Flush()
Stream.FlushAsync()
Stream.FlushAsync(CancellationToken)
Stream.ObjectInvariant()
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.Seek(long, SeekOrigin)
Stream.SetLength(long)
Stream.Synchronized(Stream)
Stream.ValidateBufferArguments(byte[], int, int)
Stream.ValidateCopyToArguments(Stream, int)
Stream.WriteAsync(byte[], int, int)
Stream.CanRead
Stream.CanSeek
Stream.CanTimeout
Stream.CanWrite
Stream.Length
Stream.Position
Stream.ReadTimeout
Stream.WriteTimeout
MarshalByRefObject.GetLifetimeService()
MarshalByRefObject.InitializeLifetimeService()
MarshalByRefObject.MemberwiseClone(bool)
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext.IO
Assembly: DotNext.dll
Syntax
public abstract class ModernStream : Stream, IAsyncDisposable, IDisposable, IFlushable

Methods

| Edit this page View Source

BeginRead(byte[], int, int, AsyncCallback?, object?)

Begins an asynchronous read operation. (Consider using ReadAsync(byte[], int, int) instead.)

Declaration
public override sealed IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
Parameters
Type Name Description
byte[] buffer

The buffer to read the data into.

int offset

The byte offset in buffer at which to begin writing data read from the stream.

int count

The maximum number of bytes to read.

AsyncCallback callback

An optional asynchronous callback, to be called when the read is complete.

object state

A user-provided object that distinguishes this particular asynchronous read request from other requests.

Returns
Type Description
IAsyncResult

An IAsyncResult that represents the asynchronous read, which could still be pending.

Overrides
Stream.BeginRead(byte[], int, int, AsyncCallback, object)
Exceptions
Type Condition
IOException

Attempted an asynchronous read past the end of the stream, or a disk error occurs.

ArgumentException

One or more of the arguments is invalid.

ObjectDisposedException

Methods were called after the stream was closed.

NotSupportedException

The current Stream implementation does not support the read operation.

| Edit this page View Source

BeginWrite(byte[], int, int, AsyncCallback?, object?)

Begins an asynchronous write operation. (Consider using WriteAsync(byte[], int, int) instead.)

Declaration
public override sealed IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
Parameters
Type Name Description
byte[] buffer

The buffer to write data from.

int offset

The byte offset in buffer from which to begin writing.

int count

The maximum number of bytes to write.

AsyncCallback callback

An optional asynchronous callback, to be called when the write is complete.

object state

A user-provided object that distinguishes this particular asynchronous write request from other requests.

Returns
Type Description
IAsyncResult

An IAsyncResult that represents the asynchronous write, which could still be pending.

Overrides
Stream.BeginWrite(byte[], int, int, AsyncCallback, object)
Exceptions
Type Condition
IOException

Attempted an asynchronous write past the end of the stream, or a disk error occurs.

ArgumentException

One or more of the arguments is invalid.

ObjectDisposedException

Methods were called after the stream was closed.

NotSupportedException

The current Stream implementation does not support the write operation.

| Edit this page View Source

EndRead(IAsyncResult)

Waits for the pending asynchronous read to complete. (Consider using ReadAsync(byte[], int, int) instead.)

Declaration
public override sealed int EndRead(IAsyncResult asyncResult)
Parameters
Type Name Description
IAsyncResult asyncResult

The reference to the pending asynchronous request to finish.

Returns
Type Description
int

The number of bytes read from the stream, between zero (0) and the number of bytes requested. ReadAsync returns zero (0) only if zero bytes were requested or if no more bytes will be available because it's at the end of the stream; otherwise, read operations do not complete until at least one byte is available. If zero bytes are requested, read operations may complete immediately or may not complete until at least one byte is available (but without consuming any data).

Overrides
Stream.EndRead(IAsyncResult)
Exceptions
Type Condition
ArgumentNullException

asyncResult is null.

ArgumentException

A handle to the pending read operation is not available.

-or-

The pending operation does not support reading.

InvalidOperationException

asyncResult did not originate from a BeginRead(byte[], int, int, AsyncCallback, object) method on the current stream.

IOException

The stream is closed or an internal error has occurred.

| Edit this page View Source

EndWrite(IAsyncResult)

Ends an asynchronous write operation. (Consider using WriteAsync(byte[], int, int) instead.)

Declaration
public override sealed void EndWrite(IAsyncResult asyncResult)
Parameters
Type Name Description
IAsyncResult asyncResult

A reference to the outstanding asynchronous I/O request.

Overrides
Stream.EndWrite(IAsyncResult)
Exceptions
Type Condition
ArgumentNullException

asyncResult is null.

ArgumentException

A handle to the pending write operation is not available.

-or-

The pending operation does not support writing.

InvalidOperationException

asyncResult did not originate from a BeginWrite(byte[], int, int, AsyncCallback, object) method on the current stream.

IOException

The stream is closed or an internal error has occurred.

| Edit this page View Source

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 sealed 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 offset and (offset + count - 1) replaced by the bytes read from the current source.

int offset

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

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 count is 0 or the end of the stream has been reached.

Overrides
Stream.Read(byte[], int, int)
Exceptions
Type Condition
ArgumentException

The sum of offset and count is larger than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

| 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 abstract int Read(Span<byte> buffer)
Parameters
Type Name Description
Span<byte> buffer

A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.

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
Stream.Read(Span<byte>)
| Edit this page View Source

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 sealed Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken token)
Parameters
Type Name Description
byte[] buffer

The buffer to write the data into.

int offset

The byte offset in buffer at which to begin writing data from the stream.

int count

The maximum number of bytes to read.

CancellationToken token
Returns
Type Description
Task<int>

A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if count is 0 or if the end of the stream has been reached.

Overrides
Stream.ReadAsync(byte[], int, int, CancellationToken)
Exceptions
Type Condition
ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

The sum of offset and count is larger than the buffer length.

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.

| 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 abstract ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken token = default)
Parameters
Type Name Description
Memory<byte> buffer

The region of memory to write the data into.

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
Stream.ReadAsync(Memory<byte>, CancellationToken)
| Edit this page View Source

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 sealed int ReadByte()
Returns
Type Description
int

The unsigned byte cast to an int, or -1 if at the end of the stream.

Overrides
Stream.ReadByte()
Exceptions
Type Condition
NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

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 sealed void Write(byte[] buffer, int offset, int count)
Parameters
Type Name Description
byte[] buffer

An array of bytes. This method copies count bytes from buffer to the current stream.

int offset

The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

int count

The number of bytes to be written to the current stream.

Overrides
Stream.Write(byte[], int, int)
Exceptions
Type Condition
ArgumentException

The sum of offset and count is greater than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

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.

| 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 abstract void Write(ReadOnlySpan<byte> buffer)
Parameters
Type Name Description
ReadOnlySpan<byte> buffer

A region of memory. This method copies the contents of this region to the current stream.

Overrides
Stream.Write(ReadOnlySpan<byte>)
| Edit this page View Source

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 sealed Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken token)
Parameters
Type Name Description
byte[] buffer

The buffer to write data from.

int offset

The zero-based byte offset in buffer from which to begin copying bytes to the stream.

int count

The maximum number of bytes to write.

CancellationToken token
Returns
Type Description
Task

A task that represents the asynchronous write operation.

Overrides
Stream.WriteAsync(byte[], int, int, CancellationToken)
Exceptions
Type Condition
ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

The sum of offset and count is larger than the buffer length.

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.

| 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 abstract ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken token = default)
Parameters
Type Name Description
ReadOnlyMemory<byte> buffer

The region of memory to write data from.

CancellationToken token
Returns
Type Description
ValueTask

A task that represents the asynchronous write operation.

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

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 sealed void WriteByte(byte value)
Parameters
Type Name Description
byte value

The byte to write to the stream.

Overrides
Stream.WriteByte(byte)
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.

Implements

IAsyncDisposable
IDisposable
IFlushable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(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
☀
☾