469,572 Members | 1,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Moving data inside the MemoryStream

Airslash
221 100+
Hello,

I'm currently working with a MemoryStream, because I need to work with a dynamic buffer that can grow depending on the needs. But I require it to be more flexible that the standard defines.

For example, I want to be able to delete a portion of the buffer and move the data behind the deleted part upwards, sort of like in a List.

To perform a delete on the "buffer", I wrote the following function. Anyone tried this before?

Expand|Select|Wrap|Line Numbers
  1. /// <summary>
  2.         /// Deletes a specific portion of the MemoryBuffer, effectively erasing
  3.         /// the data in that section and moving the data behind the deleted part
  4.         /// upwards in the buffer.
  5.         /// </summary>
  6.         /// <param name="position">The first position of where to start deleting data.</param>
  7.         /// <param name="length">The amount of bytes to delete from the MemoryBuffer.</param>
  8.         public void Delete(int position, int length)
  9.         {
  10.             // Obtain a lock first on the MemoryStream for exclusive access.
  11.             lock (m_lock)
  12.             {
  13.                 // First check if the range we wish to delete falls withing the
  14.                 // valid working space. This means that the sum of the position
  15.                 // and length cannot surpass our length.
  16.                 if ((position + length) > Length)
  17.                     throw new InvalidOperationException("Specified range falls outside the range of the MemoryBuffer.");
  18.  
  19.                 // We have verified that the selection we wish to delete falls in the
  20.                 // range of the MemoryBuffer. Now we need to extract the data first that
  21.                 // has to be moved.
  22.                 int data_length = Length - (position + length);
  23.  
  24.                 m_stream.Seek(position, SeekOrigin.Begin);
  25.                 m_stream.Write(m_stream.GetBuffer(), (position + length), data_length);
  26.  
  27.                 /*
  28.                 byte[] data = new byte[data_length];
  29.  
  30.                 // Position the memorystream to the offset specified by the user.
  31.                 m_stream.Seek(position + length, SeekOrigin.Begin);
  32.  
  33.                 // Read out the data we need to move.
  34.                 m_stream.Read(data, 0, data_length);
  35.  
  36.                 // Change the length of our class, since this will be shortened by
  37.                 // the deleted part.
  38.                 Length -= length;
  39.  
  40.                 // Write the data at the position we supposedly have deleted.
  41.                 m_stream.Seek(position, SeekOrigin.Begin);
  42.                 m_stream.Write(data, 0, data_length);
  43.                 */
  44.             }
  45.         }
  46.  
Sep 10 '10 #1
2 5038
Plater
7,872 Expert 4TB
Could you use a List<byte> ?

Also does your function work? Looking briefly it looks like it was doing things backwards?

I left out all the error checking, but you get the idea:
Expand|Select|Wrap|Line Numbers
  1. private static void DeleteFromMS(ref MemoryStream ms, int position, int length)
  2. {
  3.   int BeginingSectionLength = (int)(position);
  4.   int EndingSectionLength = (int)(ms.Length - (position + length));
  5.  
  6.   byte[] AllGood = new byte[BeginingSectionLength + EndingSectionLength];
  7.   ms.Seek(0, SeekOrigin.Begin);//rewind!
  8.   ms.Read(AllGood, 0, BeginingSectionLength);
  9.   ms.Seek(length, SeekOrigin.Current);//move over the unwanted data
  10.   ms.Read(AllGood, BeginingSectionLength, EndingSectionLength);
  11.   ms.Close();
  12.   ms.Dispose();
  13.   ms = new MemoryStream(AllGood);
  14. }
  15.  
Although to be honest, using a MemoryStream seems like a waste (unless you HAVE to use a derivative of the Stream class)
Seems like a byte[](Or List<byte>.ToArray() ) with Array.Copy() would be just as good
Sep 10 '10 #2
Airslash
221 100+
will give it a spin on monday.
Just looking for a means to do it as effective as possible without getting to much copies & new statements that take the performance down.

Might give it a go with a List and see what it does.
Sep 10 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by genojoe | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.