473,722 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Building Byte Arrays - Speed

I was trying to determine the fastest way to build a byte array from
components where the size of the individual components varied depending on
the user's input. I tried three classes I built: (1) using redim arrays to
add to a normal byte array (2) using an ArrayList and finally (3) using a
memorystream. These three classes are listed below the test sub called
"TestBuildByteA rray". It was interesting that using the memorystream was
twice as fast as the ArrayList and almost 2000 time as fast as using normal
byte array with redimensions. Thought some might be interested in this.

Private Sub TestBuildByteAr raySpeed(ByRef timeByteArray As Long, ByRef
timeArrayList As Long, ByRef TimeArrayStream As Long)
'Testing speed off byte array building
'Notes: Add in Finalize to close and release memory
Dim starttime As Long
Dim count As Integer = 2000
Dim startsize As Integer = 500
Dim i, j As Integer
Dim byt As Byte = 125
Dim nb As Byte() = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim b() As Byte

'Test ByteArray Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cb As ByteArray = New ByteArray(start size)
For i = 1 To count
For j = 0 To 50 : cb.Add(byt) : Next
cb.AddRange(nb)
Next
b = cb.ToArray
timeByteArray = DateTime.Now.Ti cks - starttime

'Test ArrayList Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cl As ArrayList = New ArrayList(start size)
For i = 1 To count
For j = 0 To 50 : cl.Add(byt) : Next
cl.AddRange(nb)
Next
b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
timeArrayList = DateTime.Now.Ti cks - starttime

'Test ArrayStream Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cs As ArrayStream = New ArrayStream(500 )
For i = 1 To count
For j = 0 To 50 : cs.Add(byt) : Next
cs.AddRange(nb)
Next
b = cs.ToArray
TimeArrayStream = DateTime.Now.Ti cks - starttime

End Sub
End Class

Public Class ByteArray
Private u, c, i As Integer
Private b As Byte()
Public Sub New(ByVal size As Integer)
MyBase.New()
ReDim b(size)
u = size
c = 0
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
Return b
End Get
End Property
Public Sub Add(ByVal nb As Byte)
If c > u Then ReDim Preserve b(c) : u = c
b(c) = nb
c = c + 1
End Sub
Public Sub AddRange(ByVal nb() As Byte)
i = c + UBound(nb, 1)
If i > u Then ReDim Preserve b(i)
nb.CopyTo(b, c)
c = i
End Sub
End Class

Public Class ArrayStream
Private ms As MemoryStream
Public Sub New(ByVal size As Integer)
MyBase.New()
ms = New MemoryStream(si ze)
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ms.Position = 0
Return ms.ToArray()
End Get
End Property
Public Sub Add(ByVal nb As Byte)
ms.WriteByte(nb )
End Sub

Public Sub AddRange(ByVal nb() As Byte)
ms.Write(nb, 0, UBound(nb) + 1)
End Sub

Protected Overrides Sub finalize()
ms.Close()
MyBase.Finalize ()
End Sub
End Class

--
Dennis in Houston
Nov 21 '05 #1
6 2780
Dennis,
Your ByteArray is so slow as you are recreating the array each time you add
a byte.

Consider doing what ArrayList & MemoryStream do, over allocate the buffer.

Instead of having the buffer contain the exact number of bytes in it, have
it contain Capicity # of bytes, then have a second variable for the Length
number of bytes.

I posted the start of a ByteBuffer class that functions very similar to the
System.Text.Str ingBuffer class in that it maintains a byte array internally
allowing you to append byte arrays on the end, expanding the internal array
as needed.

http://groups.google.com/groups?q=By...phx.gbl&rnum=3

Hope this helps
Jay

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:FA******** *************** ***********@mic rosoft.com...
I was trying to determine the fastest way to build a byte array from
components where the size of the individual components varied depending on
the user's input. I tried three classes I built: (1) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a
memorystream. These three classes are listed below the test sub called
"TestBuildByteA rray". It was interesting that using the memorystream was
twice as fast as the ArrayList and almost 2000 time as fast as using normal byte array with redimensions. Thought some might be interested in this.

Private Sub TestBuildByteAr raySpeed(ByRef timeByteArray As Long, ByRef
timeArrayList As Long, ByRef TimeArrayStream As Long)
'Testing speed off byte array building
'Notes: Add in Finalize to close and release memory
Dim starttime As Long
Dim count As Integer = 2000
Dim startsize As Integer = 500
Dim i, j As Integer
Dim byt As Byte = 125
Dim nb As Byte() = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim b() As Byte

'Test ByteArray Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cb As ByteArray = New ByteArray(start size)
For i = 1 To count
For j = 0 To 50 : cb.Add(byt) : Next
cb.AddRange(nb)
Next
b = cb.ToArray
timeByteArray = DateTime.Now.Ti cks - starttime

'Test ArrayList Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cl As ArrayList = New ArrayList(start size)
For i = 1 To count
For j = 0 To 50 : cl.Add(byt) : Next
cl.AddRange(nb)
Next
b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
timeArrayList = DateTime.Now.Ti cks - starttime

'Test ArrayStream Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cs As ArrayStream = New ArrayStream(500 )
For i = 1 To count
For j = 0 To 50 : cs.Add(byt) : Next
cs.AddRange(nb)
Next
b = cs.ToArray
TimeArrayStream = DateTime.Now.Ti cks - starttime

End Sub
End Class

Public Class ByteArray
Private u, c, i As Integer
Private b As Byte()
Public Sub New(ByVal size As Integer)
MyBase.New()
ReDim b(size)
u = size
c = 0
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
Return b
End Get
End Property
Public Sub Add(ByVal nb As Byte)
If c > u Then ReDim Preserve b(c) : u = c
b(c) = nb
c = c + 1
End Sub
Public Sub AddRange(ByVal nb() As Byte)
i = c + UBound(nb, 1)
If i > u Then ReDim Preserve b(i)
nb.CopyTo(b, c)
c = i
End Sub
End Class

Public Class ArrayStream
Private ms As MemoryStream
Public Sub New(ByVal size As Integer)
MyBase.New()
ms = New MemoryStream(si ze)
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ms.Position = 0
Return ms.ToArray()
End Get
End Property
Public Sub Add(ByVal nb As Byte)
ms.WriteByte(nb )
End Sub

Public Sub AddRange(ByVal nb() As Byte)
ms.Write(nb, 0, UBound(nb) + 1)
End Sub

Protected Overrides Sub finalize()
ms.Close()
MyBase.Finalize ()
End Sub
End Class

--
Dennis in Houston

Nov 21 '05 #2
I did find a couple of errors in the ByteArray class but it didn't make any
difference in speed. I also tried using an initial allocation sufficently
large so the only redim needed was at the end to make the byte array the
correct size. However, in real life, one can't avoid the redim if one is
dealing with variable inputs from users. The ArrayList was still twice as
fast as the ByteArray class and the MemoryStream class was twice as fast as
the ArrayList. My main point is that the Memorystream is quite fast for
simple byte array manipulation.

Haven't had a chance to look at your ByteBuffer yet but will tonight.
Private Sub TestBuildByteAr raySpeed(ByRef timeByteArray As Long, ByRef
timeArrayList As Long, ByRef TimeArrayStream As Long)
'Testing speed off byte array building
'Notes: Add in Finalize to close and release memory
Dim starttime As Long
Dim count As Integer = 1000
Dim startsize As Integer = 80000
Dim i, j As Integer
Dim byt As Byte = 125
Dim nb As Byte() = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim b() As Byte

'Test ByteArray Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cb As ByteArray = New ByteArray(start size)
For i = 1 To count
For j = 0 To 50 : cb.Add(byt) : Next
cb.AddRange(nb)
Next
b = cb.ToArray
timeByteArray = DateTime.Now.Ti cks - starttime

'Test ArrayList Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cl As ArrayList = New ArrayList(start size)
For i = 1 To count
For j = 0 To 50 : cl.Add(byt) : Next
cl.AddRange(nb)
Next
b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
timeArrayList = DateTime.Now.Ti cks - starttime

'Test ArrayStream Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cs As ArrayStream = New ArrayStream(sta rtsize)
For i = 1 To count
For j = 0 To 50 : cs.Add(byt) : Next
cs.AddRange(nb)
Next
b = cs.ToArray
TimeArrayStream = DateTime.Now.Ti cks - starttime

End Sub
End Class

Public Class ByteArray
Private u, c, i As Integer
Private b As Byte()
Public Sub New(ByVal size As Integer)
MyBase.New()
ReDim b(size)
u = size
c = 0
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ReDim Preserve b(c - 1)
Return b
End Get
End Property
Public Sub Add(ByVal nb As Byte)
If c > u Then ReDim Preserve b(c) : u = c
b(c) = nb
c = c + 1
End Sub
Public Sub AddRange(ByVal nb() As Byte)
i = c + UBound(nb, 1)
If i > u Then ReDim Preserve b(i) : u = i
nb.CopyTo(b, c)
c = i + 1
End Sub
End Class

Public Class ArrayStream
Private ms As MemoryStream
Public Sub New(ByVal size As Integer)
MyBase.New()
ms = New MemoryStream(si ze)
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ms.SetLength(ms .Position)
Return ms.ToArray()
End Get
End Property
Public Sub Add(ByVal nb As Byte)
ms.WriteByte(nb )
End Sub

Public Sub AddRange(ByVal nb() As Byte)
ms.Write(nb, 0, UBound(nb) + 1)
End Sub
Protected Overrides Sub finalize()
ms.Close()
MyBase.Finalize ()
End Sub
End Class
"Jay B. Harlow [MVP - Outlook]" wrote:
Dennis,
Your ByteArray is so slow as you are recreating the array each time you add
a byte.

Consider doing what ArrayList & MemoryStream do, over allocate the buffer.

Instead of having the buffer contain the exact number of bytes in it, have
it contain Capicity # of bytes, then have a second variable for the Length
number of bytes.

I posted the start of a ByteBuffer class that functions very similar to the
System.Text.Str ingBuffer class in that it maintains a byte array internally
allowing you to append byte arrays on the end, expanding the internal array
as needed.

http://groups.google.com/groups?q=By...phx.gbl&rnum=3

Hope this helps
Jay

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:FA******** *************** ***********@mic rosoft.com...
I was trying to determine the fastest way to build a byte array from
components where the size of the individual components varied depending on
the user's input. I tried three classes I built: (1) using redim arrays

to
add to a normal byte array (2) using an ArrayList and finally (3) using a
memorystream. These three classes are listed below the test sub called
"TestBuildByteA rray". It was interesting that using the memorystream was
twice as fast as the ArrayList and almost 2000 time as fast as using

normal
byte array with redimensions. Thought some might be interested in this.

Private Sub TestBuildByteAr raySpeed(ByRef timeByteArray As Long, ByRef
timeArrayList As Long, ByRef TimeArrayStream As Long)
'Testing speed off byte array building
'Notes: Add in Finalize to close and release memory
Dim starttime As Long
Dim count As Integer = 2000
Dim startsize As Integer = 500
Dim i, j As Integer
Dim byt As Byte = 125
Dim nb As Byte() = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim b() As Byte

'Test ByteArray Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cb As ByteArray = New ByteArray(start size)
For i = 1 To count
For j = 0 To 50 : cb.Add(byt) : Next
cb.AddRange(nb)
Next
b = cb.ToArray
timeByteArray = DateTime.Now.Ti cks - starttime

'Test ArrayList Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cl As ArrayList = New ArrayList(start size)
For i = 1 To count
For j = 0 To 50 : cl.Add(byt) : Next
cl.AddRange(nb)
Next
b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
timeArrayList = DateTime.Now.Ti cks - starttime

'Test ArrayStream Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cs As ArrayStream = New ArrayStream(500 )
For i = 1 To count
For j = 0 To 50 : cs.Add(byt) : Next
cs.AddRange(nb)
Next
b = cs.ToArray
TimeArrayStream = DateTime.Now.Ti cks - starttime

End Sub
End Class

Public Class ByteArray
Private u, c, i As Integer
Private b As Byte()
Public Sub New(ByVal size As Integer)
MyBase.New()
ReDim b(size)
u = size
c = 0
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
Return b
End Get
End Property
Public Sub Add(ByVal nb As Byte)
If c > u Then ReDim Preserve b(c) : u = c
b(c) = nb
c = c + 1
End Sub
Public Sub AddRange(ByVal nb() As Byte)
i = c + UBound(nb, 1)
If i > u Then ReDim Preserve b(i)
nb.CopyTo(b, c)
c = i
End Sub
End Class

Public Class ArrayStream
Private ms As MemoryStream
Public Sub New(ByVal size As Integer)
MyBase.New()
ms = New MemoryStream(si ze)
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ms.Position = 0
Return ms.ToArray()
End Get
End Property
Public Sub Add(ByVal nb As Byte)
ms.WriteByte(nb )
End Sub

Public Sub AddRange(ByVal nb() As Byte)
ms.Write(nb, 0, UBound(nb) + 1)
End Sub

Protected Overrides Sub finalize()
ms.Close()
MyBase.Finalize ()
End Sub
End Class

--
Dennis in Houston


Nov 21 '05 #3
Dennis,

I think (mostly I try this, I did not because I use for this kind of things
mostly the memorystream) that when you first calculate the length of your
byteArray that you can speed that part up by avoiding the redim.

Cor

Nov 21 '05 #4
Yes, it speeds it up by hundreds of times. However, my problem was that I
don't know the final size of my byte array to begin with since I was building
it from several user's input of strings and I had no idea of the number nor
the size of the strings the users would input. I mainly was just amazed that
the memorystream was so fast.

"Cor Ligthert" wrote:
Dennis,

I think (mostly I try this, I did not because I use for this kind of things
mostly the memorystream) that when you first calculate the length of your
byteArray that you can speed that part up by avoiding the redim.

Cor

Nov 21 '05 #5
Dennis,

I was not clear, I did mean, first calculating the size of the array and
after that starts filling, maybe it need more time, however I think that
that will be earned not doing the redims.

Cor
Nov 21 '05 #6
Dennis,
What ArrayList & StringBuilder does is double its buffer each time the
buffer is not large enough to add any more elements. The buffer starts out
at 16 elements. I believe MemoryStream follows the same double the buffer
rule (its a good simple rule to use).

In your original code & this code you were adding 1 to the buffer each time
the buffer was not large enough, which is each time you added an element
(once past the initial size).

What I am suggesting is you start with at least a 16 element buffer, then in
your add method if the buffer does not have enough free room double it. As
you add elements you will be increasing the buffer less & less.

Its also a good idea in your class as well as MemoryStream, ArrayList &
StringBuilder to give it a good initial estimated capacity. For example, if
you expect to add 1000 elements to the class, then start with a capacity of
1000.
the ArrayList. My main point is that the Memorystream is quite fast for
simple byte array manipulation. My main point is that the MemoryStream is holding an Array of Bytes in
memory, when it needs to reallocate this array of bytes is effectively does
an ReDim Preserve!

My second point is if the MemoryStream is doing too much you can build your
own ByteArray that will perform equally well! By "too much" I mean there are
properties & methods on MemoryStream that you don't need such as Seek.

Hope this helps
Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:8E******** *************** ***********@mic rosoft.com... I did find a couple of errors in the ByteArray class but it didn't make any difference in speed. I also tried using an initial allocation sufficently
large so the only redim needed was at the end to make the byte array the
correct size. However, in real life, one can't avoid the redim if one is
dealing with variable inputs from users. The ArrayList was still twice as
fast as the ByteArray class and the MemoryStream class was twice as fast as the ArrayList. My main point is that the Memorystream is quite fast for
simple byte array manipulation.

Haven't had a chance to look at your ByteBuffer yet but will tonight.
Private Sub TestBuildByteAr raySpeed(ByRef timeByteArray As Long, ByRef
timeArrayList As Long, ByRef TimeArrayStream As Long)
'Testing speed off byte array building
'Notes: Add in Finalize to close and release memory
Dim starttime As Long
Dim count As Integer = 1000
Dim startsize As Integer = 80000
Dim i, j As Integer
Dim byt As Byte = 125
Dim nb As Byte() = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim b() As Byte

'Test ByteArray Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cb As ByteArray = New ByteArray(start size)
For i = 1 To count
For j = 0 To 50 : cb.Add(byt) : Next
cb.AddRange(nb)
Next
b = cb.ToArray
timeByteArray = DateTime.Now.Ti cks - starttime

'Test ArrayList Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cl As ArrayList = New ArrayList(start size)
For i = 1 To count
For j = 0 To 50 : cl.Add(byt) : Next
cl.AddRange(nb)
Next
b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
timeArrayList = DateTime.Now.Ti cks - starttime

'Test ArrayStream Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cs As ArrayStream = New ArrayStream(sta rtsize)
For i = 1 To count
For j = 0 To 50 : cs.Add(byt) : Next
cs.AddRange(nb)
Next
b = cs.ToArray
TimeArrayStream = DateTime.Now.Ti cks - starttime

End Sub
End Class

Public Class ByteArray
Private u, c, i As Integer
Private b As Byte()
Public Sub New(ByVal size As Integer)
MyBase.New()
ReDim b(size)
u = size
c = 0
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ReDim Preserve b(c - 1)
Return b
End Get
End Property
Public Sub Add(ByVal nb As Byte)
If c > u Then ReDim Preserve b(c) : u = c
b(c) = nb
c = c + 1
End Sub
Public Sub AddRange(ByVal nb() As Byte)
i = c + UBound(nb, 1)
If i > u Then ReDim Preserve b(i) : u = i
nb.CopyTo(b, c)
c = i + 1
End Sub
End Class

Public Class ArrayStream
Private ms As MemoryStream
Public Sub New(ByVal size As Integer)
MyBase.New()
ms = New MemoryStream(si ze)
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ms.SetLength(ms .Position)
Return ms.ToArray()
End Get
End Property
Public Sub Add(ByVal nb As Byte)
ms.WriteByte(nb )
End Sub

Public Sub AddRange(ByVal nb() As Byte)
ms.Write(nb, 0, UBound(nb) + 1)
End Sub
Protected Overrides Sub finalize()
ms.Close()
MyBase.Finalize ()
End Sub
End Class
"Jay B. Harlow [MVP - Outlook]" wrote:
Dennis,
Your ByteArray is so slow as you are recreating the array each time you add a byte.

Consider doing what ArrayList & MemoryStream do, over allocate the buffer.
Instead of having the buffer contain the exact number of bytes in it, have it contain Capicity # of bytes, then have a second variable for the Length number of bytes.

I posted the start of a ByteBuffer class that functions very similar to the System.Text.Str ingBuffer class in that it maintains a byte array internally allowing you to append byte arrays on the end, expanding the internal array as needed.

http://groups.google.com/groups?q=By...phx.gbl&rnum=3
Hope this helps
Jay

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:FA******** *************** ***********@mic rosoft.com...
I was trying to determine the fastest way to build a byte array from
components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1) using redim arrays
to
add to a normal byte array (2) using an ArrayList and finally (3)
using a memorystream. These three classes are listed below the test sub called "TestBuildByteA rray". It was interesting that using the memorystream was twice as fast as the ArrayList and almost 2000 time as fast as using

normal
byte array with redimensions. Thought some might be interested in this.
Private Sub TestBuildByteAr raySpeed(ByRef timeByteArray As Long, ByRef timeArrayList As Long, ByRef TimeArrayStream As Long)
'Testing speed off byte array building
'Notes: Add in Finalize to close and release memory
Dim starttime As Long
Dim count As Integer = 2000
Dim startsize As Integer = 500
Dim i, j As Integer
Dim byt As Byte = 125
Dim nb As Byte() = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim b() As Byte

'Test ByteArray Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cb As ByteArray = New ByteArray(start size)
For i = 1 To count
For j = 0 To 50 : cb.Add(byt) : Next
cb.AddRange(nb)
Next
b = cb.ToArray
timeByteArray = DateTime.Now.Ti cks - starttime

'Test ArrayList Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cl As ArrayList = New ArrayList(start size)
For i = 1 To count
For j = 0 To 50 : cl.Add(byt) : Next
cl.AddRange(nb)
Next
b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
timeArrayList = DateTime.Now.Ti cks - starttime

'Test ArrayStream Class
ReDim b(0)
starttime = DateTime.Now.Ti cks
Dim cs As ArrayStream = New ArrayStream(500 )
For i = 1 To count
For j = 0 To 50 : cs.Add(byt) : Next
cs.AddRange(nb)
Next
b = cs.ToArray
TimeArrayStream = DateTime.Now.Ti cks - starttime

End Sub
End Class

Public Class ByteArray
Private u, c, i As Integer
Private b As Byte()
Public Sub New(ByVal size As Integer)
MyBase.New()
ReDim b(size)
u = size
c = 0
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
Return b
End Get
End Property
Public Sub Add(ByVal nb As Byte)
If c > u Then ReDim Preserve b(c) : u = c
b(c) = nb
c = c + 1
End Sub
Public Sub AddRange(ByVal nb() As Byte)
i = c + UBound(nb, 1)
If i > u Then ReDim Preserve b(i)
nb.CopyTo(b, c)
c = i
End Sub
End Class

Public Class ArrayStream
Private ms As MemoryStream
Public Sub New(ByVal size As Integer)
MyBase.New()
ms = New MemoryStream(si ze)
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ms.Position = 0
Return ms.ToArray()
End Get
End Property
Public Sub Add(ByVal nb As Byte)
ms.WriteByte(nb )
End Sub

Public Sub AddRange(ByVal nb() As Byte)
ms.Write(nb, 0, UBound(nb) + 1)
End Sub

Protected Overrides Sub finalize()
ms.Close()
MyBase.Finalize ()
End Sub
End Class

--
Dennis in Houston


Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
36821
by: mprocopio | last post by:
Fellow JavaScripters, I am looking for code or implementation ideas for converting an integer variable to a four-byte array. I'm porting some of my code from C#, where I make use of their BitConverter utility class, but am still feeling my way around a bit here with JavaScript. The idea is that each of the four bytes that comprise an integer (I presume numeric types in JavaScript are/can be made four bytes?) are extracted, and then...
1
7227
by: Eric Hendriks | last post by:
// In an unmanaged DLL the following function must be called: // int VFGeneralize(const BYTE * const * features); // "features" parameter is supposed to be an array of byte arrays. // function is Marshaled as follows: static extern int VFGeneralize(byte features); In C# I have the following: // Allocate memory to store "Count" references to byte arrays
8
3407
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr to my struct address but I get the following error, perhaps due to the byte members of my struct: Cannot take the address or size of a variable of a managed type. Ben
7
6886
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ASCIIEncoding asciiEncoder = new ASCIIEncoding();
12
8124
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses raw MMX and SSE power, the starting pointer MUST be starting at a 16 byte memory boundary. In C++ I allocate more memory than needed, and in a second phase I search for the address that starts on a 16 byte boundary. And I then use that new...
32
6369
by: Guoqi Zheng | last post by:
I am really do not know so much about byte/bit, etc. Question, if I defined a byte(), how can I add a single byte to it? what I want is that I have an array of bytes, I loop that array, look at the value of that individual byte, if that byte is what I want, then I insert it into the new byte(). How can I do it? -- Kind regards
5
40093
by: Oleg Subachev | last post by:
Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ? Oleg Subachev
17
7251
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
10
4894
by: =?Utf-8?B?Um95?= | last post by:
What is the way to have best performance to copy a byte to a value such as long? I use BitConverter.ToInt64(binary, offset) But the performace is not good enough. I need to have the best performance in my case.
0
8860
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9382
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2601
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.