0

Let's say I read a stream into a buffer, and I want to make multiple insertions into this buffer. Is there some efficient helper object I can call to help with this. Say if I want to make an insertion of 10 bytes length at position 100, then 20 bytes length into position 500 (which would now be 510 after inserting the first 10 bytes). What I'm after is something to keep track of where to actually make the insertions when inserting multiple times (otherwise I have to shift all address by the what I've already inserted).

It would be simple to roll my own, but I was wondering if I'm reinventing the wheel here, and this is already included in some .NET construct?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
friartuck
  • 2,954
  • 4
  • 33
  • 67

1 Answers1

0

Roll your own (or search for existing libraries) - there is nothing that helps to keep track of insertions to minimize copying bytes around.

Profile straightforward solution with byte array and Buffer.BlockCopy for each insertion to make sure that approach is indeed not acceptable. (Check out Array.Copy vs Buffer.BlockCopy to pick copy method you like).

One option to optimize the code - instead of copying keep sorted list of ranges (if ranges intersect it will get tricky...) and merge results once.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Hello, your last paragraph, I think you are explaining what I was getting at in the last sentence of my first paragraph. Niave way would be to keep doing big copys for each new insertion, rather than batching all insertions and making them in sequence (sorted). I don't need to worry about intersecting ranges Looks like I will have to roll my own, which shouldn't be hard, but wanted to be sure there wasn't something already – friartuck Jan 09 '15 at 01:51