summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/io/qbuffer.cpp17
-rw-r--r--tests/auto/qbuffer/tst_qbuffer.cpp50
2 files changed, 56 insertions, 11 deletions
diff --git a/src/corelib/io/qbuffer.cpp b/src/corelib/io/qbuffer.cpp
index 38e406b1a4..35e7b6809c 100644
--- a/src/corelib/io/qbuffer.cpp
+++ b/src/corelib/io/qbuffer.cpp
@@ -333,23 +333,18 @@ bool QBuffer::open(OpenMode flags)
{
Q_D(QBuffer);
- if ((flags & Append) == Append)
+ if ((flags & (Append | Truncate)) != 0)
flags |= WriteOnly;
- setOpenMode(flags);
- if (!(isReadable() || isWritable())) {
- qWarning("QFile::open: File access not specified");
+ if ((flags & (ReadOnly | WriteOnly)) == 0) {
+ qWarning("QBuffer::open: Buffer access not specified");
return false;
}
- if ((flags & QIODevice::Truncate) == QIODevice::Truncate) {
+ if ((flags & Truncate) == Truncate)
d->buf->resize(0);
- }
- if ((flags & QIODevice::Append) == QIODevice::Append) // append to end of buffer
- seek(d->buf->size());
- else
- seek(0);
+ d->ioIndex = (flags & Append) == Append ? d->buf->size() : 0;
- return true;
+ return QIODevice::open(flags);
}
/*!
diff --git a/tests/auto/qbuffer/tst_qbuffer.cpp b/tests/auto/qbuffer/tst_qbuffer.cpp
index 776935d6e0..bf4842ff94 100644
--- a/tests/auto/qbuffer/tst_qbuffer.cpp
+++ b/tests/auto/qbuffer/tst_qbuffer.cpp
@@ -56,6 +56,7 @@ public:
tst_QBuffer();
private slots:
+ void open();
void getSetCheck();
void readBlock();
void readBlockPastEnd();
@@ -104,6 +105,55 @@ tst_QBuffer::tst_QBuffer()
{
}
+void tst_QBuffer::open()
+{
+ QByteArray data(10, 'f');
+
+ QBuffer b;
+
+ QTest::ignoreMessage(QtWarningMsg, "QBuffer::open: Buffer access not specified");
+ QVERIFY(!b.open(QIODevice::NotOpen));
+ QVERIFY(!b.isOpen());
+ b.close();
+
+ QTest::ignoreMessage(QtWarningMsg, "QBuffer::open: Buffer access not specified");
+ QVERIFY(!b.open(QIODevice::Text));
+ QVERIFY(!b.isOpen());
+ b.close();
+
+ QTest::ignoreMessage(QtWarningMsg, "QBuffer::open: Buffer access not specified");
+ QVERIFY(!b.open(QIODevice::Unbuffered));
+ QVERIFY(!b.isOpen());
+ b.close();
+
+ QVERIFY(b.open(QIODevice::ReadOnly));
+ QVERIFY(b.isReadable());
+ b.close();
+
+ QVERIFY(b.open(QIODevice::WriteOnly));
+ QVERIFY(b.isWritable());
+ b.close();
+
+ b.setData(data);
+ QVERIFY(b.open(QIODevice::Append));
+ QVERIFY(b.isWritable());
+ QCOMPARE(b.size(), qint64(10));
+ QCOMPARE(b.pos(), b.size());
+ b.close();
+
+ b.setData(data);
+ QVERIFY(b.open(QIODevice::Truncate));
+ QVERIFY(b.isWritable());
+ QCOMPARE(b.size(), qint64(0));
+ QCOMPARE(b.pos(), qint64(0));
+ b.close();
+
+ QVERIFY(b.open(QIODevice::ReadWrite));
+ QVERIFY(b.isReadable());
+ QVERIFY(b.isWritable());
+ b.close();
+}
+
// some status() tests, too
void tst_QBuffer::readBlock()
{