Qt Toolkit - QBuffer Class

# QBuffer Class Reference

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

`#include <qbuffer.h>`

Inherits QIODevice.

List of all member functions.

## Public Members

- QBuffer ()
- QBuffer ( QByteArray )
- ~QBuffer ()
- QByteArray buffer () const
- bool setBuffer ( QByteArray )
- virtual bool open ( int )
- virtual void close ()
- virtual void flush ()
- virtual uint size () const
- virtual int at () const
- virtual bool at ( int )
- virtual int readBlock ( char * p, uint )
- virtual int writeBlock ( const char * p, uint )
- virtual int readLine ( char * p, uint )
- virtual int getch ()
- virtual int putch ( int )
- virtual int ungetch ( int )

## Detailed Description

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

QBuffer is an I/O device for reading and writing a memory buffer. A QBuffer may be used directly (readBlock() and writeBlock()) or more conveniently via QDataStream or QTextStream. Most of its behavior is inherited from QIODevice.

A QBuffer has an associated QByteArray which holds the buffer data. Writing data at the end (i.e. size()) of the buffer expands the byte array.

For convenience, the byte stream classes QDataStream and QTextStream can operate on a QByteArray (or a QString) via an internal QBuffer:

        QString str;
        QTextStream ts( str, IO_WriteOnly );
        ts << "pi = " << 3.14;                      // str == "pi = 3.14"

See also: QFile, QDataStream and QTextStream.

Examples: grapher/grapher.cpp

## Member Function Documentation

### QBuffer::QBuffer ()

Constructs an empty buffer.

### QBuffer::QBuffer ( QByteArray buf )

Constructs a buffer and sets the buffer contents to buf.

See also: setBuffer().

### QBuffer::~QBuffer ()

Destroys the buffer.

### bool QBuffer::at ( int pos ) `[virtual]`

Sets the buffer index to pos. Returns TRUE if successful, otherwise FALSE.

See also: size().

Reimplemented from QIODevice.

### int QBuffer::at () const `[virtual]`

Returns the buffer index.

See also: size().

Reimplemented from QIODevice.

### QByteArray QBuffer::buffer () const

Returns the buffer most recently set by setBuffer(), or at construction.

### void QBuffer::close () `[virtual]`

Closes an open buffer.

See also: open().

Examples: grapher/grapher.cpp

Reimplemented from QIODevice.

### void QBuffer::flush () `[virtual]`

The flush function does nothing.

Reimplemented from QIODevice.

### int QBuffer::getch () `[virtual]`

Reads a single byte/character from the buffer.

Returns the byte/character read, or -1 if the end of the buffer has been reached.

See also: putch() and ungetch().

Reimplemented from QIODevice.

### bool QBuffer::open ( int m ) `[virtual]`

Opens the file specified by the file name currently set, using the mode m. Returns TRUE if successful, otherwise FALSE.

The mode parameter m must be a combination of the following flags.

- `IO_ReadOnly` opens a buffer in read-only mode.
- `IO_WriteOnly` opens a buffer in write-only mode.
- `IO_ReadWrite` opens a buffer in read/write mode.
- `IO_Append` sets the buffer index to the end of the buffer.
- `IO_Truncate` truncates the buffer.

See also: close() and isOpen().

Examples: grapher/grapher.cpp

Reimplemented from QIODevice.

### int QBuffer::putch ( int ch ) `[virtual]`

Writes the character ch into the buffer, overwriting the character at the current index, extending the buffer if necessary.

Returns ch, or -1 if some error occurred.

See also: getch() and ungetch().

Examples: grapher/grapher.cpp

Reimplemented from QIODevice.

### int QBuffer::readBlock ( char * p, uint len ) `[virtual]`

Reads at most len bytes from the buffer into p and returns the number of bytes actually read.

Returns -1 if a serious error occurred.

See also: writeBlock().

Reimplemented from QIODevice.

### int QBuffer::readLine ( char * p, uint maxlen ) `[virtual]`

Reads a line of text.

Reads bytes from the buffer until end-of-line is reached, or up to maxlen bytes.

See also: readBlock().

Reimplemented from QIODevice.

### bool QBuffer::setBuffer ( QByteArray buf )

Replaces the buffer's contents with buf.

This may not be done while the buffer is open.

Note that if you open the buffer in write mode (`IO_WriteOnly` or IO_ReadWrite) and write something into the buffer, buf is also modified because QByteArray is an explicitly shared class.

Example:

        QString str = "abc";
        QBuffer b( str );
        b.open( IO_WriteOnly );
        b.at( 3 );                                  // position at \0
        b.writeBlock( "def", 4 );                   // write including \0
        b.close();
          // Now, str == "abcdef"

See also: open and Shared Classes

### uint QBuffer::size () const `[virtual]`

Returns the number of bytes in the buffer.

See also: at().

Reimplemented from QIODevice.

### int QBuffer::ungetch ( int ch ) `[virtual]`

Puts the character ch back into the buffer and decrements the index if it is not zero.

This function is normally called to "undo" a getch() operation.

Returns ch, or -1 if some error occurred.

See also: getch() and putch().

Reimplemented from QIODevice.

### int QBuffer::writeBlock ( const char * p, uint len ) `[virtual]`

Writes len bytes from p into the buffer at the current index, overwriting any characters there and extending the buffer if necessary. Returns the number of bytes actually written.

Returns -1 if a serious error occurred.

See also: readBlock().

Reimplemented from QIODevice.

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qbuffer.h: 1998/08/25
- qbuffer.cpp: 1998/07/03