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.
Assembly: mscorlib (in mscorlib.dll)
Public MustOverride Function Read ( _
<OutAttribute> buffer As Byte(), _
offset As Integer, _
count As Integer _
) As Integerpublic abstract int Read(
byte[] buffer,
int offset,
int count
)public:
virtual int Read(
[InAttribute] [OutAttribute] array<unsigned char>^ buffer,
int offset,
int count
) abstractabstract Read :
buffer:byte[] byref *
offset:int *
count:int -> int
Parameters
- buffer
- Type:
array< System. . :: . Byte> [] () []
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
- Type: System
. . :: . Int32
The zero-based byte offset in buffer at which to begin storing the data read from the current stream.
- count
- Type: System
. . :: . Int32
The maximum number of bytes to be read from the current stream.
Return Value
Type: SystemThe 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.
| Exception | Condition |
|---|---|
| ArgumentException | The sum of offset and count is larger than the buffer length. |
| ArgumentNullException | buffer is |
| ArgumentOutOfRangeException | offset or count is negative. |
| IOException | An I/O error occurs. |
| NotSupportedException | The stream does not support reading. |
| ObjectDisposedException | Methods were called after the stream was closed. |
Use the CanRead property to determine whether the current instance supports reading.
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 BinaryReader for reading primitive data types.
The following example shows how to use Read to read a block of data.
Imports System
Imports System.IO
Public Class Block
Public Shared Sub Main()
Dim s As Stream = New MemoryStream()
For i As Integer = 0 To 99
s.WriteByte(CType(i, Byte))
Next i
s.Position = 0
' Now read s into a byte buffer.
Dim bytes(s.Length) As Byte
Dim numBytesToRead As Integer = s.Length
Dim numBytesRead As Integer = 0
Dim n As Integer
While numBytesToRead > 0
' Read may return anything from 0 to 10.
n = s.Read(bytes, numBytesRead, 10)
' The end of the file is reached.
If n = 0 Then
Exit While
End If
numBytesRead += n
numBytesToRead -= n
End While
s.Close()
' numBytesToRead should be 0 now, and numBytesRead should
' equal 100.
Console.WriteLine("number of bytes read: {0:d}", numBytesRead)
End Sub
End Class
using System;
using System.IO;
public class Block
{
public static void Main()
{
Stream s = new MemoryStream();
for (int i = 0; i < 100; i++)
{
s.WriteByte((byte)i);
}
s.Position = 0;
// Now read s into a byte buffer.
byte[] bytes = new byte[s.Length];
int numBytesToRead = (int) s.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to 10.
int n = s.Read(bytes, numBytesRead, 10);
// The end of the file is reached.
if (n == 0)
{
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
s.Close();
// numBytesToRead should be 0 now, and numBytesRead should
// equal 100.
Console.WriteLine("number of bytes read: {0:d}", numBytesRead);
}
}
using namespace System;
using namespace System::IO;
public ref class Block
{
public:
static void Main()
{
Stream^ s = gcnew MemoryStream();
for (int i = 0; i < 100; i++)
{
s->WriteByte((Byte)i);
}
s->Position = 0;
// Now read s into a byte buffer.
array<Byte>^ bytes = gcnew array<Byte>(s->Length);
int numBytesToRead = (int) s->Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to 10.
int n = s->Read(bytes, numBytesRead, 10);
// The end of the file is reached.
if (n == 0)
{
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
s->Close();
// numBytesToRead should be 0 now, and numBytesRead should
// equal 100.
Console::WriteLine("number of bytes read: {0:d}", numBytesRead);
}
};
int main()
{
Block::Main();
}
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.