Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QBuffer Class Reference

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

    #include <QBuffer>

Inherits QIODevice.

Note: All the functions in this class are reentrant.

- List of all members, including inherited members

### Writable Properties

- 1 writable property inherited from QObject

### Public Functions

- QBuffer ()
- QBuffer ( QByteArray * a )
- QBuffer ( QObject * parent )
- QBuffer ( QByteArray * a, QObject * parent )
- ~QBuffer ()
- QByteArray & buffer ()
- const QByteArray & buffer () const
- const QByteArray & data () const
- void setBuffer ( QByteArray * a )
- void setData ( const QByteArray & data )
- void setData ( const char * data, int len )

- 29 public functions inherited from QIODevice
- 26 public functions inherited from QObject

### Public Slots

- 1 public slot inherited from QObject

### Signals

- 2 signals inherited from QIODevice
- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QObject

### Protected Functions

- 4 protected functions inherited from QIODevice
- 7 protected functions inherited from QObject

## Detailed Description

The QBuffer class provides a QIODevice interface for a QByteArray.

QBuffer is a wrapper for QByteArray which allows you to access an array using the QIODevice interface. The QByteArray is treated as a FIFO queue (First In, First Out), so the first data you write to the buffer is the first data you will read. Example:

        QBuffer buffer;

        if (buffer.open(QBuffer::ReadWrite)) {
            buffer.write("Qt rocks!");
            char c;
            buffer.getChar(&c); // 'Q'
            buffer.getChar(&c); // 't'
            buffer.getChar(&c); // ' '
            buffer.getChar(&c); // 'r'
        }

By default, an internal QByteArray buffer is created for you when you construct 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().

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, your slot connected to readyRead() will be called. QBuffer also emits bytesWritten() every time new data has been written to the buffer.

QBuffer can be used with QTextStream and QDataStream's stream operators (operator<<() and operator>>()).

See also QFile, QDataStream, QTextStream, QByteArray, and Shared Classes.

## Member Function Documentation

### QBuffer::QBuffer ()

Constructs an empty buffer.

### QBuffer::QBuffer ( QByteArray * a )

Constructs a buffer that operates on QByteArray a.

If you open the buffer in write mode (`QIODevice::WriteOnly` or `QIODevice::ReadWrite`) and write something into the buffer, the byte array, a will be modified.

Example:

        QCString str = "abc";
        QBuffer b(str);
        b.open(QIODevice::WriteOnly);
        b.at(3); // position at the 4th character (the terminating \0)
        b.writeBlock("def", 4); // write "def" including the terminating \0
        b.close();
        // Now, str == "abcdef" with a terminating \0

See also setBuffer().

### QBuffer::QBuffer ( QObject * parent )

Constructs an empty buffer with the parent parent.

### QBuffer::QBuffer ( QByteArray * a, QObject * parent )

Constructs a buffer that operates on QByteArray a, with the parent parent.

### QBuffer::~QBuffer ()

Destroys the buffer.

### QByteArray & QBuffer::buffer ()

Returns this buffer's byte array.

See also setBuffer().

### const QByteArray & QBuffer::buffer () const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns this buffer's byte array.

See also setBuffer().

### const QByteArray & QBuffer::data () const

Returns this buffer's byte array.

See also setData().

### void QBuffer::setBuffer ( QByteArray * a )

Replaces the buffer's contents with a.

Does nothing if isOpen() is true.

Note that if you open the buffer in write mode (`QIODevice::WriteOnly` or QIODevice::ReadWrite) and write something into the buffer, and a is not 0, a is modified because QByteArray is an explicitly shared class.

If a is 0, the buffer creates its own (initially empty) internal QByteArray to work on.

See also buffer(), open(), and close().

### void QBuffer::setData ( const QByteArray & data )

Sets the byte array for the buffer to be data.

Does nothing if isOpen() is true. Since data is const the buffer can only be used to read from it.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the data to be the first len bytes of the data string.