BinaryReader.ReadBytes Method (Int32)
Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- count
-
Type:
System.Int32
The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur.
Return Value
Type: System.Byte()A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached.
| Exception | Condition |
|---|---|
| ArgumentException | 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. |
| IOException | An I/O error occurs. |
| ObjectDisposedException | The stream is closed. |
| ArgumentOutOfRangeException | count is negative. |
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 code example shows how to write binary data using memory as a backing store, and then verify that the data was written correctly.
Imports System Imports System.IO Public Class BinaryRW Shared Sub Main() Const upperBound As Integer = 1000 ' Create random data to write to the stream. Dim dataArray(upperBound) As Byte Dim randomGenerator As New Random randomGenerator.NextBytes(dataArray) Dim binWriter As New BinaryWriter(New MemoryStream()) ' Write the data to the stream. Console.WriteLine("Writing the data.") binWriter.Write(dataArray) ' Create the reader using the stream from the writer. Dim binReader As New BinaryReader(binWriter.BaseStream) ' Set the stream position to the beginning of the stream. binReader.BaseStream.Position = 0 ' Read and verify the data. Dim verifyArray() As Byte = _ binReader.ReadBytes(dataArray.Length) If verifyArray.Length <> dataArray.Length Then Console.WriteLine("Error writing the data.") Return End If For i As Integer = 0 To upperBound If verifyArray(i) <> dataArray(i) Then Console.WriteLine("Error writing the data.") Return End If Next i Console.WriteLine("The data was written and verified.") End Sub End Class
This example reads the contents of a file and displays it to the console as dump text. The end of the file that is being read is detected when the length of the Byte array returned from ReadBytes is zero.
Imports System Imports System.IO Imports System.Text Module Module1 Private ReadOnly CHUNK_SIZE As Integer = 1024 Public Sub Main(args() As String) If ((args.Length = 0) OrElse Not File.Exists(args(0))) Then Console.WriteLine("Please provide an existing file name.") Else Using fs As FileStream = New FileStream(args(0), FileMode.Open, FileAccess.Read) Using br As New BinaryReader(fs, New ASCIIEncoding()) Dim chunk(CHUNK_SIZE) As Byte chunk = br.ReadBytes(CHUNK_SIZE) While chunk.Length > 0 DumpBytes(chunk, chunk.Length) chunk = br.ReadBytes(CHUNK_SIZE) End While End Using End Using End If End Sub Public Sub DumpBytes(bdata() As Byte, len As Integer) Dim i As Integer Dim j As Integer = 0 Dim dchar As Char ' 3 * 16 chars for hex display, 16 chars for text and 8 chars ' for the 'gutter' int the middle. Dim dumptext As New StringBuilder(" ", 16 * 4 + 8) For i = 0 To len - 1 dumptext.Insert(j * 3, String.Format("{0:X2} ", CType(bdata(i), Integer))) dchar = Convert.ToChar(bdata(i)) ' replace 'non-printable' chars with a '.'. If Char.IsWhiteSpace(dchar) Or Char.IsControl(dchar) Then dchar = "." End If dumptext.Append(dchar) j += 1 If j = 16 Then Console.WriteLine(dumptext) dumptext.Length = 0 dumptext.Append(" ") j = 0 End If Next i ' display the remaining line If j > 0 Then ' add blank hex spots to align the 'gutter'. For i = j To 15 dumptext.Insert(j * 3, " ") Next i Console.WriteLine(dumptext) End If End Sub End Module
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1