The QDataStream class provides serialization of binary data to a QIODevice.
.PP
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.
.PP
You can also use a data stream to read/write raw unencoded binary data. If you want a "parsing" input stream, see QTextStream.
.PP
The QDataStream class implements the serialization of C++'s basic data types, like \fCchar\fR, \fCshort\fR, \fCint\fR, \fCchar*\fR, etc. Serialization of more complex data is accomplished by breaking up the data into primitive units.
.PP
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.
.PP
Example (write binary data to a stream):
.PP
.nf
.br
QFile file( "file.dat" );
.br
file.open( IO_WriteOnly );
.br
QDataStream stream( &file ); // we will serialize the data into the file
stream >> str >> a; // extract "the answer is" and 42
.br
.fi
.PP
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.
.PP
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.
.PP
To take one example, a \fCchar*\fR 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 \fCchar*\fR string, 4 bytes are read to create the 32-bit length value, then that many characters for the \fCchar*\fR string including the NUL are read.
.PP
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.
.PP
If you want the data to be compatible with an earlier version of Qt use setVersion().
.PP
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.
.PP
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:
.PP
.nf
.br
QFile file( "file.xxx" );
.br
file.open( IO_WriteOnly );
.br
QDataStream stream( &file );
.br
.br
// Write a header with a "magic number" and a version
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.
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 \fIraw\fR counterparts as follows: readBytes() reads a TQ_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 TQ_UINT32 containing the length of the data, followed by the data. Notice that any encoding/decoding of the data (apart from the length TQ_UINT32) must be done by you.
See also QTextStream, QVariant, and Input/Output and Networking.
.SS "Member Type Documentation"
.SH "QDataStream::ByteOrder"
The byte order used for reading/writing the data.
.TP
\fCQDataStream::BigEndian\fR - the default
.TP
\fCQDataStream::LittleEndian\fR
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QDataStream::QDataStream ()"
Constructs a data stream that has no IO device.
.PP
See also setDevice().
.SH "QDataStream::QDataStream ( QIODevice * d )"
Constructs a data stream that uses the IO device \fId\fR.
.PP
\fBWarning:\fR If you use QSocket or QSocketDevice as the IO device \fId\fR 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.
.PP
See also setDevice() and device().
.SH "QDataStream::QDataStream ( QByteArray a, int mode )"
Constructs a data stream that operates on a byte array, \fIa\fR, through an internal QBuffer device. The \fImode\fR is a QIODevice::mode(), usually either IO_ReadOnly or IO_WriteOnly.
.PP
Example:
.PP
.nf
.br
static char bindata[] = { 231, 1, 44, ... };
.br
QByteArray a;
.br
a.setRawData( bindata, sizeof(bindata) ); // a points to bindata
.br
QDataStream stream( a, IO_ReadOnly ); // open on a's data
The destructor will not affect the current IO device, unless it is an internal IO device processing a QByteArray passed in the \fIconstructor\fR, in which case the internal IO device is destroyed.
.SH "bool QDataStream::atEnd () const"
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.
.PP
See also QIODevice::atEnd().
.SH "int QDataStream::byteOrder () const"
Returns the current byte order setting -- either BigEndian or LittleEndian.
.PP
See also setByteOrder().
.SH "QIODevice * QDataStream::device () const"
Returns the IO device currently set.
.PP
See also setDevice() and unsetDevice().
.SH "bool QDataStream::eof () const"
\fBThis function is obsolete.\fR It is provided to keep old source working. We strongly advise against using it in new code.
.PP
Returns TRUE if the IO device has reached the end position (end of stream or file) or if there is no IO device set.
.PP
Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position.
.PP
See also QIODevice::atEnd().
.SH "bool QDataStream::isPrintableData () const"
Returns TRUE if the printable data flag has been set; otherwise returns FALSE.
Reads the buffer \fIs\fR from the stream and returns a reference to the stream.
.PP
The buffer \fIs\fR is allocated using \fCnew\fR. Destroy it with the \fCdelete[]\fR operator. If the length is zero or \fIs\fR cannot be allocated, \fIs\fR is set to 0.
.PP
The \fIl\fR parameter will be set to the length of the buffer.
If \fIenable\fR is TRUE, data will be output in a human readable format. If \fIenable\fR is FALSE, data will be output in a binary format.
.PP
If \fIenable\fR 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.
.PP
We recommend only enabling printable data for debugging purposes.
.SH "void QDataStream::setVersion ( int v )"
Sets the version number of the data serialization format to \fIv\fR.
.PP
You don't need to set a version if you are using the current version of Qt.
.PP
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.
.PP
<center>.nf
.TS
l - l. Qt Version QDataStream Version Qt 3.3 6 Qt 3.2 5 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
.TE
.fi
</center>
.PP
See also version().
.SH "void QDataStream::unsetDevice ()"
Unsets the IO device. This is the same as calling setDevice( 0 ).
.PP
See also device() and setDevice().
.SH "int QDataStream::version () const"
Returns the version number of the data serialization format. In Qt 3.1, this number is 5.