Qt 4.8
Public Functions | Protected Functions | List of all members
QBuffer Class Reference

The QBuffer class provides a QIODevice interface for a QByteArray. More...

#include <qbuffer.h>

Inheritance diagram for QBuffer:
QIODevice QObject

Public Functions

bool atEnd () const
 Reimplemented Function More...
 
QByteArraybuffer ()
 Returns a reference to the QBuffer's internal buffer. More...
 
const QByteArraybuffer () const
 This is the same as data(). More...
 
bool canReadLine () const
 Reimplemented Function More...
 
void close ()
 Reimplemented Function More...
 
const QByteArraydata () const
 Returns the data contained in the buffer. More...
 
bool open (OpenMode openMode)
 Reimplemented Function More...
 
qint64 pos () const
 Reimplemented Function More...
 
 QBuffer (QObject *parent=0)
 Constructs an empty buffer with the given parent. More...
 
 QBuffer (QByteArray *buf, QObject *parent=0)
 Constructs a QBuffer that uses the QByteArray pointed to by byteArray as its internal buffer, and with the given parent. More...
 
bool seek (qint64 off)
 Reimplemented Function More...
 
void setBuffer (QByteArray *a)
 Makes QBuffer uses the QByteArray pointed to by byteArray as its internal buffer. More...
 
void setData (const QByteArray &data)
 Sets the contents of the internal buffer to be data. More...
 
void setData (const char *data, int len)
 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. More...
 
qint64 size () const
 Reimplemented Function More...
 
 ~QBuffer ()
 Destroys the buffer. More...
 
- Public Functions inherited from QIODevice
virtual qint64 bytesAvailable () const
 Returns the number of bytes that are available for reading. More...
 
virtual qint64 bytesToWrite () const
 For buffered devices, this function returns the number of bytes waiting to be written. More...
 
QString errorString () const
 Returns a human-readable description of the last device error that occurred. More...
 
bool getChar (char *c)
 Reads one character from the device and stores it in c. More...
 
bool isOpen () const
 Returns true if the device is open; otherwise returns false. More...
 
bool isReadable () const
 Returns true if data can be read from the device; otherwise returns false. More...
 
virtual bool isSequential () const
 Returns true if this device is sequential; otherwise returns false. More...
 
bool isTextModeEnabled () const
 Returns true if the Text flag is enabled; otherwise returns false. More...
 
bool isWritable () const
 Returns true if data can be written to the device; otherwise returns false. More...
 
OpenMode openMode () const
 Returns the mode in which the device has been opened; i.e. More...
 
qint64 peek (char *data, qint64 maxlen)
 Reads at most maxSize bytes from the device into data, without side effects (i. More...
 
QByteArray peek (qint64 maxlen)
 Peeks at most maxSize bytes from the device, returning the data peeked as a QByteArray. More...
 
bool putChar (char c)
 Writes the character c to the device. More...
 
 QIODevice ()
 Constructs a QIODevice object. More...
 
 QIODevice (QObject *parent)
 Constructs a QIODevice object with the given parent. More...
 
qint64 read (char *data, qint64 maxlen)
 Reads at most maxSize bytes from the device into data, and returns the number of bytes read. More...
 
QByteArray read (qint64 maxlen)
 Reads at most maxSize bytes from the device, and returns the data read as a QByteArray. More...
 
QByteArray readAll ()
 Reads all available data from the device, and returns it as a QByteArray. More...
 
qint64 readLine (char *data, qint64 maxlen)
 This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read. More...
 
QByteArray readLine (qint64 maxlen=0)
 Reads a line from the device, but no more than maxSize characters, and returns the result as a QByteArray. More...
 
virtual bool reset ()
 Seeks to the start of input for random-access devices. More...
 
void setTextModeEnabled (bool enabled)
 If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is removed. More...
 
void ungetChar (char c)
 Puts the character c back into the device, and decrements the current position unless the position is 0. More...
 
virtual bool waitForBytesWritten (int msecs)
 For buffered devices, this function waits until a payload of buffered written data has been written to the device and the bytesWritten() signal has been emitted, or until msecs milliseconds have passed. More...
 
virtual bool waitForReadyRead (int msecs)
 Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. More...
 
qint64 write (const char *data, qint64 len)
 Writes at most maxSize bytes of data from data to the device. More...
 
qint64 write (const char *data)
 Writes data from a zero-terminated string of 8-bit characters to the device. More...
 
qint64 write (const QByteArray &data)
 Writes the content of byteArray to the device. More...
 
virtual ~QIODevice ()
 The destructor is virtual, and QIODevice is an abstract base class. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Protected Functions

void connectNotify (const char *)
 Reimplemented Function More...
 
void disconnectNotify (const char *)
 Reimplemented Function More...
 
qint64 readData (char *data, qint64 maxlen)
 Reimplemented Function More...
 
qint64 writeData (const char *data, qint64 len)
 Reimplemented Function More...
 
- Protected Functions inherited from QIODevice
 QIODevice (QIODevicePrivate &dd, QObject *parent=0)
 
virtual qint64 readLineData (char *data, qint64 maxlen)
 Reads up to maxSize characters into data and returns the number of characters read. More...
 
void setErrorString (const QString &errorString)
 Sets the human readable description of the last device error that occurred to str. More...
 
void setOpenMode (OpenMode openMode)
 Sets the OpenMode of the device to openMode. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 

Additional Inherited Members

- Public Types inherited from QIODevice
enum  OpenModeFlag {
  NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002, ReadWrite = ReadOnly | WriteOnly,
  Append = 0x0004, Truncate = 0x0008, Text = 0x0010, Unbuffered = 0x0020
}
 This enum is used with open() to describe the mode in which a device is opened. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QIODevice
void aboutToClose ()
 This signal is emitted when the device is about to close. More...
 
void bytesWritten (qint64 bytes)
 This signal is emitted every time a payload of data has been written to the device. More...
 
void readChannelFinished ()
 This signal is emitted when the input (reading) stream is closed in this device. More...
 
void readyRead ()
 This signal is emitted once every time new data is available for reading from the device. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QBuffer class provides a QIODevice interface for a QByteArray.

Note
This class or function is reentrant.

QBuffer allows you to access a QByteArray using the QIODevice interface. The QByteArray is treated just as a standard random-accessed file. Example:

char ch;
buffer.write("Qt rocks!");
buffer.seek(0);
buffer.getChar(&ch); // ch == 'Q'
buffer.getChar(&ch); // ch == 't'
buffer.getChar(&ch); // ch == ' '
buffer.getChar(&ch); // ch == 'r'

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:

QByteArray byteArray;
QBuffer buffer(&byteArray);
QDataStream out(&buffer);

Effectively, we convert the application's QPalette into a byte array. Here's how to read the data from the QByteArray:

QPalette palette;
QBuffer buffer(&byteArray);
QDataStream in(&buffer);
in >> palette;

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.

See also
QFile, QDataStream, QTextStream, QByteArray

Definition at line 57 of file qbuffer.h.

Constructors and Destructors

◆ QBuffer() [1/2]

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().

See also
open()

Definition at line 183 of file qbuffer.cpp.

Referenced by QBufferPrivate::peek().

184  : QIODevice(*new QBufferPrivate, parent)
185 {
186  Q_D(QBuffer);
187  d->buf = &d->defaultBuf;
188  d->ioIndex = 0;
189 }
double d
Definition: qnumeric_p.h:62
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
QIODevice()
Constructs a QIODevice object.
Definition: qiodevice.cpp:390

◆ QBuffer() [2/2]

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:

QByteArray byteArray("abc");
QBuffer buffer(&byteArray);
buffer.seek(3);
buffer.write("def", 3);
buffer.close();
// byteArray == "abcdef"
See also
open(), setBuffer(), setData()

Definition at line 208 of file qbuffer.cpp.

209  : QIODevice(*new QBufferPrivate, parent)
210 {
211  Q_D(QBuffer);
212  d->buf = byteArray ? byteArray : &d->defaultBuf;
213  d->defaultBuf.clear();
214  d->ioIndex = 0;
215 }
double d
Definition: qnumeric_p.h:62
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
QIODevice()
Constructs a QIODevice object.
Definition: qiodevice.cpp:390

◆ ~QBuffer()

QBuffer::~QBuffer ( )

Destroys the buffer.

Definition at line 222 of file qbuffer.cpp.

223 {
224 }

Functions

◆ atEnd()

bool QBuffer::atEnd ( ) const
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 408 of file qbuffer.cpp.

Referenced by QAxServerBase::Load().

409 {
410  return QIODevice::atEnd();
411 }
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
Definition: qiodevice.cpp:711

◆ buffer() [1/2]

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.

See also
setBuffer(), data()

Definition at line 271 of file qbuffer.cpp.

Referenced by QPicturePrivate::checkFormat(), QTestLiteStaticInfoPrivate::initializeSupportedAtoms(), QXlibMime::mimeConvertToFormat(), QNonContiguousByteDeviceBufferImpl::QNonContiguousByteDeviceBufferImpl(), qt_get_net_supported(), qt_get_net_virtual_roots(), QAxServerBase::Save(), and QX11Data::xdndMimeConvertToFormat().

272 {
273  Q_D(QBuffer);
274  return *d->buf;
275 }
double d
Definition: qnumeric_p.h:62
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482

◆ buffer() [2/2]

const QByteArray & QBuffer::buffer ( ) const

This is the same as data().

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 286 of file qbuffer.cpp.

287 {
288  Q_D(const QBuffer);
289  return *d->buf;
290 }
double d
Definition: qnumeric_p.h:62
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482

◆ canReadLine()

bool QBuffer::canReadLine ( ) const
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 416 of file qbuffer.cpp.

417 {
418  Q_D(const QBuffer);
419  if (!isOpen())
420  return false;
421 
422  return d->buf->indexOf('\n', int(pos())) != -1 || QIODevice::canReadLine();
423 }
double d
Definition: qnumeric_p.h:62
qint64 pos() const
Reimplemented Function
Definition: qbuffer.cpp:367
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
Definition: qiodevice.cpp:1330

◆ close()

void QBuffer::close ( )
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 359 of file qbuffer.cpp.

Referenced by QPicturePrivate::checkFormat(), QPacket::clear(), QSvgPaintEngine::drawImage(), QXmlQuery::evaluateTo(), QAxServerBase::Load(), QFontEngineQPF::QFontEngineQPF(), QCacheItem::reset(), QAxServerBase::Save(), and QTextEditMimeData::setup().

360 {
362 }
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590

◆ connectNotify()

void QBuffer::connectNotify ( const char *  signal)
protectedvirtual

Reimplemented Function

Warning
This function is not part of the public interface.

Reimplemented from QObject.

Definition at line 472 of file qbuffer.cpp.

473 {
474  if (strcmp(signal + 1, "readyRead()") == 0 || strcmp(signal + 1, "bytesWritten(qint64)") == 0)
475  d_func()->signalConnectionCount++;
476 }

◆ data()

const QByteArray & QBuffer::data ( ) const

Returns the data contained in the buffer.

This is the same as buffer().

See also
setData(), setBuffer()

Definition at line 301 of file qbuffer.cpp.

Referenced by QXmlQuery::evaluateTo(), QSvgIOHandlerPrivate::load(), QFontEngineQPF::QFontEngineQPF(), QPicturePrivate::QPicturePrivate(), qt_fill_input_buffer(), QWSServer::sendIMEvent(), setData(), QTextEditMimeData::setup(), and QTextOdfWriter::writeInlineCharacter().

302 {
303  Q_D(const QBuffer);
304  return *d->buf;
305 }
double d
Definition: qnumeric_p.h:62
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482

◆ disconnectNotify()

void QBuffer::disconnectNotify ( const char *  signal)
protectedvirtual

Reimplemented Function

Warning
This function is not part of the public interface.

Reimplemented from QObject.

Definition at line 482 of file qbuffer.cpp.

483 {
484  if (!signal || strcmp(signal + 1, "readyRead()") == 0 || strcmp(signal + 1, "bytesWritten(qint64)") == 0)
485  d_func()->signalConnectionCount--;
486 }

◆ open()

bool QBuffer::open ( OpenMode  openMode)
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 338 of file qbuffer.cpp.

Referenced by RequestAnalyzer::analyze(), QPicturePrivate::checkFormat(), QPacket::clear(), QNetworkDiskCache::data(), QDeclarativeXmlQueryEngine::doQueryJob(), QDeclarativeXmlQueryEngine::doSubQueryJob(), QSvgPaintEngine::drawImage(), QMimeDataWrapper::encodedData(), QXmlQuery::evaluateTo(), QRasterPixmapData::fromData(), QVGPixmapData::fromData(), QPixmapData::fromData(), QGLPixmapData::fromData(), QImage::fromData(), QTestLiteStaticInfoPrivate::initializeSupportedAtoms(), QXmlSchemaPrivate::load(), QAxServerBase::Load(), QXlibMime::mimeConvertToFormat(), QDeclarativePixmapReader::networkRequestDone(), QNetworkAccessManager::post(), QNetworkDiskCache::prepare(), QNetworkAccessManager::put(), QDataStream::QDataStream(), QFontEngineQPF::QFontEngineQPF(), QPacket::QPacket(), QPicturePrivate::QPicturePrivate(), qt_get_net_supported(), qt_get_net_virtual_roots(), QTextStream::QTextStream(), QAxServerBase::Save(), QWSServer::sendIMEvent(), QXmlQuery::setFocus(), QMovie::supportedFormats(), QXmlSchemaValidator::validate(), ICOReader::write(), and QX11Data::xdndMimeConvertToFormat().

339 {
340  Q_D(QBuffer);
341 
342  if ((flags & (Append | Truncate)) != 0)
343  flags |= WriteOnly;
344  if ((flags & (ReadOnly | WriteOnly)) == 0) {
345  qWarning("QBuffer::open: Buffer access not specified");
346  return false;
347  }
348 
349  if ((flags & Truncate) == Truncate)
350  d->buf->resize(0);
351  d->ioIndex = (flags & Append) == Append ? d->buf->size() : 0;
352 
353  return QIODevice::open(flags);
354 }
double d
Definition: qnumeric_p.h:62
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT void qWarning(const char *,...)
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570

◆ pos()

qint64 QBuffer::pos ( ) const
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 367 of file qbuffer.cpp.

Referenced by canReadLine(), QSvgIOHandlerPrivate::load(), QNonContiguousByteDeviceBufferImpl::QNonContiguousByteDeviceBufferImpl(), QPicturePrivate::QPicturePrivate(), and qt_fill_input_buffer().

368 {
369  return QIODevice::pos();
370 }
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
Definition: qiodevice.cpp:624

◆ readData()

qint64 QBuffer::readData ( char *  data,
qint64  maxlen 
)
protectedvirtual

Reimplemented Function

Implements QIODevice.

Definition at line 428 of file qbuffer.cpp.

429 {
430  Q_D(QBuffer);
431  if ((len = qMin(len, qint64(d->buf->size()) - d->ioIndex)) <= 0)
432  return qint64(0);
433  memcpy(data, d->buf->constData() + d->ioIndex, len);
434  d->ioIndex += int(len);
435  return len;
436 }
double d
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
const QByteArray & data() const
Returns the data contained in the buffer.
Definition: qbuffer.cpp:301
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
__int64 qint64
Definition: qglobal.h:942

◆ seek()

bool QBuffer::seek ( qint64  off)
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 384 of file qbuffer.cpp.

Referenced by QDeclarativeXmlQueryEngine::doSubQueryJob(), QSvgIOHandlerPrivate::load(), and QPicturePrivate::QPicturePrivate().

385 {
386  Q_D(QBuffer);
387  if (pos > d->buf->size() && isWritable()) {
388  if (seek(d->buf->size())) {
389  const qint64 gapSize = pos - d->buf->size();
390  if (write(QByteArray(gapSize, 0)) != gapSize) {
391  qWarning("QBuffer::seek: Unable to fill gap");
392  return false;
393  }
394  } else {
395  return false;
396  }
397  } else if (pos > d->buf->size() || pos < 0) {
398  qWarning("QBuffer::seek: Invalid pos: %d", int(pos));
399  return false;
400  }
401  d->ioIndex = int(pos);
402  return QIODevice::seek(pos);
403 }
double d
Definition: qnumeric_p.h:62
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
Definition: qiodevice.cpp:558
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
qint64 pos() const
Reimplemented Function
Definition: qbuffer.cpp:367
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
bool seek(qint64 off)
Reimplemented Function
Definition: qbuffer.cpp:384
Q_CORE_EXPORT void qWarning(const char *,...)
__int64 qint64
Definition: qglobal.h:942
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
Definition: qiodevice.cpp:659

◆ setBuffer()

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:

QByteArray byteArray("abc");
buffer.setBuffer(&byteArray);
buffer.seek(3);
buffer.write("def", 3);
buffer.close();
// byteArray == "abcdef"

If byteArray is 0, the buffer creates its own internal QByteArray to work on. This byte array is initially empty.

See also
buffer(), setData(), open()

Definition at line 248 of file qbuffer.cpp.

Referenced by QPacket::clear().

249 {
250  Q_D(QBuffer);
251  if (isOpen()) {
252  qWarning("QBuffer::setBuffer: Buffer is open");
253  return;
254  }
255  if (byteArray) {
256  d->buf = byteArray;
257  } else {
258  d->buf = &d->defaultBuf;
259  }
260  d->defaultBuf.clear();
261  d->ioIndex = 0;
262 }
double d
Definition: qnumeric_p.h:62
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
Q_CORE_EXPORT void qWarning(const char *,...)

◆ setData() [1/2]

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.

See also
setBuffer()

Definition at line 315 of file qbuffer.cpp.

Referenced by QNetworkDiskCache::data(), QImage::fromData(), QNetworkAccessManager::post(), QNetworkAccessManager::put(), QDataStream::QDataStream(), QPicturePrivate::QPicturePrivate(), QTextStream::QTextStream(), QDomDocument::setContent(), and QXmlQuery::setFocus().

316 {
317  Q_D(QBuffer);
318  if (isOpen()) {
319  qWarning("QBuffer::setData: Buffer is open");
320  return;
321  }
322  *d->buf = data;
323  d->ioIndex = 0;
324 }
double d
Definition: qnumeric_p.h:62
const QByteArray & data() const
Returns the data contained in the buffer.
Definition: qbuffer.cpp:301
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
Q_CORE_EXPORT void qWarning(const char *,...)

◆ setData() [2/2]

void QBuffer::setData ( const char *  data,
int  len 
)
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 105 of file qbuffer.h.

106 { setData(QByteArray(adata, alen)); }
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
Definition: qbuffer.cpp:315
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135

◆ size()

qint64 QBuffer::size ( ) const
virtual

Reimplemented Function

Reimplemented from QIODevice.

Definition at line 375 of file qbuffer.cpp.

Referenced by QPicturePrivate::checkFormat(), QNonContiguousByteDeviceBufferImpl::QNonContiguousByteDeviceBufferImpl(), QPicturePrivate::QPicturePrivate(), QWSServer::sendIMEvent(), and QCacheItem::size().

376 {
377  Q_D(const QBuffer);
378  return qint64(d->buf->size());
379 }
double d
Definition: qnumeric_p.h:62
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
__int64 qint64
Definition: qglobal.h:942

◆ writeData()

qint64 QBuffer::writeData ( const char *  data,
qint64  len 
)
protectedvirtual

Reimplemented Function

Implements QIODevice.

Definition at line 441 of file qbuffer.cpp.

442 {
443  Q_D(QBuffer);
444  int extraBytes = d->ioIndex + len - d->buf->size();
445  if (extraBytes > 0) { // overflow
446  int newSize = d->buf->size() + extraBytes;
447  d->buf->resize(newSize);
448  if (d->buf->size() != newSize) { // could not resize
449  qWarning("QBuffer::writeData: Memory allocation error");
450  return -1;
451  }
452  }
453 
454  memcpy(d->buf->data() + d->ioIndex, (uchar *)data, int(len));
455  d->ioIndex += int(len);
456 
457 #ifndef QT_NO_QOBJECT
458  d->writtenSinceLastEmit += len;
459  if (d->signalConnectionCount && !d->signalsEmitted && !signalsBlocked()) {
460  d->signalsEmitted = true;
461  QMetaObject::invokeMethod(this, "_q_emitSignals", Qt::QueuedConnection);
462  }
463 #endif
464  return len;
465 }
double d
Definition: qnumeric_p.h:62
const QByteArray & data() const
Returns the data contained in the buffer.
Definition: qbuffer.cpp:301
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_D(Class)
Definition: qglobal.h:2482
unsigned char uchar
Definition: qglobal.h:994
Q_CORE_EXPORT void qWarning(const char *,...)
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
bool signalsBlocked() const
Returns true if signals are blocked; otherwise returns false.
Definition: qobject.h:148

The documentation for this class was generated from the following files: