views:

177

answers:

2

I have some data in a stream which is actually in the format of uint. System.IO.Stream only allows me to read the content out to a byte array, but I don't want to read the bytes and then convert them into a uint array with 1/4 the length of the byte array. I want to read it directly into the memory of the uint array so that it saves time.

If it's in unmanaged C++ it's just so easy. Just send over the pointer to the uint array and it'd be done. But in C# it doesn't seem so straightforward. I'm trying to produce a byte array with the same memory address as another uint array and then send the byte array as the parameter of Stream.Read() so that when Stream.Read() writes to the byte array the content is just in the uint array as well. But I've looked at the Marshall class and have so far found no solution to this problem. Any ideas?

+2  A: 

I'd think (yes, this is another one of those lazy suggestion type posts devoid of code on sa that people post in the attempt to gain rep in their spare 5 minutes) you could use the same technique in C# by using the unsafe keyword - this allows you to mess with pointers and access memory as is.

Depending on your data of course, I would recommend just doing it the slower more inefficient way - unsafe code isn't really that pretty - you need extra security & time to test.

Matt Jacobsen
I could take the address of the uint array, but Read() method accepts no pointers. It accepts managed byte array only. And I can use pointers to copy the memory of the byte array into the memory of the uint array, but this is not directly reading into the uint array. What matters here is time.
neuron
there's always the option of writing the your implementation in unmanaged c++ and then getting at the result in c# through P/Invoking.
Matt Jacobsen
Just out of interest: What's it for? How big is the dataset anyway? How often will the algorithm be called? Does it have to be interactive, or is it a job on a server somewhere?
Matt Jacobsen
em.. I'm just doing some stock data analysis out of my own interest and the total size of the data is around 200MB. It's not that frequently called, but you know, programmers should really enjoy faster things.
neuron
+1  A: 
List<UInt32> array = new List<UInt32>();
System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
while(stream.Position < stream.Length)
   array.Add(reader.ReadUInt32());

Note that some stream implementations don't support Position and/or Length properties.

Matt Jacobsen
In general correct but from the OQ the length is known beforehand in this case.
Henk Holterman
If the direct solution really couldn't be found, then this might be my last resort.
neuron