Fixed atEnd() and bytesAvailable() to consider QIODevice buffered data

This commit is contained in:
alqualos 2016-03-29 17:51:20 +00:00
parent d4c34ce65b
commit 43dd4b0a7f
2 changed files with 12 additions and 2 deletions

View file

@ -213,6 +213,7 @@ qint64 QuaZIODevice::readData(char *data, qint64 maxSize)
case Z_STREAM_END: case Z_STREAM_END:
read = (char *) d->zins.next_out - data; read = (char *) d->zins.next_out - data;
d->inBufPos = (char *) d->zins.next_in - d->inBuf; d->inBufPos = (char *) d->zins.next_in - d->inBuf;
d->atEnd = true;
return read; return read;
case Z_BUF_ERROR: // this should never happen, but just in case case Z_BUF_ERROR: // this should never happen, but just in case
if (!d->zBufError) { if (!d->zBufError) {
@ -323,10 +324,16 @@ bool QuaZIODevice::isSequential() const
bool QuaZIODevice::atEnd() 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 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();
} }

View file

@ -74,6 +74,9 @@ void TestQuaZIODevice::readMany()
QCOMPARE(testDevice.read(outBuf, 4), static_cast<qint64>(4)); QCOMPARE(testDevice.read(outBuf, 4), static_cast<qint64>(4));
QVERIFY(!testDevice.atEnd()); QVERIFY(!testDevice.atEnd());
QVERIFY(testDevice.bytesAvailable() > 0); QVERIFY(testDevice.bytesAvailable() > 0);
QCOMPARE(testDevice.read(4).size(), 4);
QCOMPARE(testDevice.bytesAvailable(), static_cast<qint64>(0));
QVERIFY(testDevice.atEnd());
testDevice.close(); testDevice.close();
QVERIFY(!testDevice.isOpen()); QVERIFY(!testDevice.isOpen());
} }