com.trolltech.qt.core
Class QBuffer

java.lang.Object
  extended by com.trolltech.qt.internal.QSignalEmitterInternal
      extended by com.trolltech.qt.QSignalEmitter
          extended by com.trolltech.qt.QtJambiObject
              extended by com.trolltech.qt.core.QObject
                  extended by com.trolltech.qt.core.QIODevice
                      extended by com.trolltech.qt.core.QBuffer
All Implemented Interfaces:
QtJambiInterface

public class QBuffer
extends QIODevice

The QBuffer class provides a QIODevice interface for a QByteArray. QBuffer allows you to access a QByteArray using the QIODevice interface. The QByteArray is treated just as a standard random-accessed file. Example:

    QBuffer buffer = new QBuffer();
    int b;

    buffer.open(QIODevice.OpenModeFlag.ReadWrite);
    buffer.write(new QByteArray("Qt rocks!"));
    buffer.seek(0);
    b = buffer.getByte();  // b == 'Q'
    b = buffer.getByte();  // b == 't'
    b = buffer.getByte();  // b == ' '
    b = buffer.getByte();  // b == 'r'
By default, an internal QByteArray buffer is created for you when you create 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().

The following code snippet shows how to write data to a QByteArray using QDataStream and QBuffer:

    QByteArray byteArray = new QByteArray();
    QBuffer buffer = new QBuffer(byteArray);
    buffer.open(QIODevice.OpenModeFlag.WriteOnly);

    QDataStream out = new QDataStream(buffer);
    out.writeString("Any string will do.");
Effectively, we convert the application's QPalette into a byte array. Here's how to read the data from the QByteArray:
    String string = "";
    QBuffer buffer = new QBuffer(byteArray);
    buffer.open(QIODevice.OpenModeFlag.ReadOnly);

    QDataStream in = new QDataStream(buffer);
    string = in.readString();
QTextStream and QDataStream also provide convenience constructors that take a QByteArray and that create a QBuffer behind the scenes.

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, readyRead() is emitted, and you can process the data that just arrived. QBuffer also emits bytesWritten() every time new data has been written to the buffer.

See also:
QFile, QDataStream, QTextStream, and QByteArray.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.core.QIODevice
QIODevice.OpenMode, QIODevice.OpenModeFlag
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.AbstractSignal, QSignalEmitter.PrivateSignal0, QSignalEmitter.PrivateSignal1, QSignalEmitter.PrivateSignal2, QSignalEmitter.PrivateSignal3, QSignalEmitter.PrivateSignal4, QSignalEmitter.PrivateSignal5, QSignalEmitter.PrivateSignal6, QSignalEmitter.PrivateSignal7, QSignalEmitter.PrivateSignal8, QSignalEmitter.PrivateSignal9, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9
 
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal
 
Field Summary
 
Fields inherited from class com.trolltech.qt.core.QIODevice
aboutToClose, bytesWritten, readChannelFinished, readyRead
 
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
currentSender
 
Constructor Summary
QBuffer()
          Constructs an empty buffer with the given parent.
QBuffer(QByteArray byteArray)
          Constructs a new QByteArray of which contents will be equal to byteArray.
QBuffer(QByteArray byteArray, QObject parent)
          Constructs a QBuffer that uses the QByteArray pointed to by byteArray as its internal buffer, and with the given parent.
QBuffer(QObject parent)
          Constructs an empty buffer with the given parent.
 
Method Summary
 QByteArray buffer()
          Returns a reference to the QBuffer's internal buffer.
 QByteArray data()
          Returns the data contained in the buffer.
 void setBuffer(QByteArray byteArray)
          Makes the QBuffer use the byteArray as its internal buffer.
 void setData(byte[] data)
          Sets the contents of this QBuffer to data
 void setData(QByteArray data)
          Sets the contents of the internal buffer to be data.
 
Methods inherited from class com.trolltech.qt.core.QIODevice
atEnd, bytesAvailable, bytesToWrite, canReadLine, close, errorString, getByte, isOpen, isReadable, isSequential, isTextModeEnabled, isWritable, open, open, openMode, peek, peek, pos, putByte, read, read, readAll, readData, readLine, readLine, readLine, readLineData, reset, seek, setErrorString, setOpenMode, setOpenMode, setTextModeEnabled, size, ungetByte, waitForBytesWritten, waitForReadyRead, write, write, writeData
 
Methods inherited from class com.trolltech.qt.core.QObject
childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, indexOfProperty, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, properties, property, removeEventFilter, setObjectName, setParent, setProperty, startTimer, timerEvent, toString, userProperty
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread
 
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
__qt_signalInitialization
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QBuffer

public QBuffer()
Constructs an empty buffer with the given parent. You can call setData() to fill the buffer with data, or you can open it in write mode and use write().

See also:
open().


QBuffer

public QBuffer(QObject parent)
Constructs an empty buffer with the given parent. You can call setData() to fill the buffer with data, or you can open it in write mode and use write().

See also:
open().


QBuffer

public QBuffer(QByteArray byteArray,
               QObject parent)
Constructs a QBuffer that uses the QByteArray pointed to by byteArray as its internal buffer, and with the given parent. The caller is responsible for ensuring that byteArray remains valid until the QBuffer is destroyed, or until setBuffer() is called to change the buffer. QBuffer doesn't take ownership of the QByteArray.

If you open the buffer in write-only mode or read-write mode and write something into the QBuffer, byteArray will be modified.


QBuffer

public QBuffer(QByteArray byteArray)
Constructs a new QByteArray of which contents will be equal to byteArray.

Method Detail

buffer

public final QByteArray buffer()
Returns a reference to the QBuffer's internal buffer. You can use it to modify the QByteArray behind the QBuffer's back.

See also:
setBuffer(), and data().


data

public final QByteArray data()
Returns the data contained in the buffer.

This is the same as buffer().

See also:
setData(), and setBuffer().


setData

public final void setData(QByteArray data)
Sets the contents of the internal buffer to be data. This is the same as assigning data to buffer().

Does nothing if isOpen() is true.

See also:
data(), and setBuffer().


setBuffer

public final void setBuffer(QByteArray byteArray)
Makes the QBuffer use the byteArray as its internal buffer.

Does nothing if isOpen() is true.

If you open the buffer in write-only mode or read-write mode and write something into the QBuffer, \a byteArray will be modified.

If byteArray is null, the buffer creates its own internal QByteArray to work on. This byte array is initially empty.


setData

public final void setData(byte[] data)
Sets the contents of this QBuffer to data