2
votes

I'm using qt 5.3.

I have a big thing written into a QIODevice for read. I want to have a proxy to get the data while keep the data in QIODevice available for the other thing to read. So if I call readAll() I will get everything good in proxy but the other reader cannot get any data.

I think I should use peek() but this seems requires a maxsize. I tried for passing size() or bytesAvailable() but they didn't get me the real size. They returns probably a buffer size of some weird value as 3287. But my data is as huge as 1081530 bytes.

How can I get the true size of my QIODevice for read?

Edit: The QIODevice I mensioned above is actually QNetworkReply. I want to create a proxy to observe request and response data of my program with a QWebView to access some flash games. I implemented createRequest of my subclass of QNetworkAccessManager and the thing I want to analyze is which I catched through finished() signal of a reply from createRequest.

Edit2: I notice that this QIODevice is a sequential one so the size is unknown. But how to read the data without clearing it?

2
When you read the data store it in a QMap or QHash or else where, is that possible in your situation?ahmed
No, once read, the data will be cleared. Howver, other qt functions, not mine, will try to read the data after I read it.K--

2 Answers

1
votes

You can use QIODevice::seek and then read the data again
from seek documantion:

For random-access devices, this function sets the current position to pos, returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.

0
votes

You can extract the size of the content directly from the QNetworkReply header using Qt like this:

qlonglong size = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong();

And then use that size with the peek function, example:

void DownloadManager::downloadFinished(QNetworkReply *reply)
{
    if (!reply->error())
    {
        bool ok;
        qlonglong size = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(&ok);

        if (ok)
        {
            QByteArray data = reply->peek(size); //Here lives your data without reset the internal buffer
            //Do something
        }
        else
        {
            qDebug() << "Could not read the header";
        }
    }

    //If you are ready then delete
    reply->deleteLater();
}