Qtopia Home - Classes - Hierachy - Annotated - Functions - Licenses - Reference |
|
The QDataStream class provides serialization of binary data to a QIODevice. More...
#include <qdatastream.h>
Inherited by QCopEnvelope and ServiceRequest.
The QDataStream class provides serialization of binary data to a QIODevice.
A data stream is a binary stream of encoded information which is 100% independent of the host computer operation system, CPU or byte order. A stream that is written by a PC under DOS/Windows can be read by a Sun SPARC running Solaris.
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.
The programmer 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 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 data to a stream):
QFile f( "file.dta" );
f.open( IO_WriteOnly ); // open file for writing
QDataStream s( &f ); // serialize using f
s << "the answer is"; // serialize string
s << (Q_INT32)42; // serialize integer
Example (read data from a stream):
QFile f( "file.dta" );
f.open( IO_ReadOnly ); // open file for reading
QDataStream s( &f ); // serialize using f
char *str;
Q_INT32 a;
s >> str >> a; // "the answer is" and 42
delete str; // delete string
In the last example, if you read into a QString instead of a char* you do not have to delete it.
Normally, each item written to the stream is written in a fixed binary format. For example, a char* 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. Similarly when reading a string, 4 bytes are read to create the 32-bit length value, then that many characters for the string including the NUL. For a complete description of all Qt types supporting data streaming see Format of the QDataStream operators .
If you want a "parsing" input stream, see QTextStream. If you just want the data to be human-readable to aid in debugging, you can set the data stream into printable data mode with setPrintableData(). The data is then written slower, in a human readable bloated form that is sufficient for debugging.
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:
// Open the file.
QFile f( "file.xxx" );
f.open( IO_WriteOnly );
QDataStream s( &f );
// Write a header with a "magic number" and a version
s << 0xa0b0c0d0;
s << 123;
// Write the data
s << [lots of interesting data]
Then read it in with:
// Open the file.
QFile f( "file.xxx" );
f.open( IO_ReadOnly );
QDataStream s( &f );
// Read and check the header
Q_UINT32 magic;
s >> magic;
if ( magic != 0xa0b0c0d0 )
return XXX_BAD_FILE_FORMAT;
// Read the version
Q_INT32 version;
s >> version;
if ( version < 100 )
return XXX_BAD_FILE_TOO_OLD;
if ( version > 123 )
return XXX_BAD_FILE_TOO_NEW;
if ( version <= 110 )
s.setVersion(1);
// Read the data
s >> [lots of interesting data];
if ( version > 120 )
s >> [data new in XXX version 1.2];
s >> [other interesting data];
See also QTextStream and QVariant.
See also setDevice().
See also setDevice() and device().
Example:
static char bindata[] = { 231, 1, 44, ... };
QByteArray a;
a.setRawData( bindata, sizeof(bindata) ); // a points to bindata
QDataStream s( a, IO_ReadOnly ); // open on a's data
s >> [something]; // read raw bindata
a.resetRawData( bindata, sizeof(bindata) ); // finished
The QArray::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.
Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position.
See also QIODevice::atEnd().
See also setByteOrder().
See also setDevice() and unsetDevice().
This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.
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.
See also QIODevice::atEnd().
See also setPrintableData().
The string is serialized using writeBytes().
Space for the string is allocated using new - the caller must eventually call delete[] on the value.
The buffer s is allocated using new. Destroy it with the delete[] operator. If the length is zero or s cannot be allocated, s is set to 0.
The l parameter will be set to the length of the buffer.
The serialization format is an Q_UINT32 length specifier first, then the data (l bytes).
See also readRawBytes() and writeBytes().
The buffer s must be preallocated.
See also readBytes(), QIODevice::readBlock(), and writeRawBytes().
The bo parameter can be QDataStream::BigEndian or QDataStream::LittleEndian.
The default setting is big endian. We recommend leaving this setting unless you have special requirements.
See also byteOrder().
See also device() and unsetDevice().
If this flag is set, the write functions will generate output that consists of printable characters (7 bit ASCII).
We recommend enabling printable data only for debugging purposes (it is slower and creates larger output).
In order to accomodate for 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.
For Qt 1.x compatibility, use v == 1.
For Qt 2.0.x compatibility, use v == 2 (Not required for reading in Qt 2.1).
See also version().
See also device() and setDevice().
See also setVersion().
The len is serialized as an Q_UINT32, followed by len bytes from s.
See also writeRawBytes() and readBytes().
See also writeBytes(), QIODevice::writeBlock(), and readRawBytes().
This file is part of the Qtopia platform, copyright © 1995-2005 Trolltech, all rights reserved.
| Copyright © 2005 Trolltech | Trademarks | Qtopia version 2.2.0
|