System.IO.Stream.Read Method

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.

Syntax

public abstract int Read (byte[] buffer, int offset, int count)

Parameters

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.
offset
The zero-based byte offset in buffer at which to begin storing the data read from the current stream.
count
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

TypeReason
ArgumentException(offset + count - 1) is greater than the length of buffer.
ArgumentNullException buffer is null.
ArgumentOutOfRangeException offset or count is less than zero.
System.IO.IOException An I/O error occurred.
NotSupportedExceptionThe current stream does not support reading.
ObjectDisposedExceptionThe stream is closed.

Remarks

Use the Stream.CanRead property to determine whether the current instance supports reading. Use the Stream.ReadAsync(Byte[], int, int) method to read asynchronously from the current stream.

Implementations of this method read a maximum of count bytes from the current stream and store them in buffer beginning at offset. The current position within the stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the stream remains unchanged. Implementations return the number of bytes read. The implementation will block until at least one byte of data can be read, in the event that no data is available. Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

Use System.IO.BinaryReader for reading primitive data types.

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0