#include <qbuffer.h>
Inheritance diagram for QBuffer:


QBuffer allows you to access a QByteArray using the QIODevice interface. The QByteArray is treated just as a standard random-accessed file. Example:
snippets/buffer/buffer.cpp main_snippet QBuffer buffer /^$/
By default, an internal QByteArray buffer is created for you when you create a QBuffer. You can access this buffer directly by calling buffer(). You can also use QBuffer with an existing QByteArray by calling setBuffer(), or by passing your array to QBuffer's constructor.
Call open() to open the buffer. Then call write() or putChar() to write to the buffer, and read(), readLine(), readAll(), or getChar() to read from it. size() returns the current size of the buffer, and you can seek to arbitrary positions in the buffer by calling seek(). When you are done with accessing the buffer, call close().
The following code snippet shows how to write data to a QByteArray using QDataStream and QBuffer:
write_datastream_snippet QByteArray /^$/
Effectively, we convert the application's QPalette into a byte array. Here's how to read the data from the QByteArray:
read_datastream_snippet QPalette /^$/
QTextStream and QDataStream also provide convenience constructors that take a QByteArray and that create a QBuffer behind the scenes.
QBuffer emits readyRead() when new data has arrived in the buffer. By connecting to this signal, you can use QBuffer to store temporary data before processing it. For example, you can pass the buffer to QFtp when downloading a file from an FTP server. Whenever a new payload of data has been downloaded, readyRead() is emitted, and you can process the data that just arrived. QBuffer also emits bytesWritten() every time new data has been written to the buffer.
Definition at line 37 of file qbuffer.h.
Public Member Functions | |
| QBuffer (QObject *parent=0) | |
| QBuffer (QByteArray *buf, QObject *parent=0) | |
| ~QBuffer () | |
| QByteArray & | buffer () |
| const QByteArray & | buffer () const |
| void | setBuffer (QByteArray *a) |
| void | setData (const QByteArray &data) |
| void | setData (const char *data, int len) |
| const QByteArray & | data () const |
| bool | open (OpenMode openMode) |
| void | close () |
| qint64 | size () const |
| qint64 | pos () const |
| bool | seek (qint64 off) |
| bool | atEnd () const |
| bool | canReadLine () const |
Protected Member Functions | |
| qint64 | readData (char *data, qint64 maxlen) |
| qint64 | writeData (const char *data, qint64 len) |
| QBuffer::QBuffer | ( | QObject * | parent = 0 |
) | [explicit] |
Constructs an empty buffer with the given parent. You can call setData() to fill the buffer with data, or you can open it in write mode and use write().
Definition at line 149 of file qbuffer.cpp.
References d.
00150 : QIODevice(*new QBufferPrivate, parent) 00151 { 00152 Q_D(QBuffer); 00153 d->buf = &d->defaultBuf; 00154 d->ioIndex = 0; 00155 }
| QBuffer::QBuffer | ( | QByteArray * | byteArray, | |
| QObject * | parent = 0 | |||
| ) |
Constructs a QBuffer that uses the QByteArray pointed to by byteArray as its internal buffer, and with the given parent. The caller is responsible for ensuring that byteArray remains valid until the QBuffer is destroyed, or until setBuffer() is called to change the buffer. QBuffer doesn't take ownership of the QByteArray.
If you open the buffer in write-only mode or read-write mode and write something into the QBuffer, byteArray will be modified.
Example:
snippets/buffer/buffer.cpp bytearray_ptr_ctor_snippet QByteArray /^/
Definition at line 177 of file qbuffer.cpp.
References d.
00178 : QIODevice(*new QBufferPrivate, parent) 00179 { 00180 Q_D(QBuffer); 00181 d->buf = byteArray ? byteArray : &d->defaultBuf; 00182 d->defaultBuf.clear(); 00183 d->ioIndex = 0; 00184 }
| QBuffer::~QBuffer | ( | ) |
| QByteArray & QBuffer::buffer | ( | ) |
Returns a reference to the QBuffer's internal buffer. You can use it to modify the QByteArray behind the QBuffer's back.
Definition at line 243 of file qbuffer.cpp.
References d.
Referenced by QPicturePrivate::checkFormat(), qt_get_net_supported(), and qt_get_net_virtual_roots().
| const QByteArray & QBuffer::buffer | ( | ) | const |
| void QBuffer::setBuffer | ( | QByteArray * | byteArray | ) |
Makes QBuffer uses the QByteArray pointed to by byteArray as its internal buffer. The caller is responsible for ensuring that byteArray remains valid until the QBuffer is destroyed, or until setBuffer() is called to change the buffer. QBuffer doesn't take ownership of the QByteArray.
Does nothing if isOpen() is true.
If you open the buffer in write-only mode or read-write mode and write something into the QBuffer, byteArray will be modified.
Example:
snippets/buffer/buffer.cpp setBuffer_snippet QByteArray /^/
If byteArray is 0, the buffer creates its own internal QByteArray to work on. This byte array is initially empty.
Definition at line 220 of file qbuffer.cpp.
References d, QIODevice::isOpen(), and qWarning().
00221 { 00222 Q_D(QBuffer); 00223 if (isOpen()) { 00224 qWarning("QBuffer::setBuffer: Buffer is open"); 00225 return; 00226 } 00227 if (byteArray) { 00228 d->buf = byteArray; 00229 } else { 00230 d->buf = &d->defaultBuf; 00231 } 00232 d->defaultBuf.clear(); 00233 d->ioIndex = 0; 00234 }
Here is the call graph for this function:

| void QBuffer::setData | ( | const QByteArray & | data | ) |
Sets the contents of the internal buffer to be data. This is the same as assigning data to buffer().
Does nothing if isOpen() is true.
Definition at line 284 of file qbuffer.cpp.
References d, data(), QIODevice::isOpen(), and qWarning().
Referenced by setData().
00285 { 00286 Q_D(QBuffer); 00287 if (isOpen()) { 00288 qWarning("QBuffer::setData: Buffer is open"); 00289 return; 00290 } 00291 *d->buf = data; 00292 d->ioIndex = 0; 00293 }
Here is the call graph for this function:

| void QBuffer::setData | ( | const char * | data, | |
| int | size | |||
| ) | [inline] |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Sets the contents of the internal buffer to be the first size bytes of data.
Definition at line 81 of file qbuffer.h.
References setData().
00082 { setData(QByteArray(adata, alen)); }
Here is the call graph for this function:

| const QByteArray & QBuffer::data | ( | ) | const |
Returns the data contained in the buffer.
This is the same as buffer().
Definition at line 270 of file qbuffer.cpp.
References d.
Referenced by setData().
| bool QBuffer::open | ( | OpenMode | flags | ) | [virtual] |
Reimplemented from QIODevice.
Definition at line 307 of file qbuffer.cpp.
References QIODevice::Append, d, QIODevice::isReadable(), QIODevice::isWritable(), qWarning(), seek(), QIODevice::setOpenMode(), QIODevice::Truncate, and QIODevice::WriteOnly.
Referenced by QPicturePrivate::checkFormat(), main(), qt_get_net_supported(), and qt_get_net_virtual_roots().
00308 { 00309 Q_D(QBuffer); 00310 00311 if ((flags & Append) == Append) 00312 flags |= WriteOnly; 00313 setOpenMode(flags); 00314 if (!(isReadable() || isWritable())) { 00315 qWarning("QFile::open: File access not specified"); 00316 return false; 00317 } 00318 00319 if ((flags & QIODevice::Truncate) == QIODevice::Truncate) { 00320 d->buf->resize(0); 00321 } 00322 if ((flags & QIODevice::Append) == QIODevice::Append) // append to end of buffer 00323 seek(d->buf->size()); 00324 else 00325 seek(0); 00326 00327 return true; 00328 }
Here is the call graph for this function:

| void QBuffer::close | ( | ) | [virtual] |
Reimplemented from QIODevice.
Definition at line 333 of file qbuffer.cpp.
References QIODevice::close().
Referenced by QPicturePrivate::checkFormat().
00334 { 00335 QIODevice::close(); 00336 }
Here is the call graph for this function:

| qint64 QBuffer::size | ( | ) | const [virtual] |
Reimplemented from QIODevice.
Definition at line 349 of file qbuffer.cpp.
References d.
Referenced by QPicturePrivate::checkFormat().
| qint64 QBuffer::pos | ( | ) | const [virtual] |
Reimplemented from QIODevice.
Definition at line 341 of file qbuffer.cpp.
References QIODevice::pos().
Referenced by canReadLine().
00342 { 00343 return QIODevice::pos(); 00344 }
Here is the call graph for this function:

| bool QBuffer::seek | ( | qint64 | pos | ) | [virtual] |
Reimplemented from QIODevice.
Definition at line 358 of file qbuffer.cpp.
References d, int, qWarning(), and QIODevice::seek().
Referenced by open().
00359 { 00360 Q_D(QBuffer); 00361 if (pos < 0 || pos >= d->buf->size() + 1) { 00362 qWarning("QBuffer::seek: Invalid pos: %d", int(pos)); 00363 return false; 00364 } 00365 d->ioIndex = int(pos); 00366 return QIODevice::seek(pos); 00367 }
Here is the call graph for this function:

| bool QBuffer::atEnd | ( | ) | const [virtual] |
Reimplemented from QIODevice.
Definition at line 372 of file qbuffer.cpp.
References QIODevice::atEnd().
00373 { 00374 return QIODevice::atEnd(); 00375 }
Here is the call graph for this function:

| bool QBuffer::canReadLine | ( | ) | const [virtual] |
Reimplemented from QIODevice.
Definition at line 380 of file qbuffer.cpp.
References QIODevice::canReadLine(), d, QIODevice::isOpen(), and pos().
00381 { 00382 Q_D(const QBuffer); 00383 if (!isOpen()) 00384 return false; 00385 00386 return d->buf->indexOf('\n', int(pos())) != -1 || QIODevice::canReadLine(); 00387 }
Here is the call graph for this function:

Implements QIODevice.
Definition at line 392 of file qbuffer.cpp.
References d, int, and qMin().
00393 { 00394 Q_D(QBuffer); 00395 if ((len = qMin(len, qint64(d->buf->size()) - d->ioIndex)) <= 0) 00396 return qint64(0); 00397 memcpy(data, d->buf->constData() + d->ioIndex, len); 00398 d->ioIndex += int(len); 00399 return len; 00400 }
Here is the call graph for this function:

Implements QIODevice.
Definition at line 405 of file qbuffer.cpp.
References d, int, QMetaObject::invokeMethod(), Qt::QueuedConnection, and qWarning().
00406 { 00407 Q_D(QBuffer); 00408 int extraBytes = d->ioIndex + len - d->buf->size(); 00409 if (extraBytes > 0) { // overflow 00410 int newSize = d->buf->size() + extraBytes; 00411 d->buf->resize(newSize); 00412 if (d->buf->size() != newSize) { // could not resize 00413 qWarning("QBuffer::writeData: Memory allocation error"); 00414 return -1; 00415 } 00416 } 00417 00418 memcpy(d->buf->data() + d->ioIndex, (uchar *)data, int(len)); 00419 d->ioIndex += int(len); 00420 00421 #ifndef QT_NO_QOBJECT 00422 d->writtenSinceLastEmit += len; 00423 if (!d->signalsEmitted) { 00424 d->signalsEmitted = true; 00425 QMetaObject::invokeMethod(this, "_q_emitSignals", Qt::QueuedConnection); 00426 } 00427 #endif 00428 return len; 00429 }
Here is the call graph for this function:

1.5.1