Export (0) Print
Expand All

MemoryStream.Read Method

Reads a block of bytes from the current stream and writes the data to buffer.

[Visual Basic]
Overrides Public Function Read( _
   <InteropServices.In(), _
   Out()> ByVal buffer() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
) As Integer
[C#]
public override int Read(
   [
   In,
   Out
] byte[] buffer,
 int offset,
 int count
);
[C++]
public: int Read(
   [
   In,
   Out
] unsigned char buffer __gc[],
 int offset,
 int count
);
[JScript]
public override function Read(
   buffer : Byte[],
 offset : int,
 count : int
) : int;

Parameters

buffer
When this method returns, contains the specified byte array with the values between offset and (offset + count- 1) replaced by the characters read from the current stream.
offset
The byte offset in buffer at which to begin reading.
count
The maximum number of bytes to read.

Return Value

The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.

Exceptions

Exception Type Condition
ArgumentNullException buffer is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException offset or count is negative.
ArgumentException offset subtracted from the buffer length is less than count.
ObjectDisposedException The current stream instance is 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.

This method overrides Read.

The offset parameter gives the offset of the first byte in buffer to which data from the current stream is written. The count parameter gives the maximum number of bytes to read from the current stream. The returned value is the actual number of bytes read, or zero if the end of the stream is reached.

If the read operation is successful, the current position within the stream advances by the number of bytes read. If an exception occurs, the current position within the stream remains unchanged.

The Read method will return zero 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 zero (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.

CAUTION   If the byte array specified in the buffer parameter is the underlying buffer returned by the GetBuffer method, the array contents are overwritten, and no exception is thrown.

Example

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

[Visual Basic] 
' Read the first 20 bytes from the stream.
byteArray = _
    New Byte(CType(memStream.Length, Integer)){}
count = memStream.Read(byteArray, 0, 20)

[C#] 
// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);

[C++] 
// Read the first 20 bytes from the stream.
byteArray = new Byte __gc[memStream->Length];
count = memStream->Read(byteArray, 0, 20);

[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, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

MemoryStream Class | MemoryStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft