"ByteBuilder"...?

  • Thread starter Thread starter Dennis Myrén
  • Start date Start date
D

Dennis Myrén

Hi.
I need a class similar to StringBuilder but that instead of generating a
System.String,
that generates a System.Byte array.
A System.IO.Stream would do it, but i also need an Insert function, to be
able to insert bytes at given positions in the array.
I could do this by myself, but i cant come up with any more efficient than
creating a new byte array
for each Insert invocation, copy the bytes to be inserted + the originial
array to that array and
finally assign the temporary array to the original array.
Feels like this could be a true performance killer when doing recursive
calls to Insert.
I really cant come up with anything more efficient, could any of you?

I am really appreciating help on this one.
Thank you. Dennis.
 
Dennis,

If you need insert functionality, then the only option you have
available to you (from the framework) is the ArrayList class.
Unfortunately, this will cause a lot of boxing operations which could be
costly in the long run.

If you have access to a beta of .NET 2.0, then I would recommend using a
List<byte>, this would give you what you want.

If not (and you don't have plans to migrate), I would recommend creating
your own class which works like the ArrayList, but uses an array of bytes
internally, and is strongly typed.

Hope this helps.
 
Strictly speaking it would be best to implement your own class for the
same, as there is nothing in the Framework for what u require. The
algorithm you are looking for is called sparsely populated arrays, in
which you have a lot of inserts in the middle of the data structure.

But one other solution is to use an ArrayList, but the cost here is the
time taken to box each byte into an object. You can run some performance
tests to see which solution is better.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Dennis Myrén said:
I need a class similar to StringBuilder but that instead of generating a
System.String,
that generates a System.Byte array.
A System.IO.Stream would do it, but i also need an Insert function, to be
able to insert bytes at given positions in the array.
I could do this by myself, but i cant come up with any more efficient than
creating a new byte array
for each Insert invocation, copy the bytes to be inserted + the originial
array to that array and
finally assign the temporary array to the original array.
Feels like this could be a true performance killer when doing recursive
calls to Insert.
I really cant come up with anything more efficient, could any of you?

Do what StringBuilder does - have a buffer which is larger than the
size of the actual data. When you need to insert, you then shuffle
things within the array, etc. If you're ever asked to insert data which
blows the buffer, just create a new buffer (e.g. doubling the size of
the old one), copy the old information, and then use that bigger
buffer.

That's basically what ArrayList does too, I believe.
 
I need a class similar to StringBuilder but that instead of generating a
System.String,
that generates a System.Byte array.

Have a look at the source code of the free web server called "Cassini"
(http://www.asp.net/Default.aspx?tabindex=6&tabid=41). If my memory serves
me right you'll find something very close to what you need. I had to modify
it slightly for a project of mine to include some extra append operations,
but it's a good starting point.
 
Back
Top