|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QObject
com.trolltech.qt.core.QIODevice
com.trolltech.qt.core.QBuffer
public class QBuffer
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; char ch; buffer.open(QBuffer::ReadWrite); buffer.write("Qt rocks!"); buffer.seek(0); buffer.getChar(&ch); // ch == 'Q' buffer.getChar(&ch); // ch == 't' buffer.getChar(&ch); // ch == ' ' buffer.getChar(&ch); // ch == '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; QBuffer buffer(&byteArray); buffer.open(QIODevice::WriteOnly); QDataStream out(&buffer); out << QApplication::palette();
Effectively, we convert the application's QPalette into a byte array. Here's how to read the data from the QByteArray:
QPalette palette; QBuffer buffer(&byteArray); buffer.open(QIODevice::ReadOnly); QDataStream in(&buffer); in >> palette;
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.
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.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I> |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.core.QIODevice |
---|
aboutToClose, bytesWritten, readyRead |
Constructor Summary | |
---|---|
QBuffer()
Equivalent to QBuffer(0). |
|
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 | |
---|---|
boolean |
atEnd()
Returns true if the current read and write position is at the end of the device (i.e. there is no more data available for reading on the device); otherwise returns false. |
QByteArray |
buffer()
This is the same as data. |
boolean |
canReadLine()
Returns true if a complete line of data can be read from the device; otherwise returns false. |
void |
close()
First emits aboutToClose, then closes the device and sets its OpenMode to NotOpen. |
QByteArray |
data()
Returns the data contained in the buffer. |
static QBuffer |
fromNativePointer(QNativePointer nativePointer)
This function returns the QBuffer instance pointed to by nativePointer |
boolean |
open(QIODevice.OpenMode openMode)
Opens the device and sets its OpenMode to openMode. |
long |
pos()
For random-access devices, this function returns the position that data is written to or read from. |
protected int |
readData(byte[] data)
Equivalent to readData(data, ). |
boolean |
seek(long off)
For random-access devices, this function sets the current position to off, returning true on success, or false if an error occurred. |
void |
setBuffer(QByteArray byteArray)
Makes the QBuffer use the byteArray as its internal buffer. |
void |
setData(byte[] data)
Sets the contents of the internal buffer to be data. |
void |
setData(QByteArray data)
Sets the contents of the internal buffer to be data. |
long |
size()
For open random-access devices, this function returns the size of the device. |
protected int |
writeData(byte[] data)
Equivalent to writeData(data, ). |
Methods inherited from class com.trolltech.qt.core.QIODevice |
---|
bytesAvailable, bytesToWrite, errorString, getByte, isOpen, isReadable, isSequential, isTextModeEnabled, isWritable, open, openMode, peek, peek, putByte, read, read, readAll, readLine, readLine, readLine, readLineData, reset, setErrorString, setOpenMode, setOpenMode, setTextModeEnabled, ungetByte, waitForBytesWritten, waitForReadyRead, write, write |
Methods inherited from class com.trolltech.qt.core.QObject |
---|
blockSignals, childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, property, removeEventFilter, setObjectName, setParent, setProperty, signalsBlocked, startTimer, thread, timerEvent |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
disconnect, disconnect, signalSender |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QBuffer()
Equivalent to QBuffer(0).
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.
public QBuffer(QByteArray byteArray, QObject parent)
If you open the buffer in write-only mode or read-write mode and write something into the QBuffer, byteArray will be modified.
public QBuffer(QByteArray byteArray)
Method Detail |
---|
public final QByteArray buffer()
This is the same as data.
public final QByteArray data()
Returns the data contained in the buffer.
This is the same as buffer.
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.
public boolean atEnd()
Returns true if the current read and write position is at the end of the device (i.e. there is no more data available for reading on the device); otherwise returns false.
For some devices, atEnd can return true even though there is more data to read. This special case only applies to devices that generate data in direct response to you calling read (e.g., /dev or /proc files on Unix and Mac OS X, or console input / stdin on all platforms).
atEnd
in class QIODevice
public boolean canReadLine()
Returns true if a complete line of data can be read from the device; otherwise returns false.
Note that unbuffered devices, which have no way of determining what can be read, always return false.
This function is often called in conjunction with the readyRead signal.
Subclasses that reimplement this function must call the base implementation in order to include the size of the QIODevice's buffer. Example:
bool CustomDevice::canReadLine() const { return buffer.contains('\n') || QIODevice::canReadLine(); }
canReadLine
in class QIODevice
public void close()
First emits aboutToClose, then closes the device and sets its OpenMode to NotOpen. The error string is also reset.
close
in class QIODevice
public boolean open(QIODevice.OpenMode openMode)
Opens the device and sets its OpenMode to openMode. Returns true if successful; otherwise returns false.
open
in class QIODevice
public long pos()
For random-access devices, this function returns the position that data is written to or read from. For sequential devices or closed devices, where there is no concept of a "current position", 0 is returned.
The current read/write position of the device is maintained internally by QIODevice, so reimplementing this function is not necessary. When subclassing QIODevice, use QIODevice::seek() to notify QIODevice about changes in the device position.
pos
in class QIODevice
protected int readData(byte[] data)
Equivalent to readData(data, ).
readData
in class QIODevice
public boolean seek(long off)
For random-access devices, this function sets the current position to off, returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.
When subclassing QIODevice, you must call QIODevice::seek() at the start of your function to ensure integrity with QIODevice's built-in buffer. The base implementation always returns true.
seek
in class QIODevice
public long size()
For open random-access devices, this function returns the size of the device. For open sequential devices, bytesAvailable is returned.
If the device is closed, the size returned will not reflect the actual size of the device.
size
in class QIODevice
protected int writeData(byte[] data)
Equivalent to writeData(data, ).
writeData
in class QIODevice
public static QBuffer fromNativePointer(QNativePointer nativePointer)
nativePointer
- the QNativePointer of which object should be returned.public void setBuffer(QByteArray byteArray)
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.
public final void setData(byte[] data)
Does nothing if isOpen() is true.
|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |