diff options
Diffstat (limited to 'src/corelib/io/qiodevice.cpp')
-rw-r--r-- | src/corelib/io/qiodevice.cpp | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index fd204b00de..36616a5236 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -1148,34 +1148,31 @@ QByteArray QIODevice::read(qint64 maxSize) Q_D(QIODevice); QByteArray result; - CHECK_MAXLEN(read, result); - #if defined QIODEVICE_DEBUG printf("%p QIODevice::read(%lld), d->pos = %lld, d->buffer.size() = %lld\n", this, maxSize, d->pos, d->buffer.size()); #endif + // Try to prevent the data from being copied, if we have a chunk + // with the same size in the read buffer. + if (maxSize == d->buffer.nextDataBlockSize() && !d->transactionStarted + && (d->openMode & (QIODevice::ReadOnly | QIODevice::Text)) == QIODevice::ReadOnly) { + result = d->buffer.read(); + if (!d->isSequential()) + d->pos += maxSize; + if (d->buffer.isEmpty()) + readData(nullptr, 0); + return result; + } + + CHECK_MAXLEN(read, result); if (maxSize >= MaxByteArraySize) { checkWarnMessage(this, "read", "maxSize argument exceeds QByteArray size limit"); maxSize = MaxByteArraySize - 1; } - 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, qint64(result.size() + d->readBufferChunkSize)))); - readResult = read(result.data() + readBytes, result.size() - readBytes); - if (readResult > 0 || readBytes == 0) - readBytes += readResult; - } while (readResult == d->readBufferChunkSize); - } else { - readBytes = read(result.data(), result.size()); - } - } + result.resize(int(maxSize)); + qint64 readBytes = read(result.data(), result.size()); if (readBytes <= 0) result.clear(); |