BinaryReader: Read

Read

Fills either a Byte array or Integer array with the specified number of elements. Or returns the next character to be decoded from the stream.



 Public Function Read(
	  Optional ByRef Buffer As Variant,
	  Optional ByRef Index As Variant,
	  Optional ByRef Count As Variant ) As Long

Parameters

Buffer
[ByRef] Optional. Variant. The array to write the data to.
Index
[ByRef] Optional. Variant. The first index in Buffer to start writing data.
Count
[ByRef] Optional. Variant. The number of elements to be written to the array.

Return Values

Long -  If an array is specified, this returns the actual number of elements written to Buffer. This could be less than the requested amount. If no parameters are specified then the next character is returned.

Remarks

If a Byte array is passed in, then the bytes from the underlying stream are copied to the array. If an Integer array is passed in, then enough bytes are read from the stream to produce the requested number of decoded characters. The decoded characters are placed in the array starting at Index.

The position within the stream is advanced based on the Encoding being used.

If all parameters are missing then a single character will be decoded from the stream and returned.

All parameters must either be missing or supplied or an exception is thrown.

Exceptions

Exception Condition
ObjectDisposedExceptionThe stream is closed.
IOExceptionAn I/O error occurs.

Examples

This example writes several bytes to a MemoryStream and read back from the stream bytes and characters depending what parameters are specified.

Private Sub Main()
    Dim Source As New MemoryStream
    Dim Reader As BinaryReader
    Dim Bytes(0 To 1) As Byte
    Dim Chars(0 To 1) As Integer
    
    Source.WriteBlock NewBytes(65, 66, 67, 194, 163, 70), 0, 6
    Source.Position = 0
    
    Set Reader = NewBinaryReader(Source)
    
    Debug.Print "Read next one character."
    PrintChar Reader.Read
    Debug.Print
    
    Debug.Print "Read next two bytes."
    Debug.Print CorString.Format("  {0} byte(s) read.", Reader.Read(Bytes, 0, 2))
    PrintBytes Bytes
    Debug.Print
    
    Debug.Print "Read next two characters."
    Debug.Print CorString.Format("  {0} character(s) read.", Reader.Read(Chars, 0, 2))
    PrintChars Chars
End Sub

Private Sub PrintChars(ByRef Chars() As Integer)
    Dim Char As Variant
    
    For Each Char In Chars
        PrintChar Char
    Next
End Sub

Private Sub PrintChar(ByVal Char As Integer)
    Debug.Print CorString.Format("  {0,3} = ""{0:$}""", Char)
End Sub

Private Sub PrintBytes(ByRef Bytes() As Byte)
    Dim Value As Variant
    
    For Each Value In Bytes
        Debug.Print CorString.Format("  &h{0:X2} ", Value)
    Next
End Sub

' This example code produces the following output.
'
'    Read next one character.
'       65 = "A"
'
'    Read next two bytes.
'      2 byte(s) read.
'      &h42
'      &h43
'
'    Read next two characters.
'      2 character(s) read.
'      163 = "£"
'       70 = "F"

See Also

Project CorLib Overview

Class BinaryReader Overview

ReadChars

ReadBytes

CorString

Constructors