Stream Class

Definition

Provides a generic view of a sequence of bytes. This is an abstract class.

[Serializable]
[ComVisible(true)]
public abstract class Stream : IDisposable
Inheritance
Stream
Derived

Inherited Members

System.Object

Constructors

Stream()

Initializes a new instance of the Stream class.

protected Stream()

Fields

Null

A Stream with no backing store.

public static readonly Stream Null

Properties

CanRead

When overridden in a derived class, gets a value indicating whether the current stream supports reading.

public abstract bool CanRead { get; }
Value
Boolean

true if the stream supports reading; otherwise, false.

CanSeek

When overridden in a derived class, gets a value indicating whether the current stream supports seeking.

public abstract bool CanSeek { get; }
Value
Boolean

true if the stream supports seeking; otherwise, false.

CanTimeout

Gets a value that determines whether the current stream can time out.

[ComVisible(false)]
public virtual bool CanTimeout { get; }
Value
Boolean

A value that determines whether the current stream can time out.

See Also

CanWrite

When overridden in a derived class, gets a value indicating whether the current stream supports writing.

public abstract bool CanWrite { get; }
Value
Boolean

true if the stream supports writing; otherwise, false.

Length

When overridden in a derived class, gets the length in bytes of the stream.

public abstract long Length { get; }
Value
Int64

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

Exceptions

A class derived from Stream does not support seeking.

Methods were called after the stream was closed.

Position

When overridden in a derived class, gets or sets the position within the current stream.

public abstract long Position { get; set; }
Value
Int64

The current position within the stream.

Exceptions

An I/O error occurs.

The stream does not support seeking.

Methods were called after the stream was closed.

ReadTimeout

Gets or sets a value, in miliseconds, that determines how long the stream will attempt to read before timing out.

[ComVisible(false)]
public virtual int ReadTimeout { get; set; }
Value
Int32

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

Exceptions

WriteTimeout

Gets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out.

[ComVisible(false)]
public virtual int WriteTimeout { get; set; }
Value
Int32

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

Exceptions

Methods

CopyTo(Stream)

Reads the bytes from the current stream and writes them to another stream.

public void CopyTo(Stream destination)
Parameters
destination
Stream

The stream to which the contents of the current stream will be copied.

Exceptions

destination is null.

The current stream does not support reading.

-or-

destination does not support writing.

Either the current stream or destination were closed before the CopyTo(Stream) method was called.

An I/O error occurred.

CopyTo(Stream, Int32)

Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

public virtual void CopyTo(Stream destination, int bufferSize)
Parameters
destination
Stream

The stream to which the contents of the current stream will be copied.

bufferSize
Int32

The size of the buffer. This value must be greater than zero. The default size is 81920.

Exceptions

destination is null.

bufferSize is negative or zero.

The current stream does not support reading.

-or-

destination does not support writing.

Either the current stream or destination were closed before the CopyTo(Stream) method was called.

An I/O error occurred.

CopyToAsync(Stream)

Asynchronously reads the bytes from the current stream and writes them to another stream.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public Task CopyToAsync(Stream destination)
Parameters
destination
Stream

The stream to which the contents of the current stream will be copied.

Returns

A task that represents the asynchronous copy operation.

Exceptions

destination is null.

Either the current stream or the destination stream is disposed.

The current stream does not support reading, or the destination stream does not support writing.

CopyToAsync(Stream, Int32)

Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public Task CopyToAsync(Stream destination, int bufferSize)
Parameters
destination
Stream

The stream to which the contents of the current stream will be copied.

bufferSize
Int32

The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.

Returns

A task that represents the asynchronous copy operation.

Exceptions

destination is null.

buffersize is negative or zero.

Either the current stream or the destination stream is disposed.

The current stream does not support reading, or the destination stream does not support writing.

CopyToAsync(Stream, Int32, CancellationToken)

Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
Parameters
destination
Stream

The stream to which the contents of the current stream will be copied.

bufferSize
Int32

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

A task that represents the asynchronous copy operation.

Exceptions

destination is null.

buffersize is negative or zero.

Either the current stream or the destination stream is disposed.

The current stream does not support reading, or the destination stream does not support writing.

Dispose()

Releases all resources used by the Stream.

public void Dispose()

Dispose(Boolean)

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

protected virtual void Dispose(bool disposing)
Parameters
disposing
Boolean

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

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.

public abstract void Flush()
Exceptions

An I/O error occurs.

FlushAsync()

Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public Task FlushAsync()
Returns

A task that represents the asynchronous flush operation.

Exceptions

The stream has been disposed.

FlushAsync(CancellationToken)

Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task FlushAsync(CancellationToken cancellationToken)
Parameters
cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

A task that represents the asynchronous flush operation.

Exceptions

The stream has been disposed.

Read(Byte[], Int32, Int32)

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.

public abstract 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 current source.

offset
Int32

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

count
Int32

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

Returns

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

Exceptions

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

buffer is null.

offset or count is negative.

An I/O error occurs.

The stream does not support reading.

Methods were called after the stream was closed.

ReadAsync(Byte[], Int32, Int32)

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

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public Task<int> ReadAsync(byte[] buffer, int offset, int count)
Parameters
buffer
Byte[]

The buffer to write the data into.

offset
Int32

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

count
Int32

The maximum number of bytes to read.

Returns

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

Exceptions

buffer is null.

offset or count is negative.

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

The stream does not support reading.

The stream has been disposed.

The stream is currently in use by a previous read operation.

ReadAsync(Byte[], Int32, Int32, 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.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Parameters
buffer
Byte[]

The buffer to write the data into.

offset
Int32

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

count
Int32

The maximum number of bytes to read.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

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

Exceptions

buffer is null.

offset or count is negative.

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

The stream does not support reading.

The stream has been disposed.

The stream is currently in use by a previous read operation.

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.

public virtual int ReadByte()
Returns

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

Exceptions

The stream does not support reading.

Methods were called after the stream was closed.

Seek(Int64, SeekOrigin)

When overridden in a derived class, sets the position within the current stream.

public abstract long Seek(long offset, SeekOrigin origin)
Parameters
offset
Int64

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

The new position within the current stream.

Exceptions

An I/O error occurs.

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

Methods were called after the stream was closed.

SetLength(Int64)

When overridden in a derived class, sets the length of the current stream.

public abstract void SetLength(long value)
Parameters
value
Int64

The desired length of the current stream in bytes.

Exceptions

An I/O error occurs.

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

Methods were called after the stream was closed.

Write(Byte[], Int32, Int32)

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.

public abstract 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
Int32

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

count
Int32

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

Exceptions

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

buffer is null.

offset or count is negative.

An I/O error occured, such as the specified file cannot be found.

The stream does not support writing.

Write(Byte[], Int32, Int32) was called after the stream was closed.

WriteAsync(Byte[], Int32, Int32)

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

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public Task WriteAsync(byte[] buffer, int offset, int count)
Parameters
buffer
Byte[]

The buffer to write data from.

offset
Int32

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

count
Int32

The maximum number of bytes to write.

Returns

A task that represents the asynchronous write operation.

Exceptions

buffer is null.

offset or count is negative.

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

The stream does not support writing.

The stream has been disposed.

The stream is currently in use by a previous write operation.

WriteAsync(Byte[], Int32, Int32, 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.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Parameters
buffer
Byte[]

The buffer to write data from.

offset
Int32

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

count
Int32

The maximum number of bytes to write.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

A task that represents the asynchronous write operation.

Exceptions

buffer is null.

offset or count is negative.

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

The stream does not support writing.

The stream has been disposed.

The stream is currently in use by a previous write operation.

WriteByte(Byte)

Writes a byte to the current position in the stream and advances the position within the stream by one byte.

public virtual void WriteByte(byte value)
Parameters
value
Byte

The byte to write to the stream.

Exceptions

An I/O error occurs.

The stream does not support writing, or the stream is already closed.

Methods were called after the stream was closed.