diff options
Diffstat (limited to 'src/corelib/io/qiodevice.cpp')
-rw-r--r-- | src/corelib/io/qiodevice.cpp | 53 |
1 files changed, 23 insertions, 30 deletions
diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index b908ae3145..f0d3250cd2 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -39,6 +39,7 @@ #include "qfile.h" #include "qstringlist.h" #include "qdir.h" +#include "private/qbytearray_p.h" #include <algorithm> @@ -692,12 +693,13 @@ bool QIODevice::seek(qint64 pos) bool QIODevice::atEnd() const { Q_D(const QIODevice); + const bool result = (d->openMode == NotOpen || (d->buffer.isEmpty() + && bytesAvailable() == 0)); #if defined QIODEVICE_DEBUG printf("%p QIODevice::atEnd() returns %s, d->openMode == %d, d->pos == %lld\n", this, - (d->openMode == NotOpen || d->pos == size()) ? "true" : "false", int(d->openMode), - d->pos); + result ? "true" : "false", int(d->openMode), d->pos); #endif - return d->openMode == NotOpen || (d->buffer.isEmpty() && bytesAvailable() == 0); + return result; } /*! @@ -941,9 +943,9 @@ QByteArray QIODevice::read(qint64 maxSize) Q_UNUSED(d); #endif - if (quint64(maxSize) >= QByteArray::MaxSize) { + if (maxSize >= MaxByteArraySize) { checkWarnMessage(this, "read", "maxSize argument exceeds QByteArray size limit"); - maxSize = QByteArray::MaxSize - 1; + maxSize = MaxByteArraySize - 1; } qint64 readBytes = 0; @@ -990,40 +992,31 @@ QByteArray QIODevice::readAll() #endif QByteArray result; - qint64 readBytes = 0; - const bool sequential = d->isSequential(); - - // flush internal read buffer - if (!(d->openMode & Text) && !d->buffer.isEmpty()) { - if (quint64(d->buffer.size()) >= QByteArray::MaxSize) - return QByteArray(); - result = d->buffer.readAll(); - readBytes = result.size(); - if (!sequential) - d->pos += readBytes; - } - - qint64 theSize; - if (sequential || (theSize = size()) == 0) { + qint64 readBytes = (d->isSequential() ? Q_INT64_C(0) : size()); + if (readBytes == 0) { // Size is unknown, read incrementally. + qint64 readChunkSize = qMax(d->buffer.size(), QIODEVICE_BUFFERSIZE); qint64 readResult; do { - if (quint64(readBytes) + QIODEVICE_BUFFERSIZE > QByteArray::MaxSize) { + if (readBytes + readChunkSize >= MaxByteArraySize) { // If resize would fail, don't read more, return what we have. break; } - result.resize(readBytes + QIODEVICE_BUFFERSIZE); - readResult = read(result.data() + readBytes, QIODEVICE_BUFFERSIZE); - if (readResult > 0 || readBytes == 0) + result.resize(readBytes + readChunkSize); + readResult = read(result.data() + readBytes, readChunkSize); + if (readResult > 0 || readBytes == 0) { readBytes += readResult; + readChunkSize = QIODEVICE_BUFFERSIZE; + } } while (readResult > 0); } else { // Read it all in one go. // If resize fails, don't read anything. - if (quint64(readBytes + theSize - d->pos) > QByteArray::MaxSize) + readBytes -= d->pos; + if (readBytes >= MaxByteArraySize) return QByteArray(); - result.resize(int(readBytes + theSize - d->pos)); - readBytes += read(result.data() + readBytes, result.size() - readBytes); + result.resize(readBytes); + readBytes = read(result.data(), readBytes); } if (readBytes <= 0) @@ -1178,9 +1171,9 @@ QByteArray QIODevice::readLine(qint64 maxSize) Q_UNUSED(d); #endif - if (quint64(maxSize) >= QByteArray::MaxSize) { + if (maxSize >= MaxByteArraySize) { qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit"); - maxSize = QByteArray::MaxSize - 1; + maxSize = MaxByteArraySize - 1; } result.resize(int(maxSize)); @@ -1188,7 +1181,7 @@ QByteArray QIODevice::readLine(qint64 maxSize) if (!result.size()) { // If resize fails or maxSize == 0, read incrementally if (maxSize == 0) - maxSize = QByteArray::MaxSize - 1; + maxSize = MaxByteArraySize - 1; // The first iteration needs to leave an extra byte for the terminating null result.resize(1); |