Stream Class
Namespace: System.IO
Summary
Represents a base class for all streams. This is an abstract class.
Remarks
A stream is a sequence of bytes, and a direct successor to the stream can be any device that has bytes associated with it—input, output, or both. For example, a file stream is an Stream to a file on the disk. A network stream is a Stream over the network. Memory-backed streams are also Streams. The Stream class and its derived classes provide the core functionality for working with streams of data.
The Stream class is an abstract class that provides a generic view of a sequence of bytes. You cannot instantiate the Stream class directly. Instead, create an instance of a derived class such as MemoryStream, FileStream, or NetworkStream.
Members
-
CanWrite bool CanWrite { get; }
Gets a value indicating whether the current stream supports writing.
-
BeginRead IAsyncResult BeginRead(...)
Asynchronously reads the bytes from the current stream and writes them to a buffer.
-
BeginWrite IAsyncResult BeginWrite(...)
Asynchronously writes bytes to the current stream from a buffer.
-
EndRead int EndRead(...)
Confirms that an asynchronous read operation has completed and returns the number of bytes read.
-
Flush void Flush()
Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
-
Seek long Seek(long offset, SeekOrigin origin)
Sets the current position of this stream to the given value.
-
Read int Read(byte[] buffer, int offset, int count)
When overridden in a derived class, reads a sequence of bytes from the current stream into an array of bytes, starting at the specified offset in the array and for the specified number of bytes.
-
Write void Write(byte[] buffer, int offset, int count)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream according to the number of bytes written.
Properties
CanRead
Gets a value indicating whether the current stream supports reading.
public virtual bool CanRead { get; }
CanSeek
Gets a value indicating whether the current stream supports seeking.
public virtual bool CanSeek { get; }
CanWrite
Gets a value indicating whether the current stream supports writing.
public virtual bool CanWrite { get; }
Length
Gets the length in bytes, in bytes.
public virtual long Length { get; }
Position
Gets or sets the current position within the stream.
public virtual long Position { get; set; }
Methods
BeginRead
Asynchronously reads the bytes from the current stream and writes them to a buffer.
public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
Parameters:
buffer: The buffer to read data into.offset: The zero-based position in buffer at which to begin storing the data read from the current stream.count: The maximum number of bytes to read.callback: An AsyncCallback delegate that references the method to call when the read operation is complete.state: A user-provided object that distinguishes this particular call to BeginRead from other calls with the same buffer.
Returns: An IAsyncResult that represents the asynchronous read, which could still be pending.
BeginWrite
Asynchronously writes bytes to the current stream from a buffer.
public virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
Parameters:
buffer: The buffer to write data from.offset: The zero-based position in buffer from which to begin writing data.count: The number of bytes to write.callback: An AsyncCallback delegate that references the method to call when the write operation is complete.state: A user-provided object that distinguishes this particular call to BeginWrite from other calls with the same buffer.
Close
Releases the unmanaged resources and closes the stream.
public virtual void Close()
CopyTo
Copies the current stream to another stream.
public virtual void CopyTo(Stream destination)
Parameters:
destination: The stream to which the current stream will be copied.
Dispose
Releases all resources used by the current instance of the Stream class.
public void Dispose()
EndRead
Confirms that an asynchronous read operation has completed and returns the number of bytes read.
public virtual int EndRead(IAsyncResult asyncResult)
Parameters:
asyncResult: An IAsyncResult that represents the asynchronous call.
Returns: The number of bytes read from the stream.
EndWrite
Confirms that an asynchronous write operation has completed.
public virtual void EndWrite(IAsyncResult asyncResult)
Parameters:
asyncResult: An IAsyncResult that represents the asynchronous call.
Flush
Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
public virtual void Flush()
Seek
Sets the current position of this stream to the given value.
public virtual long Seek(long offset, SeekOrigin origin)
Parameters:
offset: A number of bytes to move.origin: A reference point relative to which the new position is obtained.
Returns: The new position within the current stream.
SetLength
Sets the length of this stream to the given value.
public virtual void SetLength(long value)
Parameters:
value: The desired length of the stream.
Read
When overridden in a derived class, reads a sequence of bytes from the current stream into an array of bytes, starting at the specified offset in the array and for the specified number of bytes.
public abstract int Read(byte[] buffer, int offset, int count)
Parameters:
buffer: The buffer to read data into.offset: The zero-based position in buffer at which to begin storing the data read from the current stream.count: The maximum number of bytes to read.
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.
Write
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream according to the number of bytes written.
public abstract void Write(byte[] buffer, int offset, int count)
Parameters:
buffer: The buffer to write data from.offset: The zero-based position in buffer from which to begin writing data.count: The number of bytes to write.