Table of Contents

Class OnDisposeStream

Namespace
NetEx.IO
Assembly
NetEx.IO.dll

Creates a wrapper around a Stream that can be used to perform additional cleanup when the underlying stream is disposed.

public sealed class OnDisposeStream : Stream, IAsyncDisposable, IDisposable
Inheritance
OnDisposeStream
Implements
Inherited Members

Constructors

OnDisposeStream(Stream, Action<bool>)

Initializes a new instance of the OnDisposeStream class.

public OnDisposeStream(Stream stream, Action<bool> disposeAction)

Parameters

stream Stream

The Stream to wrap.

disposeAction Action<bool>

The Action<T> to invoke when the wrapped stream is disposed.

Properties

CanRead

Gets a value indicating whether the wrapped stream supports reading.

public override bool CanRead { get; }

Property Value

bool

true if the wrapped stream supports reading; otherwise, false.

Remarks

If the wrapped stream does not support reading, calls to the Read(byte[], int, int), ReadByte(), and BeginRead(byte[], int, int, AsyncCallback, object) methods throw a NotSupportedException.

If the wrapped stream is closed, this property may return false.

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

CanSeek

Gets a value indicating whether the wrapped stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

true if the wrapped stream supports seeking; otherwise, false.

Remarks

If the wrapped stream does not support seeking, calls to Length, SetLength(long), Position, and Seek(long, SeekOrigin) may throw a NotSupportedException.

If the wrapped stream is closed, this property may return false.

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

CanWrite

Gets a value indicating whether the wrapped stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

true if the wrapped stream supports writing; otherwise, false.

Remarks

If the wrapped stream does not support writing, a call to Write(byte[], int, int), BeginWrite(byte[], int, int, AsyncCallback, object), or WriteByte(byte) throws a NotSupportedException. In such cases, Flush() is typically implemented as an empty method to ensure full compatibility with other Stream types since it's valid to flush a read-only stream.

If the wrapped stream is closed, this property may return false.

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

Length

Gets the length in bytes of the wrapped stream.

public override long Length { get; }

Property Value

long

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

Remarks

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

Exceptions

NotSupportedException

The wrapped stream does not support seeking and the length is unknown.

IOException

An I/O error occured.

ObjectDisposedException

Methods were called after the wrapped stream was closed.

Position

Gets or sets the position within the wrapped stream.

public override long Position { get; set; }

Property Value

long

The current position within the wrapped stream.

Remarks

The wrapped stream must support seeking to get or set the position. Use the CanSeek property to determine whether the wrapped stream supports seeking.

Seeking to any location beyond the length of the wrapped stream may be supported.

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

Exceptions

NotSupportedException

The wrapped stream does not support seeking.

IOException

An I/O error occurred.

ArgumentOutOfRangeException

The position is set to a value outside the supported range of the wrapped stream.

EndOfStreamException

Attempted seeking past the end of a wrapped stream that does not support this.

ObjectDisposedException

Methods were called after the wrapped stream was closed.

Methods

Dispose(bool)

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

protected override void Dispose(bool disposing)

Parameters

disposing bool

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

Flush()

When supported by the wrapped stream, clears all buffers for the stream and causes any buffered data to be written to the underlying device.

public override void Flush()

Remarks

In a wrapped stream that doesn't support writing, Flush() is typically implemented as an empty method to ensure full compatibility with other Stream types since it's valid to flush a read-only stream.

When using the StreamWriter or BinaryWriter class, do not flush the base Stream object. Instead, use the class's Flush() or Close() method, which makes sure that the data is flushed to the underlying stream first and then written to the file.

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

Exceptions

IOException

An I/O error occurs.

ObjectDisposedException

Methods were called after the wrapped stream was closed.

Read(byte[], int, int)

Reads a sequence of bytes from the wrapped 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 wrapped stream.

offset int

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

count int

The maximum number of bytes to be read from the wrapped 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

Use the CanRead property to determine whether the current instance supports reading.

Wrapped streams will read a maximum of count bytes and store them in buffer beginning at offset. The current position within the wrapped stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the wrapped stream remains unchanged. Wrapped streams will return the number of bytes read. If more than zero bytes are requested, the wrapped 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 wrapped stream and no more is expected (such as a closed socket or end of file). A wrapped 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 stream. Refer to the documentation for the wrapped stream implementation for expected behaviour.

Exceptions

ArgumentException

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

ArgumentOutOfRangeException

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

IOException

An I/O error occurs.

NotSupportedException

The wrapped stream does not support reading.

ObjectDisposedException

Methods were called after the wrapped stream was closed.

Seek(long, SeekOrigin)

Sets the position within the wrapped 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 wrapped stream.

Remarks

Use the CanSeek property to determine whether the wrapped 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.

Seeking to any location beyond the length of the wrapped stream may be supported.

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

Exceptions

ArgumentException

There is an invalid SeekOrigin.

-or-

offset is set to a value outside the supported range of the wrapped stream.
ArgumentOutOfRangeException

The offset is set to a value outside the supported range of the wrapped stream.

IOException

An I/O error occurs.

NotSupportedException

The wrapped stream does not support seeking, such as if the stream is constructed from a pipe or console output.

ObjectDisposedException

Methods were called after the wrapped 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 wrapped stream in bytes.

Remarks

If the specified value is less than the current length of the stream, the stream is truncated. If the specified value is larger than the current length of the stream, the stream is expanded. If the stream is expanded, the contents of the stream between the old and the new length are not defined.

The wrapped stream must support both writing and seeking for SetLength to work.

Use the CanWrite property to determine whether the wrapped supports writing, and the CanSeek property to determine whether seeking is supported.

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

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The wrapped 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 wrapped stream was closed.

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

Use the CanWrite property to determine whether the wrapped stream supports writing.

If the write operation is successful, the position within the stream advances by the number of bytes written. If an exception occurs, the position within the stream should remain unchanged.

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

Exceptions

ArgumentException

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

ArgumentOutOfRangeException

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

IOException

An I/O error occurs.

NotSupportedException

The wrapped stream does not support writing.

ObjectDisposedException

Methods were called after the wrapped stream was closed.