Buffer

Buffer


This class provides methods for copying manipulating arrays as byte arrays.


Public:

Methods:

NameDescription
 BlockCopy Copies the bytes from the source array to the destination array.  
 ByteLength Returns the length of the array in bytes.  
 GetByte Returns a the byte value at the given byte index within an array.  
 SetByte Sets the byte value at a given byte index within the array.  

Remarks

Any array that is used in these methods are treated as byte arrays. No attention is paid to the type of the original array. They are manipulated as bytes.

Examples

The following code example illustrates the use of several Buffer class methods.

Private Sub Main()
    Dim Arr() As Integer
    
    Arr = NewIntegers(258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271)
    
    Debug.Print t("This example of the Buffer class " & _
        "methods generates the following output.\n" & _
        "Note: The array is displayed from right to left.\n")
    Debug.Print t("Initial values of array:\n")
    
    ' Display the initial array values and ByteLength.
    DisplayArray Arr
    Debug.Print t("\nBuffer.ByteLength(Arr): ") & Buffer.ByteLength(Arr)
    
    ' Copy a region of the array; set a byte within the array.
    Debug.Print t("\nCall these methods: \n" & _
        "  Buffer.BlockCopy(Arr, 5, Arr, 16, 9),\n" & _
        "  Buffer.SetByte(Arr, 7, 170).\n")
    
    Buffer.BlockCopy Arr, 5, Arr, 16, 9
    Buffer.SetByte Arr, 7, 170
    
    ' Display the array and a byte within the array.
    Debug.Print t("Final values of array:\n")
    DisplayArray Arr
    Debug.Print t("\nBuffer.GetByte(Arr, 26): ") & Buffer.GetByte(Arr, 26)
End Sub

' Display the array elements from right to left in hexadecimal.
Private Sub DisplayArray(ByRef Arr() As Integer)
    Dim i As Long
    
    Debug.Print "  Arr:";
    
    For i = UBound(Arr) To LBound(Arr) Step -1
        Debug.Print CorString.Format(" {0:X4}", Arr(i));
    Next
    
    Debug.Print
End Sub

' This code produces the following output.
'
'    This example of the Buffer class methods generates the following output.
'    Note: The array is displayed from right to left.
'
'    Initial values of array:
'
'      Arr: 010F 010E 010D 010C 010B 010A 0109 0108 0107 0106 0105 0104 0103 0102
'
'    Buffer.ByteLength(Arr): 28
'
'    Call these methods:
'      Buffer.BlockCopy(Arr, 5, Arr, 16, 9),
'      Buffer.SetByte(Arr, 7, 170).
'
'    Final values of array:
'
'      Arr: 010F 0101 0801 0701 0601 0501 0109 0108 0107 0106 AA05 0104 0103 0102
'
'    Buffer.GetByte(Arr, 26): 15

See Also

Project CorLib Overview

Class Buffer Overview