#include <qdatastream.h>
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 the serialization of C++'s basic data 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.
For integers it is best to always cast to a Qt integer type for writing, and to read back into the same Qt integer type. This ensures that you get integers of the size you want and insulates you from compiler and platform differences.
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 ('\0'), 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.
A similar pair of functions is readBytes() and writeBytes(). These differ from their 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.
See also QTextStream, QVariant, and Input/Output and Networking.
Warning: If you use QSocket or QSocketDevice as the IO device d for reading data, you must make sure that enough data is available on the socket for the operation to successfully proceed; QDataStream does not have any means to handle or recover from short-reads.
See also setDevice() and device().
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.
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.
See also setDevice() and unsetDevice().
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.
Writes an unsigned byte, i, to the stream and returns a reference to the stream.
Writes a signed 16-bit integer, i, to the stream and returns a reference to the stream.
Writes an unsigned 16-bit integer, i, to the stream and returns a reference to the stream.
Writes a signed 32-bit integer, i, to the stream and returns a reference to the stream.
Writes an unsigned integer, i, to the stream as a 32-bit unsigned integer (Q_UINT32). Returns a reference to the stream.
Writes a signed 64-bit integer, i, to the stream and returns a reference to the stream.
Writes an unsigned 64-bit integer, i, to the stream and returns a reference to the stream.
Writes a signed integer i, of the system's word length, to the stream and returns a reference to the stream.
Writes an unsigned integer i, of the system's word length, to the stream and returns a reference to the stream.
Writes a 32-bit floating point number, f, to the stream using the standard IEEE754 format. Returns a reference to the stream.
Writes a 64-bit floating point number, f, to the stream using the standard IEEE754 format. Returns a reference to the stream.
Writes the '\0'-terminated string s to the stream and returns a reference to the stream.
The string is serialized using writeBytes().
Reads an unsigned byte from the stream into i, and returns a reference to the stream.
Reads a signed 16-bit integer from the stream into i, and returns a reference to the stream.
Reads an unsigned 16-bit integer from the stream into i, and returns a reference to the stream.
Reads a signed 32-bit integer from the stream into i, and returns a reference to the stream.
Reads an unsigned 32-bit integer from the stream into i, and returns a reference to the stream.
Reads a signed 64-bit integer from the stream into i, and returns a reference to the stream.
Reads an unsigned 64-bit integer from the stream, into i, and returns a reference to the stream.
Reads a signed integer of the system's word length from the stream into i, and returns a reference to the stream.
Reads an unsigned integer of the system's word length from the stream, into i, and returns a reference to the stream.
Reads a 32-bit floating point number from the stream into f, using the standard IEEE754 format