Copies bytes from the current buffered stream to an array.
Assembly: mscorlib (in mscorlib.dll)
Public Overrides Function Read ( _ <OutAttribute> array As Byte(), _ offset As Integer, _ count As Integer _ ) As Integer
Dim instance As BufferedStream Dim array As Byte() Dim offset As Integer Dim count As Integer Dim returnValue As Integer returnValue = instance.Read(array, offset, _ count)
public override int Read( byte[] array, int offset, int count )
public: virtual int Read( [InAttribute] [OutAttribute] array<unsigned char>^ array, int offset, int count ) override
public override function Read( array : byte[], offset : int, count : int ) : int
Parameters
- array
- Type: System.Byte[]
The buffer to which bytes are to be copied.
- offset
- Type: System.Int32
The byte offset in the buffer at which to begin reading bytes.
- count
- Type: System.Int32
The number of bytes to be read.
Return Value
Type: System.Int32The 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 |
|---|---|
| ArgumentException |
Length of array minus offset is less than count. |
| ArgumentNullException |
array is null. |
| ArgumentOutOfRangeException |
offset or count is negative. |
| IOException |
The stream is not open or is null. |
| 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 Dim numBytesToRead As Integer = receivedData.Length Dim n As Integer Do While numBytesToRead > 0 'Read my return anything from 0 to numBytesToRead n = bufStream.Read(receivedData, 0, receivedData.Length) 'The end of the file is reached. If n = 0 Then Exit Do End If bytesReceived += n numBytesToRead -= n Loop bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds Console.WriteLine("{0} bytes received in {1} " & _ "seconds." & vbCrLf, _ bytesReceived.ToString(), _ bufferedTime.ToString("F1"))
// 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) break; bytesReceived += n; numBytesToRead -= n; } 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::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" ) );
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.