BinaryReader: ReadBytes

ReadBytes

Reads the specified number of bytes from the stream and returns them in an array.



 Public Function ReadBytes(
	  ByVal Count As Long ) As Byte ( )

Parameters

Count
[ByVal] Long. The number of bytes to read from the stream.

Return Values

Byte() -  A byte array containing the bytes read from the stream.

Remarks

If not enough bytes are in the stream, then the remaining bytes are returned. If there are no bytes in the stream, then a zero-length array is returned, not a null array.

Exceptions

Exception Condition
ArgumentExceptionThe number of decoded characters to read is greater than Count. This can happen if a Unicode decoder returns fallback characters or a surrogate pair.
ObjectDisposedExceptionThe stream is closed.
IOExceptionAn I/O error occurs.
ArgumentOutOfRangeExceptionCount is less than zero.

Examples

The following example writes a 32-bit integer value then reads them all back at once using ReadByte.

Public Sub Main()
    Dim Source As New MemoryStream
    Dim Reader As BinaryReader
    Dim Writer As BinaryWriter
    
    Set Writer = NewBinaryWriter(Source, New UnicodeEncoding)
    Writer.WriteValue &H80706050
    
    Source.Position = 0
    
    Set Reader = NewBinaryReader(Source)
    
    Dim Bytes() As Byte
    Dim Value As Variant
    
    Bytes = Reader.ReadBytes(4)
    
    Debug.Print CorString.Format("Read {0} bytes.", CorArray.Length(Bytes))
    
    For Each Value In Bytes
        PrintByte Value
    Next
End Sub

Private Sub PrintByte(ByVal Value As Byte)
    Debug.Print CorString.Format("&h{0:X2}", Value)
End Sub


' This example code produces the following output.
'
'   Read 4 bytes.
'   &h50
'   &h60
'   &h70
'   &h80

See Also

Project CorLib Overview

Class BinaryReader Overview

Read

ReadByte

CorString

CorArray

Constructors