Table of Contents

Class MultiStream

Namespace
NetEx.IO
Assembly
NetEx.IO.dll

Creates a wrapper around multiple Stream instances, and presents them as a single, read-only stream.

public class MultiStream : Stream, IAsyncDisposable, IDisposable
Inheritance
MultiStream
Implements
Inherited Members

Constructors

MultiStream(IEnumerable<Stream>)

Creates a new MultiStream instance.

public MultiStream(IEnumerable<Stream> streams)

Parameters

streams IEnumerable<Stream>

The collection of Stream instances to wrap.

MultiStream(params Stream[])

Creates a new MultiStream instance.

public MultiStream(params Stream[] streams)

Parameters

streams Stream[]

The collection of Stream instances to wrap.

Properties

ActiveStreamName

The name of the Stream currently being accessed within the MultiStream, based on Position.

public string? ActiveStreamName { get; }

Property Value

string

The name of the Stream currently being accessed within the MultiStream.

Exceptions

ObjectDisposedException

Methods were called after the stream was closed.

CanRead

Gets a value indicating whether the stream supports reading.

public override sealed bool CanRead { get; }

Property Value

bool

true if the stream supports reading; otherwise, false.

Remarks

If the stream is closed, this property will return false.

CanSeek

Gets a value indicating whether the stream supports seeking.

public override sealed bool CanSeek { get; }

Property Value

bool

true if the stream supports seeking; otherwise, false.

Remarks

If the stream is closed, this property will return false.

CanTimeout

Gets a value indicating whether the stream can time out.

public override sealed bool CanTimeout { get; }

Property Value

bool

MultiStream does not support streams that can time out, so this property will always return false.

CanWrite

Gets a value indicating whether the stream supports writing.

public override sealed bool CanWrite { get; }

Property Value

bool

MultiStream does not support writing, so this property will always return false.

Length

Gets the length in bytes of the stream.

public override sealed long Length { get; }

Property Value

long

A long value representing the length of the stream in bytes.

Exceptions

ObjectDisposedException

Methods were called after the stream was closed.

Position

Gets or sets the position within the stream.

public override sealed long Position { get; set; }

Property Value

long

The current position within the stream.

Exceptions

ArgumentOutOfRangeException

Attempted to set the position to a negative value.

EndOfStreamException

Attempted seeking past the end of the stream.

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.

public override sealed int ReadTimeout { get; }

Property Value

int

A value, in milliseconds, that determines how long the stream will attempt to read before timing out.

Exceptions

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.

public override sealed int WriteTimeout { get; }

Property Value

int

A value, in milliseconds, that determines how long the stream will attempt to write before timing out.

Exceptions

InvalidOperationException

The WriteTimeout method always throws an InvalidOperationException.

Methods

Dispose(bool)

Releases the unmanaged resources used by the Stream and optionally releases the managed resources.

protected override sealed void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Flush()

Overrides the Flush() method so that no action is performed.

public override void Flush()

Remarks

This method overrides the Stream.Flush method.

Because MultiStream objects are read-only, this method is redundant.

OnDispose(bool)

Allows derived classes to dispose of any resources when the class is disposed or finalized.

protected virtual void OnDispose(bool disposing)

Parameters

disposing bool

true to indicate that this method has been called during disposal, or false to indicate the method has been called during finalization.

Read(byte[], int, int)

Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

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 stream.

offset int

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

count int

The maximum number of bytes to be read from the stream.

Returns

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.

Remarks

MultiStreams will read a maximum of count bytes and store them in buffer beginning at offset. The current position within the stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the stream remains unchanged. MultiStreams will return the number of bytes read. If more than zero bytes are requested, the stream will not complete the operation until at least one byte of data can be read (some streams may similarly not complete until at least one byte is available even if zero bytes were requested, but no data will be consumed from the stream in such a case). Read(byte[], int, int) returns 0 only if zero bytes were requested or when there is no more data in the stream and no more is expected (such as a closed socket or end of file). A stream may return fewer bytes than requested even if the end of the stream has not been reached.

Use BinaryReader for reading primitive data types.

Please note that the behaviour described is not guaranteed as it is dependent upon the implementation of the wrapped streams. Refer to the documentation for the wrapped stream implementations for expected behaviour.

Exceptions

ArgumentException

The sum of offset and count is outside the supported range of the stream.

ArgumentOutOfRangeException

The offset or count are set to a value outside the supported range of the stream.

IOException

An I/O error occurs.

ObjectDisposedException

Methods were called after the stream was closed.

Seek(long, SeekOrigin)

Sets the position within the stream.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

A byte offset relative to the origin parameter.

origin SeekOrigin

A value of type SeekOrigin indicating the reference point used to obtain the new position.

Returns

long

The new position within the stream.

Remarks

Use the CanSeek property to determine whether the stream supports seeking.

If offset is negative, the new position is required to precede the position specified by origin by the number of bytes specified by offset.If offset is zero(0), the new position is required to be the position specified by origin.If offset is positive, the new position is required to follow the position specified by origin by the number of bytes specified by offset.

Exceptions

ArgumentException

There is an invalid SeekOrigin.

ArgumentOutOfRangeException

Attempted to set the position to a negative value.

EndOfStreamException

Attempted seeking past the end of the stream.

ObjectDisposedException

Methods were called after the stream was closed.

SetLength(long)

Sets the length of the wrapped stream

public override void SetLength(long value)

Parameters

value long

The desired length of the stream in bytes.

Remarks

MultiStreams are read-only, so this method will always throw a NotSupportedException.

Exceptions

NotSupportedException

The stream does not support both writing and seeking.

Write(byte[], int, int)

Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

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

offset int

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

count int

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

Remarks

MultiStreams are read-only, so this method will always throw a NotSupportedException.

Exceptions

NotSupportedException

The stream does not support writing.

Events

ActiveStreamNameChanged

Occurs when the wrapped stream currently being read from changes.

public event EventHandler? ActiveStreamNameChanged

Event Type

EventHandler