Class Stream
Provides a generic view of a sequence of bytes. This is an abstract class.
public abstract class Stream : MarshalByRefObject, IDisposable
- Inheritance
-
Stream
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
Stream()
protected Stream()
Properties
CanRead
When overridden in a derived class, gets a value indicating whether the current stream supports reading.
public abstract bool CanRead { get; }
Property Value
- 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.
public abstract bool CanSeek { get; }
Property Value
- bool
true if the stream supports seeking; otherwise, false.
CanTimeout
Gets a value that determines whether the current stream can time out.
public virtual bool CanTimeout { get; }
Property Value
- 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.
public abstract bool CanWrite { get; }
Property Value
- bool
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; }
Property Value
- 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.
public abstract long Position { get; set; }
Property Value
- 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.
public virtual int ReadTimeout { get; set; }
Property Value
- int
A value, in milliseconds, that determines how long the stream will attempt to read before timing out.
Exceptions
WriteTimeout
Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.
public virtual int WriteTimeout { get; set; }
Property Value
- int
A value, in milliseconds, that determines how long the stream will attempt to write before timing out.
Exceptions
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.
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.
public void CopyTo(Stream destination)
Parameters
destination
StreamThe 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
- ArgumentNullException
destination
is null.- NotSupportedException
The current stream does not support reading.
-or-
destination
does not support writing.- ObjectDisposedException
Either the current stream or
destination
were closed before the CopyTo(Stream) method was called.- IOException
An I/O error occurred.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
protected virtual void Dispose(bool disposing)
Parameters
disposing
booltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
Finalize()
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.
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.
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
intThe zero-based byte offset in buffer at which to begin storing the data read from the current stream.
count
intThe maximum number of bytes to be read from the current 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 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.
public abstract int Read(SpanByte buffer)
Parameters
buffer
SpanByteA region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.
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 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.
public virtual int ReadByte()
Returns
- 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.
public abstract long Seek(long offset, SeekOrigin origin)
Parameters
offset
longA byte offset relative to the origin parameter.
origin
SeekOriginA value of type SeekOrigin indicating the reference point used to obtain the new position.
Returns
- long
The new position within the current stream.
SetLength(long)
When overridden in a derived class, sets the length of the current stream.
public abstract void SetLength(long value)
Parameters
value
longThe 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.
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
intThe zero-based byte offset in buffer at which to begin copying bytes to the current stream.
count
intThe 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.
public virtual void WriteByte(byte value)
Parameters
value
byteThe byte to write to the stream.