Class MultiStream
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
streamsIEnumerable<Stream>The collection of Stream instances to wrap.
MultiStream(params Stream[])
Creates a new MultiStream instance.
public MultiStream(params Stream[] streams)
Parameters
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
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
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
disposingbooltrue 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
disposingbooltrue 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
bufferbyte[]An array of bytes. When this method returns, the buffer contains the specified byte array with the values between
offsetand (offset+count- 1) replaced by the bytes read from the stream.offsetintThe zero-based byte offset in
bufferat which to begin storing data from the stream.countintThe 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
countis 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
offsetandcountis outside the supported range of the stream.- ArgumentOutOfRangeException
The
offsetorcountare 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
offsetlongA byte offset relative to the
originparameter.originSeekOriginA 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
valuelongThe 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
bufferbyte[]An array of bytes. This method copies
countbytes frombufferto the current stream.offsetintThe zero-based byte offset in
bufferat which to begin copying bytes to the current stream.countintThe 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