C# BinaryReader Read(Byte[], Int32, Int32)

Description

BinaryReader Read(Byte[], Int32, Int32) Reads the specified number of bytes from the stream, starting from a specified point in the byte array.

Syntax

BinaryReader.Read(Byte[], Int32, Int32) has the following syntax.


public virtual int Read(
  byte[] buffer,/*w w w . j a  v  a  2s. co  m*/
  int index,
  int count
)

Parameters

BinaryReader.Read(Byte[], Int32, Int32) has the following parameters.

  • buffer - The buffer to read data into.
  • index - The starting point in the buffer at which to begin reading into the buffer.
  • count - The number of bytes to read.

Returns

BinaryReader.Read(Byte[], Int32, Int32) method returns The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the stream is reached.

Example

The following example shows how to write binary data by using memory as a backing store.


/* w  ww .j a  v a2  s. c  om*/
using System;
using System.IO;


class Program
{
    static void Main(string[] args)
    {
        const int arrayLength = 1000;
        byte[] dataArray = new byte[arrayLength];
        byte[] verifyArray = new byte[arrayLength];

        new Random().NextBytes(dataArray);

        using (BinaryWriter binWriter = new BinaryWriter(new MemoryStream()))
        {
            binWriter.Write(dataArray, 0, arrayLength);
            using (BinaryReader binReader = new BinaryReader(binWriter.BaseStream))
            {
                binReader.BaseStream.Position = 0;

                if (binReader.Read(verifyArray, 0, arrayLength) != arrayLength)
                {
                    Console.WriteLine("Error writing the data.");
                    return;
                }
            }
        }

    }
}




















Home »
  C# Tutorial »
    System.IO »




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