#include </home/clem/local/src/opie/qmake/include/qdatastream.h>
Inheritance diagram for QDataStream:


A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris.
You can also use a data stream to read/write raw unencoded binary data. If you want a "parsing" input stream, see QTextStream.
The QDataStream class implements serialization of primitive types, like char, short, int, char* etc. Serialization of more complex data is accomplished by breaking up the data into primitive units.
A data stream cooperates closely with a QIODevice. A QIODevice represents an input/output medium one can read data from and write data to. The QFile class is an example of an IO device.
Example (write binary data to a stream):
QFile file( "file.dat" ); file.open( IO_WriteOnly ); QDataStream stream( &file ); // we will serialize the data into the file stream << "the answer is"; // serialize a string stream << (Q_INT32)42; // serialize an integer
Example (read binary data from a stream):
QFile file( "file.dat" ); file.open( IO_ReadOnly ); QDataStream stream( &file ); // read the data serialized from the file QString str; Q_INT32 a; stream >> str >> a; // extract "the answer is" and 42
Each item written to the stream is written in a predefined binary format that varies depending on the item's type. Supported Qt types include QBrush, QColor, QDateTime, QFont, QPixmap, QString, QVariant and many others. For the complete list of all Qt types supporting data streaming see the Format of the QDataStream operators .
To take one example, a char* string is written as a 32-bit integer equal to the length of the string including the NUL byte (''), followed by all the characters of the string including the NUL byte. When reading a char* string, 4 bytes are read to create the 32-bit length value, then that many characters for the char* string including the NUL are read.
The initial IODevice is usually set in the constructor, but can be changed with setDevice(). If you've reached the end of the data (or if there is no IODevice set) atEnd() will return TRUE.
If you want the data to be compatible with an earlier version of Qt use setVersion().
If you want the data to be human-readable, e.g. for debugging, you can set the data stream into printable data mode with setPrintableData(). The data is then written slower, in a bloated but human readable format.
If you are producing a new binary data format, such as a file format for documents created by your application, you could use a QDataStream to write the data in a portable format. Typically, you would write a brief header containing a magic string and a version number to give yourself room for future expansion. For example:
QFile file( "file.xxx" ); file.open( IO_WriteOnly ); QDataStream stream( &file ); // Write a header with a "magic number" and a version stream << (Q_UINT32)0xA0B0C0D0; stream << (Q_INT32)123; // Write the data stream << [lots of interesting data]
Then read it in with:
QFile file( "file.xxx" ); file.open( IO_ReadOnly ); QDataStream stream( &file ); // Read and check the header Q_UINT32 magic; stream >> magic; if ( magic != 0xA0B0C0D0 ) return XXX_BAD_FILE_FORMAT; // Read the version Q_INT32 version; stream >> version; if ( version < 100 ) return XXX_BAD_FILE_TOO_OLD; if ( version > 123 ) return XXX_BAD_FILE_TOO_NEW; if ( version <= 110 ) stream.setVersion(1); // Read the data stream >> [lots of interesting data]; if ( version > 120 ) stream >> [data new in XXX version 1.2]; stream >> [other interesting data];
You can select which byte order to use when serializing data. The default setting is big endian (MSB first). Changing it to little endian breaks the portability (unless the reader also changes to little endian). We recommend keeping this setting unless you have special requirements.
raw
You may wish to read/write your own raw binary data to/from the data stream directly. Data may be read from the stream into a preallocated char* using readRawBytes(). Similarly data can be written to the stream using writeRawBytes(). Notice that any encoding/decoding of the data must be done by you. A similar pair of functions is readBytes() and writeBytes(). These differ from their \e raw counterparts as follows: readBytes() reads a Q_UINT32 which is taken to be the length of the data to be read, then that number of bytes is read into the preallocated char*; writeBytes() writes a Q_UINT32 containing the length of the data, followed by the data. Notice that any encoding/decoding of the data (apart from the length Q_UINT32) must be done by you. \sa QTextStream QVariant Definition at line 47 of file qdatastream.h.
|
|
The byte order used for reading/writing the data. BigEndian the default LittleEndian Definition at line 62 of file qdatastream.h. |
|
|
Constructs a data stream that has no IO device.
Definition at line 231 of file qdatastream.cpp. References BigEndian, byteorder, DefaultStreamVersion, dev, FALSE, noswap, owndev, printable, systemBigEndian, systemWordSize, and ver. |
|
|
Constructs a data stream that uses the IO device d.
Definition at line 255 of file qdatastream.cpp. References BigEndian, byteorder, DefaultStreamVersion, dev, FALSE, noswap, owndev, printable, systemBigEndian, systemWordSize, and ver. |
|
||||||||||||
|
Constructs a data stream that operates on a byte array, a, through an internal QBuffer device. The mode is a QIODevice::mode(), usually either Example: static char bindata[] = { 231, 1, 44, ... }; QByteArray a; a.setRawData( bindata, sizeof(bindata) ); // a points to bindata QDataStream stream( a, IO_ReadOnly ); // open on a's data stream >> [something]; // read raw bindata a.resetRawData( bindata, sizeof(bindata) ); // finished The QByteArray::setRawData() function is not for the inexperienced. Definition at line 286 of file qdatastream.cpp. References BigEndian, byteorder, DefaultStreamVersion, dev, FALSE, noswap, owndev, printable, systemBigEndian, systemWordSize, TRUE, and ver. |
|
|
Destroys the data stream. The destructor will not affect the current IO device, unless it is an internal IO device processing a QByteArray passed in the constructor, in which case the internal IO device is destroyed. Definition at line 307 of file qdatastream.cpp. |
|
|
|
|
|
Returns TRUE if the IO device has reached the end position (end of the stream or file) or if there is no IO device set; otherwise returns FALSE, i.e. if the current position of the IO device is before the end position.
Definition at line 126 of file qdatastream.h. References QIODevice::atEnd(), dev, and TRUE. Referenced by eof(), AlarmServer::initialize(), QIMPenCharSet::load(), operator>>(), QSafeDataStream::operator>>(), QPEApplication::QPEApplication(), QSafeDataStream::readBytes(), QSafeDataStream::readRawBytes(), and WLANModule::receive(). |
|
|
Returns the current byte order setting -- either
Definition at line 132 of file qdatastream.h. References byteorder. Referenced by QString::operator<<(). |
|
|
Returns the IO device currently set.
Definition at line 123 of file qdatastream.h. References dev. Referenced by QCopEnvelope::QCopEnvelope(), and QCopEnvelope::~QCopEnvelope(). |
|
|
Returns TRUE if the IO device has reached the end position (end of stream or file) or if there is no IO device set. Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position.
Definition at line 129 of file qdatastream.h. References atEnd(). Referenced by QCString::operator>>(), QMemArray< type >::operator>>(), and readBytes(). |
|
|
Returns TRUE if the printable data flag has been set; otherwise returns FALSE.
Definition at line 135 of file qdatastream.h. References printable. |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes the ''-terminated string s to the stream and returns a reference to the stream. The string is serialized using writeBytes(). Definition at line 980 of file qdatastream.cpp. References len, qstrlen(), and writeRawBytes(). |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes a 64-bit floating point number, f, to the stream using the standard IEEE754 format. Returns a reference to the stream. Definition at line 945 of file qdatastream.cpp. References buf, CHECK_STREAM_PRECOND, dev, noswap, p, printable, and QIODevice::writeBlock(). |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes a 32-bit floating point number, f, to the stream using the standard IEEE754 format. Returns a reference to the stream. Definition at line 913 of file qdatastream.cpp. References buf, CHECK_STREAM_PRECOND, dev, g, noswap, p, printable, and QIODevice::writeBlock(). |
|
|
Definition at line 168 of file qdatastream.h. |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes a signed integer i, of the system's word length, to the stream and returns a reference to the stream. Definition at line 880 of file qdatastream.cpp. References buf, CHECK_STREAM_PRECOND, dev, noswap, p, printable, and QIODevice::writeBlock(). |
|
|
Definition at line 165 of file qdatastream.h. |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes a signed 32-bit integer, i, to the stream and returns a reference to the stream. Definition at line 845 of file qdatastream.cpp. References buf, CHECK_STREAM_PRECOND, dev, noswap, p, printable, and QIODevice::writeBlock(). |
|
|
Definition at line 162 of file qdatastream.h. |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes a signed 16-bit integer, i, to the stream and returns a reference to the stream. Definition at line 819 of file qdatastream.cpp. References buf, CHECK_STREAM_PRECOND, dev, noswap, p, printable, and QIODevice::writeBlock(). |
|
|
Definition at line 159 of file qdatastream.h. |
|
|
Writes a signed byte, i, to the stream and returns a reference to the stream. Definition at line 787 of file qdatastream.cpp. References buf, CHECK_STREAM_PRECOND, dev, printable, QIODevice::putch(), and QIODevice::writeBlock(). |
|
|
|
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads the ''-terminated string s from the stream and returns a reference to the stream.
Space for the string is allocated using Reimplemented in QSafeDataStream. Definition at line 697 of file qdatastream.cpp. References len, and readBytes(). |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads a 64-bit floating point number from the stream into f, using the standard IEEE754 format. Returns a reference to the stream. Reimplemented in QSafeDataStream. Definition at line 663 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, dev, noswap, p, printable, read_double_ascii(), and QIODevice::readBlock(). |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads a 32-bit floating point number from the stream into f, using the standard IEEE754 format. Returns a reference to the stream. Reimplemented in QSafeDataStream. Definition at line 635 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, dev, noswap, p, printable, read_double_ascii(), and QIODevice::readBlock(). |
|
|
Definition at line 156 of file qdatastream.h. |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads a signed integer of the system's word length from the stream into i, and returns a reference to the stream. Definition at line 595 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, dev, noswap, p, printable, read_int_ascii(), and QIODevice::readBlock(). |
|
|
Reimplemented in QSafeDataStream. Definition at line 153 of file qdatastream.h. |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads a signed 32-bit integer from the stream into i, and returns a reference to the stream. Reimplemented in QSafeDataStream. Definition at line 562 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, dev, noswap, p, printable, read_int_ascii(), and QIODevice::readBlock(). |
|
|
Reimplemented in QSafeDataStream. Definition at line 150 of file qdatastream.h. |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads a signed 16-bit integer from the stream into i, and returns a reference to the stream. Reimplemented in QSafeDataStream. Definition at line 530 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, dev, noswap, p, printable, read_int_ascii(), and QIODevice::readBlock(). |
|
|
Reimplemented in QSafeDataStream. Definition at line 147 of file qdatastream.h. |
|
|
Reads a signed byte from the stream into i, and returns a reference to the stream. Reimplemented in QSafeDataStream. Definition at line 499 of file qdatastream.cpp. References buf, CHECK_STREAM_PRECOND, dev, QIODevice::getch(), printable, and QIODevice::readBlock(). Referenced by QSafeDataStream::operator>>(). |
|
||||||||||||
|
Reads the buffer s from the stream and returns a reference to the stream.
The buffer s is allocated using The l parameter will be set to the length of the buffer. The serialization format is a Q_UINT32 length specifier first, then l bytes of data. Note that the data is not encoded.
Reimplemented in QSafeDataStream. Definition at line 720 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, eof(), len, and readRawBytes(). Referenced by operator>>(), and QSafeDataStream::readBytes(). |
|
||||||||||||
|
Reads len bytes from the stream into s and returns a reference to the stream. The buffer s must be preallocated. The data is not encoded.
Reimplemented in QSafeDataStream. Definition at line 748 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, dev, p, printable, QIODevice::readBlock(), and version(). Referenced by MainWindow::documentOpen(), QCString::operator>>(), QMemArray< type >::operator>>(), QBitArray::operator>>(), readBytes(), and QSafeDataStream::readRawBytes(). |
|
|
Sets the serialization byte order to bo.
The bo parameter can be The default setting is big endian. We recommend leaving this setting unless you have special requirements.
Definition at line 396 of file qdatastream.cpp. References BigEndian, byteorder, LittleEndian, noswap, and systemBigEndian. Referenced by Opietooth2::OTHCISocket::sendCommand(), and Opietooth2::OTHCISocket::updateStatus(). |
|
|
void QDataStream::setDevice(QIODevice *d ) Sets the IO device to d.
Definition at line 330 of file qdatastream.cpp. References dev, FALSE, and owndev. Referenced by unsetDevice(). |
|
|
If enable is TRUE, data will be output in a human readable format. If enable is FALSE, data will be output in a binary format. If enable is TRUE, the write functions will generate output that consists of printable characters (7 bit ASCII). This output will typically be a lot larger than the default binary output, and consequently slower to write. We recommend only enabling printable data for debugging purposes. Definition at line 138 of file qdatastream.h. References printable. |
|
|
Sets the version number of the data serialization format to v. You don't need to set a version if you are using the current version of Qt. In order to accommodate new functionality, the datastream serialization format of some Qt classes has changed in some versions of Qt. If you want to read data that was created by an earlier version of Qt, or write data that can be read by a program that was compiled with an earlier version of Qt, use this function to modify the serialization format of QDataStream. Qt Version QDataStream Version Qt 3.1 5 Qt 3.0 4 Qt 2.1.x and Qt 2.2.x 3 Qt 2.0.x 2 Qt 1.x 1
Definition at line 144 of file qdatastream.h. References ver. |
|
|
Unsets the IO device. This is the same as calling setDevice( 0 ).
Definition at line 345 of file qdatastream.cpp. References setDevice(). |
|
|
Returns the version number of the data serialization format. In Qt 3.1, this number is 5.
Definition at line 141 of file qdatastream.h. References ver. Referenced by QString::operator<<(), readRawBytes(), and writeRawBytes(). |
|
||||||||||||
|
Writes the length specifier len and the buffer s to the stream and returns a reference to the stream. The len is serialized as a Q_UINT32, followed by len bytes from s. Note that the data is not encoded.
Definition at line 1002 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, and writeRawBytes(). Referenced by QString::operator<<(), QCString::operator<<(), QMemArray< type >::operator<<(), and QDawgPrivate::write(). |
|
||||||||||||
|
Writes len bytes from s to the stream and returns a reference to the stream. The data is not encoded.
Definition at line 1019 of file qdatastream.cpp. References CHECK_STREAM_PRECOND, dev, p, printable, version(), and QIODevice::writeBlock(). Referenced by MainWindow::documentSave(), operator<<(), QBitArray::operator<<(), ProcessInvoker::readOutputs(), Opietooth2::OTHCISocket::sendCommand(), QDawgPrivate::write(), and writeBytes(). |
|
|
Definition at line 106 of file qdatastream.h. Referenced by byteOrder(), QDataStream(), and setByteOrder(). |
|
|
Definition at line 104 of file qdatastream.h. Referenced by atEnd(), device(), operator<<(), operator>>(), QDataStream(), readRawBytes(), setDevice(), writeRawBytes(), and ~QDataStream(). |
|
|
Definition at line 108 of file qdatastream.h. Referenced by operator<<(), operator>>(), QDataStream(), and setByteOrder(). |
|
|
Definition at line 105 of file qdatastream.h. Referenced by QDataStream(), setDevice(), and ~QDataStream(). |
|
|
Definition at line 107 of file qdatastream.h. Referenced by isPrintableData(), operator<<(), operator>>(), QDataStream(), readRawBytes(), setPrintableData(), and writeRawBytes(). |
|
|
Definition at line 109 of file qdatastream.h. Referenced by QDataStream(), setVersion(), and version(). |
1.4.2