BinaryReader: 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 ( )
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.
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. |
ObjectDisposedException | The stream is closed. |
IOException | An I/O error occurs. |
ArgumentOutOfRangeException | Count is less than zero. |
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