CopperSpice API  1.7.2
QBuffer Class Reference

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

Inheritance diagram for QBuffer:
QIODevice QObject

Public Methods

 QBuffer (QByteArray *buffer, QObject *parent=nullptr)
 
 QBuffer (QObject *parent=nullptr)
 
 ~QBuffer ()
 
bool atEnd () const override
 
QByteArraybuffer ()
 
const QByteArraybuffer () const
 
bool canReadLine () const override
 
void close () override
 
const QByteArraydata () const
 
bool open (OpenMode openMode) override
 
qint64 pos () const override
 
bool seek (qint64 off) override
 
void setBuffer (QByteArray *buffer)
 
void setData (const char *data, int size)
 
void setData (const QByteArray &buffer)
 
qint64 size () const override
 
- Public Methods inherited from QIODevice
 QIODevice ()
 
 QIODevice (QObject *parent)
 
virtual ~QIODevice ()
 
virtual qint64 bytesAvailable () const
 
virtual qint64 bytesToWrite () const
 
QString errorString () const
 
bool getChar (char *c)
 
bool isOpen () const
 
bool isReadable () const
 
virtual bool isSequential () const
 
bool isTextModeEnabled () const
 
bool isWritable () const
 
OpenMode openMode () const
 
qint64 peek (char *data, qint64 maxSize)
 
QByteArray peek (qint64 maxSize)
 
bool putChar (char c)
 
qint64 read (char *data, qint64 maxSize)
 
QByteArray read (qint64 maxSize)
 
QByteArray readAll ()
 
qint64 readLine (char *data, qint64 maxSize)
 
QByteArray readLine (qint64 maxSize=0)
 
virtual bool reset ()
 
void setTextModeEnabled (bool enabled)
 
void ungetChar (char c)
 
virtual bool waitForBytesWritten (int msecs)
 
virtual bool waitForReadyRead (int msecs)
 
qint64 write (const char *data)
 
qint64 write (const char *data, qint64 maxSize)
 
qint64 write (const QByteArray &data)
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &objName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Protected Methods

void connectNotify (const QMetaMethod &signalMethod) const override
 
void disconnectNotify (const QMetaMethod &signalMethod) const override
 
qint64 readData (char *data, qint64 maxSize) override
 
qint64 writeData (const char *data, qint64 size) override
 
- Protected Methods inherited from QIODevice
virtual qint64 readLineData (char *data, qint64 maxSize)
 
void setErrorString (const QString &errorString)
 
void setOpenMode (OpenMode openMode)
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Additional Inherited Members

- Public Typedefs inherited from QIODevice
using OpenMode = QFlags< OpenModeFlag >
 
- Public Types inherited from QIODevice
enum  OpenModeFlag
 
- Public Signals inherited from QIODevice
void aboutToClose ()
 
void bytesWritten (qint64 bytes)
 
void readChannelFinished ()
 
void readyRead ()
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 
- Public Slots inherited from QObject
void deleteLater ()
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QBuffer class provides a QIODevice interface for a QByteArray.

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.open(QBuffer::ReadWrite);
buffer.write("CopperSpice is cool");
buffer.seek(0);
buffer.getChar(&ch); // ch == 'C'
buffer.getChar(&ch); // ch == 'o'
buffer.getChar(&ch); // ch == 'p'
buffer.getChar(&ch); // ch == 'p'
buffer.getChar(&ch); // ch == 'e'
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);
buffer.open(QIODevice::WriteOnly);
QDataStream out(&buffer);

Effectively, we convert the application's QPalette into a byte array. This is an example of how to read the data from the QByteArray.

QPalette palette;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::ReadOnly);
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

Constructor & Destructor Documentation

QBuffer::QBuffer ( QObject parent = nullptr)
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()
QBuffer::QBuffer ( QByteArray buffer,
QObject parent = nullptr 
)

Constructs a QBuffer that uses the QByteArray pointed to by buffer as its internal buffer, and with the given parent. The caller is responsible for ensuring that buffer remains valid until the QBuffer is destroyed, or until setBuffer() is called to change the buffer. QBuffer does not take ownership of the QByteArray.

If you open the buffer in write-only mode or read-write mode and write something into the QBuffer, buffer will be modified.

QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
buffer.seek(3);
buffer.write("def", 3);
buffer.close(); // data == "abcdef"
See also
open(), setBuffer(), setData()
QBuffer::~QBuffer ( )

Destroys the buffer.

Method Documentation

bool QBuffer::atEnd ( ) const
overridevirtual

Returns true if the current read and write position is at the end of the device (i.e. there is no more data available for reading on the device), otherwise returns false.

For some devices, atEnd() can return true even though there is more data to read. This special case only applies to devices that generate data in direct response to you calling read() (e.g., /dev or /proc files on Unix and Mac OS X, or console input / stdin on all platforms).

See also
bytesAvailable(), read(), isSequential()

Reimplemented from QIODevice.

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()
const QByteArray & QBuffer::buffer ( ) const

Equivalent to calling data().

bool QBuffer::canReadLine ( ) const
overridevirtual

Returns true if a complete line of data can be read from the device, otherwise returns false. Unbuffered devices, which have no way of determining what can be read, always return false. This method is often called in conjunction with the readyRead() signal.

Subclasses that reimplement this method must call the base implementation in order to include the contents of the QIODevice's buffer.

bool CustomDevice::canReadLine() const {
return buffer.contains('\n') || QIODevice::canReadLine();
}
See also
readyRead(), readLine()

Reimplemented from QIODevice.

void QBuffer::close ( )
overridevirtual

First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen. The error string is also reset.

See also
setOpenMode(), OpenMode

Reimplemented from QIODevice.

void QBuffer::connectNotify ( const QMetaMethod signalMethod) const
overrideprotectedvirtual

This method is called when a connection has been made to signalMethod in this object.

See also
connect(), disconnectNotify()

Reimplemented from QObject.

const QByteArray & QBuffer::data ( ) const

Returns the data contained in the buffer. Equivalent to calling buffer().

See also
setData(), setBuffer()
void QBuffer::disconnectNotify ( const QMetaMethod signalMethod) const
overrideprotectedvirtual

This method is called when a slot is disconnected from signalMethod in this object.

See also
disconnect(), connectNotify()

Reimplemented from QObject.

bool QBuffer::open ( OpenMode  mode)
overridevirtual

Opens the device and sets its OpenMode to mode. Returns true if successful, otherwise returns false. This method should be called from any existing reimplementation of open() or other functions that open the device.

See also
openMode(), OpenMode

Reimplemented from QIODevice.

qint64 QBuffer::pos ( ) const
overridevirtual

For random-access devices, this method returns the position that data is written to or read from. For sequential devices or closed devices, where there is no concept of a "current position", 0 is returned.

The current read/write position of the device is maintained internally by QIODevice, so reimplementing this method is not necessary. When subclassing QIODevice, use QIODevice::seek() to notify QIODevice about changes in the device position.

See also
isSequential(), seek()

Reimplemented from QIODevice.

qint64 QBuffer::readData ( char *  data,
qint64  maxSize 
)
overrideprotectedvirtual

Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an error occurred. If there are no bytes to be read and there can never be more bytes available (examples include socket closed, pipe closed, sub-process finished), returns -1.

This method is called by QIODevice. Reimplement this method when creating a subclass of QIODevice.

When reimplementing it is important this method reads all the required data before returning. This is required in order for QDataStream to be able to operate on the class. QDataStream assumes all the requested information was read and therefore does not retry reading if there was a problem.

See also
read(), readLine(), writeData()

Implements QIODevice.

bool QBuffer::seek ( qint64  pos)
overridevirtual

For random-access devices, this method sets the current position to pos, returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.

When subclassing QIODevice, you must call QIODevice::seek() at the start of your function to ensure integrity with QIODevice's built-in buffer. The base implementation always returns true.

See also
pos(), isSequential()

Reimplemented from QIODevice.

void QBuffer::setBuffer ( QByteArray buffer)

Makes QBuffer uses the QByteArray pointed to by buffer 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 does not 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 buffer will be modified.

QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
buffer.seek(3);
buffer.write("def", 3);
buffer.close(); // data == "abcdef"

If buffer is a nullptr the QBuffer creates its own internal QByteArray. This byte array is initially empty.

See also
buffer(), setData(), open()
void QBuffer::setData ( const char *  data,
int  size 
)
inline

Sets the contents of the internal buffer to be the first size bytes of data.

void QBuffer::setData ( const QByteArray buffer)

Sets the contents of the internal buffer to be buffer. This is the same as assigning data to buffer(). This method does nothing if isOpen() is true.

See also
data(), setBuffer()
qint64 QBuffer::size ( ) const
overridevirtual

For open random-access devices, this method returns the size of the device. For open sequential devices, bytesAvailable() is returned.If the device is closed, the size returned will not reflect the actual size of the device.

See also
isSequential(), pos()

Reimplemented from QIODevice.

qint64 QBuffer::writeData ( const char *  data,
qint64  maxSize 
)
overrideprotectedvirtual

Writes up to maxSize bytes from data to the device. Returns the number of bytes written, or -1 if an error occurred.

This method is called by QIODevice. Reimplement when creating a subclass of QIODevice. When reimplementing it is important that this method writes all the data available before returning. This is required in order for QDataStream to be able to operate on the class. QDataStream assumes all the information was written and therefore does not retry writing if there was a problem.

See also
read(), write()

Implements QIODevice.