3

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.

epsi1on
  • 561
  • 5
  • 16
  • Have you read about `QNetworkReply::setReadBufferSize`? – Ilya Nov 09 '15 at 09:05
  • @Ilya i've added some extra info on what i'm trying to do, do you think `QNetworkReply::setReadBufferSize` works for me? i'm not so much familiar with Qt – epsi1on Nov 09 '15 at 09:16

1 Answers1

1

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);
  } 
}   
Ari Hietanen
  • 1,749
  • 13
  • 15