From 43dd4b0a7f61190c16d63c069ec41919123ffd1a Mon Sep 17 00:00:00 2001 From: alqualos Date: Tue, 29 Mar 2016 17:51:20 +0000 Subject: [PATCH] Fixed atEnd() and bytesAvailable() to consider QIODevice buffered data --- quazip/quazip/quaziodevice.cpp | 11 +++++++++-- quazip/qztest/testquaziodevice.cpp | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/quazip/quazip/quaziodevice.cpp b/quazip/quazip/quaziodevice.cpp index 8580fcf..04f34bf 100644 --- a/quazip/quazip/quaziodevice.cpp +++ b/quazip/quazip/quaziodevice.cpp @@ -213,6 +213,7 @@ qint64 QuaZIODevice::readData(char *data, qint64 maxSize) case Z_STREAM_END: read = (char *) d->zins.next_out - data; d->inBufPos = (char *) d->zins.next_in - d->inBuf; + d->atEnd = true; return read; case Z_BUF_ERROR: // this should never happen, but just in case if (!d->zBufError) { @@ -323,10 +324,16 @@ bool QuaZIODevice::isSequential() const bool QuaZIODevice::atEnd() const { - return (openMode() == NotOpen) || d->atEnd; + // Here we MUST check QIODevice::bytesAvailable() because WE + // might have reached the end, but QIODevice didn't-- + // it could have simply pre-buffered all remaining data. + return (openMode() == NotOpen) || (QIODevice::bytesAvailable() == 0 && d->atEnd); } qint64 QuaZIODevice::bytesAvailable() const { - return (atEnd() ? 0 : 1) + QIODevice::bytesAvailable(); + // If we haven't recevied Z_STREAM_END, it means that + // we have at least one more input byte available. + // Plus whatever QIODevice has buffered. + return (d->atEnd ? 0 : 1) + QIODevice::bytesAvailable(); } diff --git a/quazip/qztest/testquaziodevice.cpp b/quazip/qztest/testquaziodevice.cpp index 56963b2..59489e4 100644 --- a/quazip/qztest/testquaziodevice.cpp +++ b/quazip/qztest/testquaziodevice.cpp @@ -74,6 +74,9 @@ void TestQuaZIODevice::readMany() QCOMPARE(testDevice.read(outBuf, 4), static_cast(4)); QVERIFY(!testDevice.atEnd()); QVERIFY(testDevice.bytesAvailable() > 0); + QCOMPARE(testDevice.read(4).size(), 4); + QCOMPARE(testDevice.bytesAvailable(), static_cast(0)); + QVERIFY(testDevice.atEnd()); testDevice.close(); QVERIFY(!testDevice.isOpen()); }