PDA

View Full Version : Better usage of a downloaded binary



Momergil
7th March 2012, 20:47
Hello!

In my application I need to download a binary file from a server and use its contents. I was already able to download it and save it into a binary file, so it seems that the downloaded part is OK.

The problem is that I don't know what is the best option: to download the file in a .bin and open it with Qt and read it, or to save the data into a internal buffer and to use this buffer. Interesting to notice is that I don't need the file in my PC.

In case I donwload and save it in a buffer inside my software, which data "container" is the best to storage the bin data? QByteArray? And which functions and classes should I use to read this data and work with it given the fact is binary?


Thanks!

Momergil

d_stranz
8th March 2012, 00:04
If you don't need to actually store the file on disk after retrieving it from the server, then keeping it in memory is just fine as long as you are always guaranteed that there will be enough memory available to keep the entire file. What happens if you run out of memory halfway through the download? I personally think it is far better to break a process like this into independent, reliable parts: first download the file and save it (to a temporary file if you want), then open the file and read it into memory. In this case, if the file is too big to completely read into memory, then at least you haven't lost the entire thing and your application can take an alternative path and use it in pieces if possible.

As far as what do do with it after you have read it in, this is like asking "How long is a piece of string?".

It depends entirely on what is inside the file. Is it an EXE, is it an MP3, is it a video? If the file has structured data inside it, then you would want to map that data onto C++ objects that put the binary data into appropriate member variables. Since you haven't told us anything about the files except that they are binary, we can only guess at how you might want to use one. QByteArray is probably fine for holding the raw bytes of the file, but in order to do anything with it, you'll have to map the binary bytes in QByteArray into something meaningful.

ChrisW67
8th March 2012, 03:04
Also take a look at QBuffer.

Momergil
8th March 2012, 17:57
Thanks for the tips! That certanly will help.



Momergil