Buffer Class in VB.NET

In this article I will explain you about the Buffer Class in VB.NET.
  • 5857
 

The word buffer implies something that works directly on memory. In the VB.NET, buffering is basically a manipulation of unmanaged memory that is represented as arrays of bytes. Table 21.14 describes some members of the Buffer class. Let's look at an example of a program where we copy one array of data into another using the Array class, and then we will compare that with a Buffer class example doing the same thing.

Table 21.14: Buffer Class Members
 

table21.14.gif

The Copy() member allows us to copy from one array to another. Let's take an array of five elements, initialized with the data 1, 2, 3, 4, 5 and another array of 10 elements, with the data 0, 0, 0, 0, 0, 6, 7, 8, 9, 10. In arrays, length refers to the number of elements in the array. In our example, Myarr1 has five elements, so the array length is 5, and Myarr2 has 10 elements, so the array length is 10. The Array class includes the Copy() method, which copies the contents of one array into another. It copies a range of elements from an array starting at the specified source index and pastes it to another array starting at the specified destination index. The Copy() method takes five parameters: source array, source index, destination array, destination index, and number of elements to copy.

The method Array.Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length) takes five parameters: first, the array that contains the data to copy; second, the index in the sourceArray at which copying begins; third, the array that receives the data; fourth, DestinationIndex, the index in the destinationArray at which storing begins; and, fifth, the number of elements to copy.

The example given below demonstrates the use of the Array.Copy() method. In the example we perform Array.Copy(myarr1, 0, myarr2, 0, 5) for the following arrays:

    Dim myarr1 As Integer() = New Integer(4) {1, 2, 3, 4, 5}
    Dim myarr2 As Integer() = New Integer(9) {0, 0, 0, 0, 0, 6, 7, 8, 9, 10}

Example of Array.Copy method:

    Public Class Array1
        Shared Sub Main(ByVal args As String())
            Dim myarr1 As Integer() = New Integer(4) {1, 2, 3, 4, 5}
            Dim myarr2 As Integer() = New Integer(9) {0, 0, 0, 0, 0, 6, 7, 8, 9, 10}
            Console.Write("Before Array copy operation" & vbLf)
            Console.Write("Myarr1 and Byte Length{0}" & vbLf, myarr1.Length)

            For Each i As Integer In myarr1
                Console.Write("{0} " & vbTab, i)
            Next

            For Each i As Integer In myarr2
                Console
.Write("{0} " & vbTab, i)
            Next

            Array.Copy(myarr1, 0, myarr2, 0, 5)
            Console.Write("After Array copy operation" & vbLf)
            Console.Write("Myarr1 :" & vbLf)

            For Each i As Integer In myarr1
                Console.Write("{0} " & vbTab, i)
            Next

            Console.WriteLine(vbLf & "Myarr2: " & vbLf)
            For Each i As Integer In myarr2
                Console.Write("{0} " & vbTab, i)
            Next
            Console.ReadLine()
        End Sub
    End
Class

Output
 

outlputListing 1.gif

Now let's see how the same thing is done using the BlockCopy() method. The major difference between the two types of copying, Array.Copy and BlockCopy, is that the copy made in the Buffer class is not an index-to-index copy. It is from offset to offset in memory. The Buffer class copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset in memory. In our example, we are using arrays of integers, and we know that each integer occupies four bytes. Therefore, the offset values will be the addition of four bytes from the starting offset value.

Let's look at an example of copying using BlockCopy with an array of five elements, initialized with the data 1, 2, 3, 4, 5 and another array of 10 elements, with the data 0, 0, 0, 0, 0, 6, 7, 8, 9, 10.

In the Buffer class the length refers to the number of bytes in the array. In our example, Myarr1 has five elements, so the byte length is 5 (elements) X 4 (number of bytes in an integer) = 20 bytes. Myarr2 has 10 elements, so the byte length is 10 (elements) X 4 = 40 bytes.

The BlockCopy method in the Buffer class copies a range of elements from an array starting at the specified source offset value to another array starting at the specified destination offset value. The BlockCopy() method takes five parameters: source array, source offset value, destination array, destination offset value, and the number of bytes to copy. In our example, we need to copy five elements, so 5 X 4 = 20 bytes is the number of bytes to copy.

The BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count) method takes five parameters. The first is the source buffer, the second is the byte offset into src, the third is the destination buffer, the fourth is the byte offset into dst, and the fifth is the number of bytes to copy. Example below demonstrates the use of the Buffer.BlockCopy() method.

Example of Buffer.BlockCopy method:

    Public Class buffer1
        Shared Sub Main(ByVal args As String())
            Dim myarr1 As Integer() = New Integer(4) {1, 2, 3, 4, 5}
            Dim myarr2 As Integer() = New Integer(9) {0, 0, 0, 0, 0, 6, 7, 8, 9, 10}
            Console.Write("Before Block copy operation" & vbLf)
            Console.Write("Myarr1 and Byte Length :{0}" & vbLf, Buffer.ByteLength(myarr1))

            For Each i As Integer In myarr1
                Console.Write("{0} " & vbTab, i)
            Next

            Console.WriteLine(vbLf & "Myarr2 and Byte Length:{0} " & vbLf, Buffer.ByteLength(myarr2))
            For Each i As Integer In myarr2
                Console.Write("{0} " & vbTab, i)
            Next

            Buffer.BlockCopy(myarr1, 0, myarr2, 0, 20)
            Console.Write("After Block copy operation" & vbLf)
            Console.Write("Myarr1 :" & vbLf)

            For Each i As Integer In myarr1
                Console.Write("{0} " & vbTab, i)
            Next

            Console.WriteLine(vbLf & "Myarr2: " & vbLf)
            For Each i As Integer In myarr2
                Console.Write("{0} " & vbTab, i)
            Next
            Console.ReadLine()
        End Sub
    End
Class

Output

outlputListing21.33.gif

Conclusion

Hope this article would have help you in understanding the Buffer Class in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.