473,466 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Moving data inside the MemoryStream

Airslash
221 New Member
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 5744
Plater
7,872 Recognized Expert Expert
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 New Member
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

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

Similar topics

7
by: bjam | last post by:
Hi, I am trying to have a style sheet read an xml file that has some tagged data inside of it that has the name of the section the syle sheet should process, does anyone know how I can do this??...
6
by: davidmdalle | last post by:
Hello, I have been having a bit of trouble finding help on the safest way to move data files to a different disk on the same server. Most help is about moving data files to a different sqlserver. ...
3
by: genojoe | last post by:
I have tried everything I can think of to move records from one Access database to another. It should not be that hard. Read on.... I have identical access mdb's. One contains data; one...
3
by: Iavor Raytchev | last post by:
Hello, We a situation with a central database that contains the data that needs to be presented at N off-line terminals (N can be 5 000 can be 15 000). Each terminal presents unique data. The...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
11
by: ulyses | last post by:
Let's assume I have following file: 2938929384902491233..... 923949919199191919112.... File contains INTs only. What is more they are huge. For example first row in file may contain integer...
0
by: yogeshtiwarijbp | last post by:
how to insert data inside a table using for loop in asp.net2005
2
by: jleadbetter | last post by:
MS SQL 2005 Express When a project is set to a completed status...I would like the data to move into a different table. In other words...if the "Completed" value is set to "True"...the data would...
1
by: dragonrose | last post by:
Hi all, im new to coding (started coding last month) so im a little stuck on how to do this, im making some emulation programs to learn coding with, and a part of it is to point the app to a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.