BinaryReader.Read Method (Byte[], Int32, Int32)
Reads the specified number of bytes from the stream, starting from a specified point in the byte array.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Parameters
- buffer
- Type: System.Byte[]
The buffer to read data into.
- index
- Type: System.Int32
The starting point in the buffer at which to begin reading into the buffer.
- count
- Type: System.Int32
The number of bytes to read.
Return Value
Type: System.Int32The 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.
| Exception | Condition |
|---|---|
| ArgumentException | The buffer length minus index is less than count. -or- The number of decoded characters to read is greater than count. This can happen if a Unicode decoder returns fallback characters or a surrogate pair. |
| ArgumentNullException | buffer is null. |
| ArgumentOutOfRangeException | index or count is negative. |
| ObjectDisposedException | The stream is closed. |
| IOException | An I/O error occurs. |
BinaryReader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.
The following example shows how to write binary data by using memory as a backing store. It displays a message to the console that indicates whether the data was written correctly.
using System; using System.IO; namespace BinaryRW { 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())) { Console.WriteLine("Writing the data."); 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; } } } for (int i = 0; i < arrayLength; i++) { if (verifyArray[i] != dataArray[i]) { Console.WriteLine("Error writing the data."); return; } } Console.WriteLine("The data was written and verified."); } } }
This example reads the contents of a file and displays each byte's numeric value in 16-column format. The end of the file that is being read is detected when the Read method returns zero bytes.
using System; using System.IO; using System.Text; public class DumpFileSample { private static readonly int CHUNK_SIZE = 1024; public static void Main(String[] args) { if ((args.Length == 0) || !File.Exists(args[0])) { Console.WriteLine("Please provide an existing file name."); } else { using (FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read)) { using (BinaryReader br = new BinaryReader(fs, new ASCIIEncoding())) { byte[] chunk; chunk = br.ReadBytes(CHUNK_SIZE); while(chunk.Length > 0) { DumpBytes(chunk, chunk.Length); chunk = br.ReadBytes(CHUNK_SIZE); } } } } } public static void DumpBytes(byte[] bdata, int len) { int i; int j = 0; char dchar; // 3 * 16 chars for hex display, 16 chars for text and 8 chars // for the 'gutter' int the middle. StringBuilder dumptext = new StringBuilder(" ", 16 * 4 + 8); for (i = 0; i < len; i++) { dumptext.Insert(j * 3, String.Format("{0:X2} ", (int)bdata[i])); dchar = (char)bdata[i]; //' replace 'non-printable' chars with a '.'. if (Char.IsWhiteSpace(dchar) || Char.IsControl(dchar)) { dchar = '.'; } dumptext.Append(dchar); j++; if (j == 16) { Console.WriteLine(dumptext); dumptext.Length = 0; dumptext.Append(" "); j = 0; } } // display the remaining line if (j > 0) { for (i = j; i < 16; i++) { dumptext.Insert(j * 3, " "); } Console.WriteLine(dumptext); } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.