Qt logo

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


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

Constructs a buffer and sets the buffer contents to buf.

See also: setBuffer().

QBuffer::~QBuffer()

Destroys the buffer.

boolQBuffer::at(intpos) [virtual]

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

See also: size().

Reimplemented from QIODevice.

intQBuffer::at()const [virtual]

Returns the buffer index.

See also: size().

Reimplemented from QIODevice.

QByteArrayQBuffer::buffer()const

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

voidQBuffer::close() [virtual]

Closes an open buffer.

See also: open().

Examples: grapher/grapher.cpp

Reimplemented from QIODevice.

voidQBuffer::flush() [virtual]

The flush function does nothing.

Reimplemented from QIODevice.

intQBuffer::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.

boolQBuffer::open(intm) [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.

See also: close() and isOpen().

Examples: grapher/grapher.cpp

Reimplemented from QIODevice.

intQBuffer::putch(intch) [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.

intQBuffer::readBlock(char*p, uintlen) [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.

intQBuffer::readLine(char*p, uintmaxlen) [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.

boolQBuffer::setBuffer(QByteArraybuf)

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

uintQBuffer::size()const [virtual]

Returns the number of bytes in the buffer.

See also: at().

Reimplemented from QIODevice.

intQBuffer::ungetch(intch) [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.

intQBuffer::writeBlock(constchar*p, uintlen) [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.


Copyright 1999 Troll TechTrademarks
Qt version 2.0.2