summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJoão Abecasis <joao@abecasis.name>2009-11-16 16:38:18 (GMT)
committerJason McDonald <jason.mcdonald@nokia.com>2009-11-19 03:01:55 (GMT)
commit302152bca4dc690574e1b841b60a343dba47e86a (patch)
tree4979eba40cf5e5b64ff68b3e8363390a59ef6e6c /src
parentf68fa03ab06440d26b8e667fa3b65375d3fa0c00 (diff)
downloadQt-302152bca4dc690574e1b841b60a343dba47e86a.zip
Qt-302152bca4dc690574e1b841b60a343dba47e86a.tar.gz
Qt-302152bca4dc690574e1b841b60a343dba47e86a.tar.bz2
Removing unnecessary chunking and stat'ing when reading QIODevice
Chunk size increased to QIODEVICE_BUFFERSIZE (currently 16k) where chunking is still needed. Namely, on sequential devices and when QByteArray is unable to allocate a large enough buffer. This is necessary for backward compatibility Improved validation and prevention of overflow in maxSize argument. Updated autotest that relied on a null QByteArray when no data was available and no errors were found. The only guarantee we should be providing in this case is an empty result -- even though that behavior is preserved for the time being. Affected functions: * QIODevice::read(qint64 maxSize) Chunking will still happen for large maxSize (i.e., QByteArray resize fails), where it could be used as a synonym for QIODevice::readAll(). No stat'ing performed. Read from device continues for as long as it is successful. Stops if an error occurs or if we get less data than requested. * QIODevice::readAll() Chunking is performed for sequential devices where total size wouldn't be known beforehand. For sequential devices, reading continues as long as data is returned, even if less than requested. Non-sequential devices will be stat'ed once. If QIODevice::size returns 0, this is taken to mean unknown size and chunking is performed. Otherwise, a single read request is made for the specified size. On failure to resize QByteArray, nothing is returned. * QIODevice::readLine(qint64 maxSize) Chunking is performed for maxSize == 0, or if we can't allocate a large enough buffer. No stat'ing performed at this level. Read from device continues until EOL is found, as long as we get all requested data. Task-number: QT-2347 Reviewed-by: Thiago Macieira Reviewed-by: Miikka Heikkinen (cherry picked from commit 1e6b424b692b20dcfec920f8d3563e520ec1ff05)
Diffstat (limited to 'src')
-rw-r--r--src/corelib/io/qiodevice.cpp135
1 files changed, 81 insertions, 54 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp
index 4494d2a..43cc78d 100644
--- a/src/corelib/io/qiodevice.cpp
+++ b/src/corelib/io/qiodevice.cpp
@@ -911,10 +911,10 @@ qint64 QIODevice::read(char *data, qint64 maxSize)
QByteArray QIODevice::read(qint64 maxSize)
{
Q_D(QIODevice);
- CHECK_MAXLEN(read, QByteArray());
- QByteArray tmp;
- qint64 readSoFar = 0;
- char buffer[4096];
+ QByteArray result;
+
+ CHECK_MAXLEN(read, result);
+
#if defined QIODEVICE_DEBUG
printf("%p QIODevice::read(%d), d->pos = %d, d->buffer.size() = %d\n",
this, int(maxSize), int(d->pos), int(d->buffer.size()));
@@ -922,16 +922,34 @@ QByteArray QIODevice::read(qint64 maxSize)
Q_UNUSED(d);
#endif
- do {
- qint64 bytesToRead = qMin(int(maxSize - readSoFar), int(sizeof(buffer)));
- qint64 readBytes = read(buffer, bytesToRead);
- if (readBytes <= 0)
- break;
- tmp.append(buffer, (int) readBytes);
- readSoFar += readBytes;
- } while (readSoFar < maxSize && bytesAvailable() > 0);
+ if (maxSize != qint64(int(maxSize))) {
+ qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
+ maxSize = INT_MAX;
+ }
- return tmp;
+ qint64 readBytes = 0;
+ if (maxSize) {
+ result.resize(int(maxSize));
+ if (!result.size()) {
+ // If resize fails, read incrementally.
+ qint64 readResult;
+ do {
+ result.resize(int(qMin(maxSize, result.size() + QIODEVICE_BUFFERSIZE)));
+ readResult = read(result.data() + readBytes, result.size() - readBytes);
+ if (readResult > 0 || readBytes == 0)
+ readBytes += readResult;
+ } while (readResult == QIODEVICE_BUFFERSIZE);
+ } else {
+ readBytes = read(result.data(), result.size());
+ }
+ }
+
+ if (readBytes <= 0)
+ result.clear();
+ else
+ result.resize(int(readBytes));
+
+ return result;
}
/*!
@@ -952,28 +970,30 @@ QByteArray QIODevice::readAll()
this, int(d->pos), int(d->buffer.size()));
#endif
- QByteArray tmp;
- if (d->isSequential() || size() == 0) {
- // Read it in chunks. Use bytesAvailable() as an unreliable hint for
- // sequential devices, but try to read 4K as a minimum.
- int chunkSize = qMax(qint64(4096), bytesAvailable());
- qint64 totalRead = 0;
- forever {
- tmp.resize(tmp.size() + chunkSize);
- qint64 readBytes = read(tmp.data() + totalRead, chunkSize);
- tmp.chop(chunkSize - (readBytes < 0 ? 0 : readBytes));
- if (readBytes <= 0)
- return tmp;
- totalRead += readBytes;
- chunkSize = qMax(qint64(4096), bytesAvailable());
- }
+ QByteArray result;
+ qint64 readBytes = 0;
+ if (d->isSequential() || (readBytes = size()) == 0) {
+ // Size is unknown, read incrementally.
+ qint64 readResult;
+ do {
+ result.resize(result.size() + QIODEVICE_BUFFERSIZE);
+ readResult = read(result.data() + readBytes, result.size() - readBytes);
+ if (readResult > 0 || readBytes == 0)
+ readBytes += readResult;
+ } while (readResult > 0);
} else {
// Read it all in one go.
- tmp.resize(int(bytesAvailable()));
- qint64 readBytes = read(tmp.data(), tmp.size());
- tmp.resize(readBytes < 0 ? 0 : int(readBytes));
+ // If resize fails, don't read anything.
+ result.resize(int(readBytes - d->pos));
+ readBytes = read(result.data(), result.size());
}
- return tmp;
+
+ if (readBytes <= 0)
+ result.clear();
+ else
+ result.resize(int(readBytes));
+
+ return result;
}
/*!
@@ -1122,11 +1142,9 @@ qint64 QIODevice::readLine(char *data, qint64 maxSize)
QByteArray QIODevice::readLine(qint64 maxSize)
{
Q_D(QIODevice);
- CHECK_MAXLEN(readLine, QByteArray());
- QByteArray tmp;
- const int BufferGrowth = 4096;
- qint64 readSoFar = 0;
- qint64 readBytes = 0;
+ QByteArray result;
+
+ CHECK_MAXLEN(readLine, result);
#if defined QIODEVICE_DEBUG
printf("%p QIODevice::readLine(%d), d->pos = %d, d->buffer.size() = %d\n",
@@ -1135,25 +1153,34 @@ QByteArray QIODevice::readLine(qint64 maxSize)
Q_UNUSED(d);
#endif
- do {
- if (maxSize != 0)
- tmp.resize(int(readSoFar + qMin(int(maxSize), BufferGrowth)));
- else
- tmp.resize(int(readSoFar + BufferGrowth));
- readBytes = readLine(tmp.data() + readSoFar, tmp.size() - readSoFar);
- if (readBytes <= 0)
- break;
-
- readSoFar += readBytes;
- } while ((!maxSize || readSoFar < maxSize) &&
- readSoFar + 1 == tmp.size() && // +1 due to the ending null
- tmp.at(readSoFar - 1) != '\n');
+ if (maxSize > INT_MAX) {
+ qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
+ maxSize = INT_MAX;
+ }
- if (readSoFar == 0 && readBytes == -1)
- tmp.clear(); // return Null if we found an error
+ result.resize(int(maxSize));
+ qint64 readBytes = 0;
+ if (!result.size()) {
+ // If resize fails or maxSize == 0, read incrementally
+ if (maxSize == 0)
+ maxSize = INT_MAX;
+ qint64 readResult;
+ do {
+ result.resize(int(qMin(maxSize, result.size() + QIODEVICE_BUFFERSIZE)));
+ readResult = readLine(result.data() + readBytes, result.size() - readBytes);
+ if (readResult > 0 || readBytes == 0)
+ readBytes += readResult;
+ } while (readResult == QIODEVICE_BUFFERSIZE
+ && result[int(readBytes)] != '\n');
+ } else
+ readBytes = readLine(result.data(), result.size());
+
+ if (readBytes <= 0)
+ result.clear();
else
- tmp.resize(int(readSoFar));
- return tmp;
+ result.resize(readBytes);
+
+ return result;
}
/*!