Read Method

BufferedStream.Read Method (Byte[], Int32, Int32)


Copies bytes from the current buffered stream to an array.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public override int Read(
	byte[] array,
	int offset,
	int count


Type: System.Byte[]

The buffer to which bytes are to be copied.

Type: System.Int32

The byte offset in the buffer at which to begin reading bytes.

Type: System.Int32

The number of bytes to be read.

Return Value

Type: System.Int32

The total number of bytes read into array. This can be less than the number of bytes requested if that many bytes are not currently available, or 0 if the end of the stream has been reached before any data can be read.

Exception Condition

Length of array minus offset is less than count.


array is null.


offset or count is negative.


The stream is not open or is null.


The stream does not support reading.


Methods were called after the stream was closed.

The Read method will return 0 only if the end of the stream is reached. In all other cases, Read always reads at least one byte from the stream before returning. By definition, if no data is available from the stream upon a call to Read, the Read method returns 0 (the end of the stream is reached automatically). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

Use BinaryReader for reading primitive data types.

This code example is part of a larger example provided for the BufferedStream class.

// Receive data using the BufferedStream.
Console.WriteLine("Receiving data using BufferedStream.");
bytesReceived = 0;
startTime = DateTime.Now;

int numBytesToRead = receivedData.Length;

while (numBytesToRead > 0)
    // Read may return anything from 0 to numBytesToRead.
    int n = bufStream.Read(receivedData,0, receivedData.Length);
    // The end of the file is reached.
    if (n == 0)
    bytesReceived += n;
    numBytesToRead -= n;

bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes received in {1} seconds.\n",

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft