Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

BufferedStream.Read Method

Copies bytes from the current buffered stream to an array.

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

public override int Read (
	[InAttribute] [OutAttribute] byte[] array,
	int offset,
	int count
)
public int Read (
	/** @attribute InAttribute() */ /** @attribute OutAttribute() */ byte[] array, 
	int offset, 
	int count
)
public override function Read (
	array : byte[], 
	offset : int, 
	count : int
) : int

Parameters

array

The buffer to which bytes are to be copied.

offset

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

count

The number of bytes to be read.

Return Value

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 typeCondition

ArgumentException

Length of array minus offset is less than count.

ArgumentNullException

array is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

offset or count is negative.

IOException

The stream is not open or is a null reference (Nothing in Visual Basic).

NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

For an example of creating a file and writing text to a file, see How to: Write Text to a File. For an example of reading text from a file, see How to: Read Text from a File. For an example of reading from and writing to a binary file, see How to: Read and Write to a Newly Created Data File.

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;
while(bytesReceived < numberOfLoops * receivedData.Length)
{
    bytesReceived += bufStream.Read(
        receivedData, 0, receivedData.Length);
}
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes received in {1} seconds.\n",
    bytesReceived.ToString(), 
    bufferedTime.ToString("F1"));

// Receive data using the BufferedStream.
Console.WriteLine("Receiving data using BufferedStream.");
bytesReceived = 0;
startTime = DateTime.get_Now();
while((bytesReceived < numberOfLoops * receivedData.length)) {
    bytesReceived += bufStream.Read(receivedData,0,receivedData.length);
}
bufferedTime =
    ((DateTime.get_Now()).Subtract(startTime)).get_TotalSeconds();
Console.WriteLine("{0} bytes received in {1} seconds.\n",
    (new Integer(bytesReceived)).ToString(),
    ((System.Double) bufferedTime).ToString("F1"));

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

Show:
© 2014 Microsoft