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()); }