diff options
Diffstat (limited to 'src/corelib/io/qiodevice.cpp')
-rw-r--r-- | src/corelib/io/qiodevice.cpp | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index f31ac75425..fbee1a223f 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -739,6 +739,18 @@ void QIODevicePrivate::setWriteChannelCount(int count) } /*! + \internal +*/ +bool QIODevicePrivate::allWriteBuffersEmpty() const +{ + for (const QRingBuffer &ringBuffer : writeBuffers) { + if (!ringBuffer.isEmpty()) + return false; + } + return true; +} + +/*! Opens the device and sets its OpenMode to \a mode. Returns \c true if successful; otherwise returns \c false. This function should be called from any reimplementations of open() or other functions that open the device. @@ -1148,34 +1160,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(); |