This documentation is archived and is not being maintained.

BufferedStream.Read Method

Copies bytes from the current buffered stream to an array.

[Visual Basic]
Overrides Public Function Read( _
   <InteropServices.In(), _
   Out()> ByVal array() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
) As Integer
[C#]
public override int Read(
   [
   In,
   Out
] byte[] array,
 int offset,
 int count
);
[C++]
public: int Read(
   [
   In,
   Out
] unsigned char array __gc[],
 int offset,
 int count
);
[JScript]
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 aren't currently available, or 0 if the end of the stream has been reached before any data can be read.

Exceptions

Exception Type Condition
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.

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing 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.

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for the BufferedStream class.

[Visual Basic] 
' 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)
End While
bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes received in {1} " & _
    "seconds." & vbCrLf, _
    bytesReceived.ToString(), _
    bufferedTime.ToString("F1"))

[C#] 
// 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"));

[C++] 
// 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(S"{0} bytes received in {1} seconds.\n",
    bytesReceived.ToString(), 
    bufferedTime.ToString("F1"));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

BufferedStream Class | BufferedStream Members | System.IO Namespace | BlockCopy | CanRead | Write | Working with I/O | Reading Text from a File | Writing Text to a File

Show: