QuaZioDevice::atEnd() and bytesAvailable() (might break ABI, but

probably safe)
This commit is contained in:
alqualos 2016-03-29 17:41:12 +00:00
parent 84e2472a27
commit d4c34ce65b
4 changed files with 45 additions and 2 deletions

View file

@ -42,6 +42,7 @@ class QuaZIODevicePrivate {
int outBufPos;
int outBufSize;
bool zBufError;
bool atEnd;
int doFlush(QString &error);
};
@ -53,7 +54,8 @@ QuaZIODevicePrivate::QuaZIODevicePrivate(QIODevice *io):
outBuf(NULL),
outBufPos(0),
outBufSize(0),
zBufError(false)
zBufError(false),
atEnd(false)
{
zins.zalloc = (alloc_func) NULL;
zins.zfree = (free_func) NULL;
@ -318,3 +320,13 @@ bool QuaZIODevice::isSequential() const
{
return true;
}
bool QuaZIODevice::atEnd() const
{
return (openMode() == NotOpen) || d->atEnd;
}
qint64 QuaZIODevice::bytesAvailable() const
{
return (atEnd() ? 0 : 1) + QIODevice::bytesAvailable();
}

View file

@ -87,6 +87,10 @@ public:
QIODevice *getIoDevice() const;
/// Returns true.
virtual bool isSequential() const;
/// Returns true iff the end of the compressed stream is reached.
virtual bool atEnd() const;
/// Returns the number of the bytes buffered.
virtual qint64 bytesAvailable() const;
protected:
/// Implementation of QIODevice::readData().
virtual qint64 readData(char *data, qint64 maxSize);

View file

@ -52,6 +52,32 @@ void TestQuaZIODevice::read()
QVERIFY(!testDevice.isOpen());
}
void TestQuaZIODevice::readMany()
{
QByteArray buf(256, 0);
z_stream zouts;
zouts.zalloc = (alloc_func) NULL;
zouts.zfree = (free_func) NULL;
zouts.opaque = NULL;
deflateInit(&zouts, Z_DEFAULT_COMPRESSION);
zouts.next_in = reinterpret_cast<Bytef*>(const_cast<char*>("testtest"));
zouts.avail_in = 8;
zouts.next_out = reinterpret_cast<Bytef*>(buf.data());
zouts.avail_out = buf.size();
deflate(&zouts, Z_FINISH);
deflateEnd(&zouts);
QBuffer testBuffer(&buf);
testBuffer.open(QIODevice::ReadOnly);
QuaZIODevice testDevice(&testBuffer);
QVERIFY(testDevice.open(QIODevice::ReadOnly));
char outBuf[4];
QCOMPARE(testDevice.read(outBuf, 4), static_cast<qint64>(4));
QVERIFY(!testDevice.atEnd());
QVERIFY(testDevice.bytesAvailable() > 0);
testDevice.close();
QVERIFY(!testDevice.isOpen());
}
void TestQuaZIODevice::write()
{
QByteArray buf(256, 0);

View file

@ -31,6 +31,7 @@ class TestQuaZIODevice: public QObject {
Q_OBJECT
private slots:
void read();
void readMany();
void write();
};