Assume that I have executed a QNetworkRequest
and got the appropriated QNetworkReply
. If it be a large file (say 1 GB) how can I create a say 4k byte array buffer and read data 4k by 4k into that array and write it at same time into an open file stream?
For example the equivalent C# code would be this (I'm familiar with C# not Qt):
public static void CopyStream(Stream input, Stream output)
{
// input is web stream, output is filestream
byte[] buffer = new byte[4096];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write (buffer, 0, read);
}
}
---- edit
Actually what i am trying to do is a download resume capability on every run of my application. every time i try to resume the download i set the range
header of QNetworkRequest
and just get the rest of data so need to write data not at once but step by step.
You probably want a solution in c++ as Qt is a library. One possibility is to use QIODevice class, which is inherited by both QFile and QNetworkReply
void copyStream(QIODevice& input, QIODevice& output){
std::vector<char> buffer(4096);
qint64 bytesRead;
while ((bytesRead=input.read(&buffer[0],buffer.size()))>0){
output.write(&buffer[0],bytesRead);
}
}