C# MemoryStream Read

Description

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

Syntax

MemoryStream.Read has the following syntax.


public override int Read(
  byte[] buffer,/*from   w w w .j  a va2  s .  c om*/
  int offset,
  int count
)

Parameters

MemoryStream.Read has the following 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 zero-based byte offset in buffer at which to begin storing data from the current stream.
  • count - The maximum number of bytes to read.

Returns

MemoryStream.Read method returns 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.

Example

Read the first 20 bytes from the stream.


/*  w w w.j  a  v  a 2s  .co  m*/
using System.IO;
using System;
public class MainClass
{
    public static void Main(String[] argv)
    {
        MemoryStream memStream = new MemoryStream();
        System.Console.WriteLine(memStream.CanRead);

        byte[] byteArray = new byte[memStream.Length];
        int count = memStream.Read(byteArray, 0, 20);

    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter