473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

"ByteBuilder"...?

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.
Nov 16 '05 #1
7 5436
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:I4******************@news2.e.nsc.no...
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.

Nov 16 '05 #2
Dennis Myrén <de****@oslokb.no> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
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 wrote:
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.

Nov 16 '05 #4
Thank you all for great help.

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:I4******************@news2.e.nsc.no...
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.

Nov 16 '05 #5
> 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.
--
WildHeart'2k4
Nov 16 '05 #6
Thanks for the tip Stefano.
I will check it out right away.

"Stefano "WildHeart" Lanzavecchia" <st********@apl.it> wrote in message
news:eY**************@TK2MSFTNGP10.phx.gbl...
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.
--
WildHeart'2k4

Nov 16 '05 #7
Dennis,
As Jon suggested I would build my own.

I posted the start of a ByteBuffer (ByteBuilder) class that functions very
similar to the
System.Text.StringBuffer 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 Myrén" <de****@oslokb.no> wrote in message
news:I4******************@news2.e.nsc.no...
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.

Nov 16 '05 #8

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

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.