Mono Class Library: System.IO.MemoryStream Overview | Members

System.IO.MemoryStream.Read Method

Reads a block of bytes from the current stream at the current position, and writes the data to the specified byte array. [Edit]

public override int Read (byte[] buffer, int offset, int count)

Parameters

buffer
A byte array. When this method returns, buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream. [Edit]
offset
A int that specifies the byte offset in buffer at which to begin writing. [Edit]
count
A int that specifies the maximum number of bytes to read. [Edit]

Returns

A int that specifies the total number of bytes written into the buffer, or zero if the end of the stream is reached before any bytes are read. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionbuffer is null. [Edit]
ArgumentOutOfRangeExceptionoffset or count is negative. [Edit]
ArgumentException(offset + count ) is larger than the length of buffer. [Edit]
ObjectDisposedException The current stream is closed. [Edit]

Remarks

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.

If the read takes place immediately following a seek beyond the end of the stream, the end of the stream is reached.

Note:

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

This method overrides Stream.Read(Byte[], int, int).

[Edit]

Example

The following example demonstrates the result of reading from a System.IO.MemoryStream into its underlying byte array.

C# Example
using System;
using System.IO;

public class MemoryStreamTest {
   public static void Main() {

      byte[] values = new byte [] {0,1,2,3,4,5,6,7,8,9};

      foreach (byte b in values) {
         Console.Write(b);
      }

      Console.WriteLine();

      MemoryStream ms = new MemoryStream (values);

      ms.Read(values, 1, 5);

      foreach (byte b in values) {
         Console.Write(b);
      }
   }
}

The output is

0123456789
0012346789

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0