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? -
/// <summary>
-
/// Deletes a specific portion of the MemoryBuffer, effectively erasing
-
/// the data in that section and moving the data behind the deleted part
-
/// upwards in the buffer.
-
/// </summary>
-
/// <param name="position">The first position of where to start deleting data.</param>
-
/// <param name="length">The amount of bytes to delete from the MemoryBuffer.</param>
-
public void Delete(int position, int length)
-
{
-
// Obtain a lock first on the MemoryStream for exclusive access.
-
lock (m_lock)
-
{
-
// First check if the range we wish to delete falls withing the
-
// valid working space. This means that the sum of the position
-
// and length cannot surpass our length.
-
if ((position + length) > Length)
-
throw new InvalidOperationException("Specified range falls outside the range of the MemoryBuffer.");
-
-
// We have verified that the selection we wish to delete falls in the
-
// range of the MemoryBuffer. Now we need to extract the data first that
-
// has to be moved.
-
int data_length = Length - (position + length);
-
-
m_stream.Seek(position, SeekOrigin.Begin);
-
m_stream.Write(m_stream.GetBuffer(), (position + length), data_length);
-
-
/*
-
byte[] data = new byte[data_length];
-
-
// Position the memorystream to the offset specified by the user.
-
m_stream.Seek(position + length, SeekOrigin.Begin);
-
-
// Read out the data we need to move.
-
m_stream.Read(data, 0, data_length);
-
-
// Change the length of our class, since this will be shortened by
-
// the deleted part.
-
Length -= length;
-
-
// Write the data at the position we supposedly have deleted.
-
m_stream.Seek(position, SeekOrigin.Begin);
-
m_stream.Write(data, 0, data_length);
-
*/
-
}
-
}
-
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: -
private static void DeleteFromMS(ref MemoryStream ms, int position, int length)
-
{
-
int BeginingSectionLength = (int)(position);
-
int EndingSectionLength = (int)(ms.Length - (position + length));
-
-
byte[] AllGood = new byte[BeginingSectionLength + EndingSectionLength];
-
ms.Seek(0, SeekOrigin.Begin);//rewind!
-
ms.Read(AllGood, 0, BeginingSectionLength);
-
ms.Seek(length, SeekOrigin.Current);//move over the unwanted data
-
ms.Read(AllGood, BeginingSectionLength, EndingSectionLength);
-
ms.Close();
-
ms.Dispose();
-
ms = new MemoryStream(AllGood);
-
}
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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??...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
by: yogeshtiwarijbp |
last post by:
how to insert data inside a table using for loop in asp.net2005
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |