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

# QBuffer Class Reference

The QBuffer class is an I/O device that operates on a QByteArray. More...

    #include <QBuffer>

Inherits QIODevice.

Note: All the functions in this class are reentrant.

- List of all members, including inherited members

### Public Functions

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

- 36 public functions inherited from QIODevice

### Protected Functions

- 2 protected functions inherited from QIODevice

## Detailed Description

The QBuffer class is an I/O device that operates on a QByteArray.

QBuffer is used to read and write to a memory buffer. It is normally used with a QTextStream or a QDataStream. QBuffer has an associated QByteArray which holds the buffer data. The buffer is automatically resized as data is written.

The constructor `QBuffer(QByteArray)` creates a QBuffer using an existing byte array. The byte array can also be set with setBuffer(). Writing to the QBuffer will modify the original byte array because QByteArray is explicitly shared.

Use open() to open the buffer before use and to set the mode (read-only, write-only, etc.). close() closes the buffer. The buffer must be closed before reopening or calling setBuffer().

A common way to use QBuffer is through QDataStream or QTextStream, which have constructors that take a QBuffer parameter. For convenience, there are also QDataStream and QTextStream constructors that take a QByteArray parameter. These constructors operate on an internal QBuffer.

Note that QTextStream can also operate on a QString (a Unicode string), but a QBuffer cannot.

You can also use QBuffer directly through the standard QIODevice functions readBlock(), writeBlock() readLine(), at(), getch(), putch() and ungetch().

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

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.

### 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.