6.0.0-rc4Source

QBuffer Class

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

Header#include <QBuffer>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQIODevice

List of all members, including inherited members

Public Functions

QBuffer(QObject *parent = nullptr)
QBuffer(QByteArray *byteArray, QObject *parent = nullptr)
~QBuffer()
QByteArray &buffer()
const QByteArray &buffer() const
const QByteArray &data() const
voidsetBuffer(QByteArray *a)
voidsetData(const QByteArray &data)
voidsetData(const char *data, qint64 len)

Reimplemented Public Functions

boolopen(OpenMode mode)
voidclose()
qint64pos() const
qint64size() const
boolseek(qint64 off)
boolatEnd() const
qint64bytesAvailable() const
boolcanReadLine() const

Reimplemented Protected Functions

qint64readData(char *data, qint64 maxSize)
qint64writeData(const char *data, qint64 maxSize)

Detailed Description

QBuffer lets code that works with a QIODevice read from and write to memory. By default the buffer owns an internal QByteArray; pass a pointer to the constructor or to setBuffer() to operate on a byte array owned by the caller instead, which must outlive the buffer.

QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
buffer.write("Hello");
buffer.close();
const QByteArray data = buffer.data();

Writing past the end of the buffer grows the byte array. A buffer is a random-access device.

See also QIODevice, QByteArray and QFile.

Differences from Qt

The Text open flag is cleared on open, and open() always succeeds. seek() fails for positions past the end of the buffer instead of growing it. bytesWritten() and readyRead() are never emitted. The length argument of setData(const char *, qint64) is a qint64.

Member Function Documentation

[explicit] QBuffer::QBuffer(QObject *parent = nullptr)

Constructs a buffer that uses an internal QByteArray, with the given parent. The buffer is not open.

See also open() and setBuffer().

[explicit] QBuffer::QBuffer(QByteArray *byteArray, QObject *parent = nullptr)

Constructs a buffer that uses the QByteArray pointed to by byteArray, with the given parent. The caller keeps ownership of the byte array, which must outlive the buffer. If byteArray is nullptr, the buffer uses its internal byte array.

See also setBuffer() and buffer().

[override] QBuffer::~QBuffer()

Destroys the buffer.

[override] bool QBuffer::atEnd() const

Reimplements QIODevice::atEnd().

Returns true if the position is at or past the end of the data.

QByteArray & QBuffer::buffer()

Returns a reference to the buffer's byte array, which can be modified.

See also data() and setBuffer().

const QByteArray & QBuffer::buffer() const

This is an overloaded function.

Returns a const reference to the buffer's byte array.

[override] qint64 QBuffer::bytesAvailable() const

Reimplements QIODevice::bytesAvailable().

Returns the number of bytes between the current position and the end of the data, plus any buffered look-ahead data.

[override] bool QBuffer::canReadLine() const

Reimplements QIODevice::canReadLine().

Returns true if there is a newline between the current position and the end of the data; otherwise returns false.

[override] void QBuffer::close()

Reimplements QIODevice::close().

Closes the buffer and resets the position to 0. The data is kept.

const QByteArray & QBuffer::data() const

Returns the data contained in the buffer.

See also setData() and buffer().

[override] bool QBuffer::open(OpenMode mode)

Reimplements QIODevice::open().

Opens the buffer in the given mode and returns true. The Text flag is ignored. If mode includes both Truncate and WriteOnly, the byte array is cleared first. With Append the position starts at the end of the data; otherwise at the start.

See also close().

[override] qint64 QBuffer::pos() const

Reimplements QIODevice::pos().

Returns the current position in the data.

[override, protected] qint64 QBuffer::readData(char *data, qint64 maxSize)

Reimplements QIODevice::readData().

Copies up to maxSize bytes from the byte array into data, starting at the current position. Returns the number of bytes copied, which is 0 at the end of the data.

[override] bool QBuffer::seek(qint64 off)

Reimplements QIODevice::seek().

Sets the current position to off. Returns true on success. Returns false if off is negative or past the end of the data.

void QBuffer::setBuffer(QByteArray *a)

Makes the buffer use the QByteArray pointed to by a, which the caller keeps ownership of. If a is nullptr, the buffer uses its internal byte array. Does nothing if the buffer is open.

See also buffer() and setData().

void QBuffer::setData(const QByteArray &data)

Sets the contents of the buffer to data. Does nothing if the buffer is open.

See also data() and setBuffer().

void QBuffer::setData(const char *data, qint64 len)

This is an overloaded function.

Sets the contents of the buffer to the first len bytes of data. Does nothing if the buffer is open.

[override] qint64 QBuffer::size() const

Reimplements QIODevice::size().

Returns the size of the data in bytes.

[override, protected] qint64 QBuffer::writeData(const char *data, qint64 maxSize)

Reimplements QIODevice::writeData().

Writes maxSize bytes from data into the byte array at the current position, growing the array if needed, and returns maxSize. In append mode, the data is always written at the end.