Streaming data without saving to HDD
Hey,
I'm working on a project where we have to stream files from a server app to clients. I have it all working nicely, but we have a special client type that cannot save files to HDD, not even temporarily. I can get the byte stream from server to this special client, so only the data handling in the client side is the problem.
I was thinking, is there any way to open the streamed files in the client app without saving them first? For example, if I send a .JPG file from server to client, how can I open the file without saving it as .JPG with FileStream first? Can I do this with MemoryStreams or..? At the moment I have a partial system using MemoryStreams, but I haven't found a way to handle all kinds of file types I need. At the moment it can only handle .TXT and .JPG files, but the list goes on, and I really wouldn't like to write my own byte-parsers for all of them :P
Thanks for your answers in advance :)
bleedi
Junior Poster in Training
75 posts since Jul 2010
Reputation Points: 13
Solved Threads: 1
Well, assuming you aren't sending something that takes up more memory than they have, you can just take the raw byte data, held in memory after a tcp receive, and convert it to whatever you choose.
Here's an example with a bmp:
//assume the byte[] is what was received via socket
private Bitmap MakeBMP(byte[] data)
{
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
return (Bitmap)tc.ConvertFrom(byteArray);
}
Note that the more complex the resulting object is, the harder it is to cast a byte[] into it. BMP, fortunately, is very straight forward. I'm not too sure if the same is true for a JPEG.
This is just about as far as I have got now, but I see a problem here. What if I have a complex file format, that isn't open-sourced? It can be serialized to byte array and streamed over the web... But how can I construct, for example, a Unity .prefab file or Autodesk's .FBX file with TypeConverter (or anything else for that matter)?
EDIT:
Also, another problem here is, I have a closed library I have load the item in. The loader needs a file path, so is there a way to map a virtual path to the memory file? I don't have any object classes to represent the file format, like there is in skamatic's example (Bitmap).
bleedi
Junior Poster in Training
75 posts since Jul 2010
Reputation Points: 13
Solved Threads: 1
CsharpChico
Junior Poster in Training
72 posts since May 2010
Reputation Points: 12
Solved Threads: 8
Okay, I've managed to get a reply from the company that owns the file, and they will be implementing the byte array constructor in their next release. We we're just a bit early building our own software. :)
bleedi
Junior Poster in Training
75 posts since Jul 2010
Reputation Points: 13
Solved Threads: 1
If you had read the thread, you would have noticed that he mentioned this is not for data structures in the public domain (ie bitmap or jpg). Instead he has to come up with a method of loading a closed source function that accepts a path to a file when there is in fact no hard drive to save the file to in order to build an object.
Yes I am aware of that this is why I have mentioned that he can change the byte[] where i get from url to his byte array so it would look like this
Bitmap MyImage(byte[] data, Size size)
{
return new Bitmap(Image.FromStream(new System.IO.MemoryStream(data)),size);
}
Another goog way to use this is also reading from resource when the resource is a binary resource :)
CsharpChico
Junior Poster in Training
72 posts since May 2010
Reputation Points: 12
Solved Threads: 8