Class Stream
Provides a generic view of a sequence of bytes. This is an abstract class.
Implements
Inherited Members
Namespace: System.IO
Assembly: System.IO.Streams.dll
Syntax
public abstract class Stream : MarshalByRefObject, IDisposable
Constructors
Stream()
Declaration
protected Stream()
Properties
CanRead
When overridden in a derived class, gets a value indicating whether the current stream supports reading.
Declaration
public abstract bool CanRead { get; }
Property Value
Type | Description |
---|---|
bool | 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.
Declaration
public abstract bool CanSeek { get; }
Property Value
Type | Description |
---|---|
bool | true if the stream supports seeking; otherwise, false. |
CanTimeout
Gets a value that determines whether the current stream can time out.
Declaration
public virtual bool CanTimeout { get; }
Property Value
Type | Description |
---|---|
bool | A value that determines whether the current stream can time out. |
CanWrite
When overridden in a derived class, gets a value indicating whether the current stream supports writing.
Declaration
public abstract bool CanWrite { get; }
Property Value
Type | Description |
---|---|
bool | true if the stream supports writing; otherwise, false. |
Length
When overridden in a derived class, gets the length in bytes of the stream.
Declaration
public abstract long Length { get; }
Property Value
Type | Description |
---|---|
long | A long value representing the length of the stream in bytes. |
Position
When overridden in a derived class, gets or sets the position within the current stream.
Declaration
public abstract long Position { get; set; }
Property Value
Type | Description |
---|---|
long | The current position within the stream. |
ReadTimeout
Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.
Declaration
public virtual int ReadTimeout { get; set; }
Property Value
Type | Description |
---|---|
int | A value, in milliseconds, that determines how long the stream will attempt to read before timing out. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException |
WriteTimeout
Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.
Declaration
public virtual int WriteTimeout { get; set; }
Property Value
Type | Description |
---|---|
int | A value, in milliseconds, that determines how long the stream will attempt to write before timing out. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException |
Methods
Close()
Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.
Declaration
public virtual void Close()
Remarks
Stream used to require that all cleanup logic went into Close(), which was thought up before we invented IDisposable. However, we need to follow the IDisposable pattern so that users can write sensible subclasses without needing to inspect all their base classes, and without worrying about version brittleness, from a base class switching to the Dispose pattern. We're moving Stream to the Dispose(bool) pattern - that's where all subclasses should put their cleanup starting in V2.
CopyTo(Stream)
Reads the bytes from the current stream and writes them to another stream.
Declaration
public void CopyTo(Stream destination)
Parameters
Type | Name | Description |
---|---|---|
Stream | destination | The stream to which the contents of the current stream will be copied. |
Remarks
Copying begins at the current position in the current stream, and does not reset the position of the destination stream after the copy operation is complete.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
NotSupportedException | The current stream does not support reading. -or-
|
ObjectDisposedException | Either the current stream or |
IOException | An I/O error occurred. |
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public void Dispose()
Dispose(bool)
Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | true to release both managed and unmanaged resources; false to release only unmanaged resources. |
Finalize()
Declaration
protected override void Finalize()
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.
Declaration
public abstract void Flush()
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 abstract 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 the end of the stream has been reached. |
Read(SpanByte)
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 abstract int Read(SpanByte buffer)
Parameters
Type | Name | Description |
---|---|---|
SpanByte | 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 number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. |
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 virtual int ReadByte()
Returns
Type | Description |
---|---|
int | The unsigned byte cast to an Int32, or -1 if at the end of the stream. |
Seek(long, SeekOrigin)
When overridden in a derived class, sets the position within the current stream.
Declaration
public abstract long Seek(long offset, SeekOrigin origin)
Parameters
Type | Name | Description |
---|---|---|
long | offset | A byte offset relative to the origin parameter. |
SeekOrigin | origin | A value of type SeekOrigin indicating the reference point used to obtain the new position. |
Returns
Type | Description |
---|---|
long | The new position within the current stream. |
SetLength(long)
When overridden in a derived class, sets the length of the current stream.
Declaration
public abstract void SetLength(long value)
Parameters
Type | Name | Description |
---|---|---|
long | value | The desired length of the current stream in bytes. |
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 abstract 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. |
WriteByte(byte)
Writes a byte to the current position in the stream and advances the position within the stream by one byte.
Declaration
public virtual void WriteByte(byte value)
Parameters
Type | Name | Description |
---|---|---|
byte | value | The byte to write to the stream. |