QDataStream Class Reference

#include <qdatastream.h>

Collaboration diagram for QDataStream:

Collaboration graph
[legend]
List of all members.

Detailed Description

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'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}{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 I/O device.

Example (write binary data to a stream):

        QFile file("file.dat");
        file.open(QIODevice::WriteOnly);
        QDataStream out(&file);   // we will serialize the data into the file
        out << "the answer is";   // serialize a string
        out << (qint32)42;        // serialize an integer

Example (read binary data from a stream):

        QFile file("file.dat");
        file.open(QIODevice::ReadOnly);
        QDataStream in(&file);    // read the data serialized from the file
        QString str;
        qint32 a;
        in >> 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 '\0' byte, followed by all the characters of the string including the '\0' 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 '\0' terminator are read.

The initial I/O device 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 I/O device set) atEnd() will return true.

QDataStream's binary format has evolved since Qt 1.0, and is likely to continue evolving to reflect changes done in Qt. When inputting or outputting complex types, it's very important to make sure that the same version of the stream (version()) is used for reading and writing. If you need both forward and backward compatibility, you can hardcode the version number in the application: @code stream.setVersion(QDataStream::Qt_4_0); \endcode 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: @code QFile file("file.xxx"); file.open(QIODevice::WriteOnly); QDataStream out(&file); // Write a header with a "magic number" and a version out << (quint32)0xA0B0C0D0; out << (qint32)123; out.setVersion(QDataStream::Qt_4_0); // Write the data out << lots_of_interesting_data; \endcode Then read it in with: @code QFile file("file.xxx"); file.open(QIODevice::ReadOnly); QDataStream in(&file); // Read and check the header quint32 magic; in >> magic; if (magic != 0xA0B0C0D0) return XXX_BAD_FILE_FORMAT; // Read the version qint32 version; in >> version; if (version < 100) return XXX_BAD_FILE_TOO_OLD; if (version > 123) return XXX_BAD_FILE_TOO_NEW; if (version <= 110) in.setVersion(QDataStream::Qt_3_2); else in.setVersion(QDataStream::Qt_4_0); // Read the data in >> lots_of_interesting_data; if (version >= 120) in >> data_new_in_XXX_version_1_2; in >> other_interesting_data; \endcode 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. \target raw @section Reading and writing raw binary data 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 \c{char *} using readRawData(). Similarly data can be written to the stream using writeRawData(). Note 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 quint32 which is taken to be the length of the data to be read, then that number of bytes is read into the preallocated \c{char *}; writeBytes() writes a quint32 containing the length of the data, followed by the data. Note that any encoding/decoding of the data (apart from the length quint32) must be done by you. \sa QTextStream QVariant Definition at line 51 of file qdatastream.h.

Public Types

enum  Version
enum  ByteOrder
enum  Status

Public Member Functions

 QDataStream ()
 QDataStream (QIODevice *)
 QDataStream (QByteArray *, QIODevice::OpenMode flags)
 QDataStream (const QByteArray &)
virtual ~QDataStream ()
QIODevicedevice () const
void setDevice (QIODevice *)
void unsetDevice ()
bool atEnd () const
Status status () const
void setStatus (Status status)
void resetStatus ()
ByteOrder byteOrder () const
void setByteOrder (ByteOrder)
int version () const
void setVersion (int)
QDataStreamoperator>> (qint8 &i)
QDataStreamoperator>> (quint8 &i)
QDataStreamoperator>> (qint16 &i)
QDataStreamoperator>> (quint16 &i)
QDataStreamoperator>> (qint32 &i)
QDataStreamoperator>> (quint32 &i)
QDataStreamoperator>> (qint64 &i)
QDataStreamoperator>> (quint64 &i)
QDataStreamoperator>> (bool &i)
QDataStreamoperator>> (float &f)
QDataStreamoperator>> (double &f)
QDataStreamoperator>> (char *&str)
QDataStreamoperator<< (qint8 i)
QDataStreamoperator<< (quint8 i)
QDataStreamoperator<< (qint16 i)
QDataStreamoperator<< (quint16 i)
QDataStreamoperator<< (qint32 i)
QDataStreamoperator<< (quint32 i)
QDataStreamoperator<< (qint64 i)
QDataStreamoperator<< (quint64 i)
QDataStreamoperator<< (bool i)
QDataStreamoperator<< (float f)
QDataStreamoperator<< (double f)
QDataStreamoperator<< (const char *str)
QDataStreamreadBytes (char *&, uint &len)
int readRawData (char *, int len)
QDataStreamwriteBytes (const char *, uint len)
int writeRawData (const char *, int len)
int skipRawData (int len)

Private Attributes

QDataStreamPrivate * d
QIODevicedev
bool owndev
bool noswap
ByteOrder byteorder
int ver
Status q_status


Member Enumeration Documentation

enum QDataStream::Version

This enum provides symbolic synonyms for the data serialization format version numbers.

Qt_1_0 Version 1 (Qt 1.x) Qt_2_0 Version 2 (Qt 2.0) Qt_2_1 Version 3 (Qt 2.1, 2.2, 2.3) Qt_3_0 Version 4 (Qt 3.0) Qt_3_1 Version 5 (Qt 3.1, 3.2) Qt_3_3 Version 6 (Qt 3.3) Qt_4_0 Version 7 (Qt 4.0, Qt 4.1) Qt_4_1 Version 7 (Qt 4.0, Qt 4.1) Qt_4_2 Version 8 (Qt 4.2)

See also:
setVersion(), version()

Definition at line 54 of file qdatastream.h.

00054                  {
00055         Qt_1_0 = 1,
00056         Qt_2_0 = 2,
00057         Qt_2_1 = 3,
00058         Qt_3_0 = 4,
00059         Qt_3_1 = 5,
00060         Qt_3_3 = 6,
00061         Qt_4_0 = 7,
00062         Qt_4_1 = Qt_4_0,
00063         Qt_4_2 = 8
00064 #if QT_VERSION >= 0x040300
00065 #error Add Qt_4_3 = Qt_4_2
00066 #endif
00067     };

enum QDataStream::ByteOrder

The byte order used for reading/writing the data.

BigEndian Most significant byte first (the default) LittleEndian Less significant byte first

Definition at line 69 of file qdatastream.h.

00069                    {
00070         BigEndian = QSysInfo::BigEndian,
00071         LittleEndian = QSysInfo::LittleEndian
00072     };

enum QDataStream::Status

This enum describes the current status of the data stream.

Ok The data stream is operating normally. ReadPastEnd The data stream has read past the end of the data in the underlying device. ReadCorruptData The data stream has read corrupt data.

Definition at line 74 of file qdatastream.h.

00074                 {
00075         Ok,
00076         ReadPastEnd,
00077   ReadCorruptData
00078     };


Constructor & Destructor Documentation

QDataStream::QDataStream (  ) 

Constructs a data stream that has no I/O device.

See also:
setDevice()

Definition at line 246 of file qdatastream.cpp.

References BigEndian, byteorder, DefaultStreamVersion, dev, noswap, Ok, owndev, q_status, and ver.

00247 {
00248     dev = 0;
00249     owndev = false;
00250     byteorder = BigEndian;
00251     ver = DefaultStreamVersion;
00252     noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
00253     q_status = Ok;
00254 }

QDataStream::QDataStream ( QIODevice d  )  [explicit]

Constructs a data stream that uses the I/O device d.

Warning:
If you use QSocket or QSocketDevice as the I/O 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(), device()

Definition at line 268 of file qdatastream.cpp.

References BigEndian, byteorder, d, DefaultStreamVersion, dev, noswap, Ok, owndev, q_status, and ver.

00269 {
00270     dev = d;                                // set device
00271     owndev = false;
00272     byteorder = BigEndian;                        // default byte order
00273     ver = DefaultStreamVersion;
00274     noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
00275     q_status = Ok;
00276 }

QDataStream::QDataStream ( QByteArray a,
QIODevice::OpenMode  mode 
)

Constructs a data stream that operates on a byte array, a. The mode describes how the device is to be used.

Alternatively, you can use QDataStream(const QByteArray &) if you just want to read from a byte array.

Since QByteArray is not a QIODevice subclass, internally a QBuffer is created to wrap the byte array.

Definition at line 313 of file qdatastream.cpp.

References a, BigEndian, buf, byteorder, DefaultStreamVersion, dev, noswap, Ok, owndev, q_status, and ver.

00314 {
00315     QBuffer *buf = new QBuffer(a);
00316     buf->open(flags);
00317     dev = buf;
00318     owndev = true;
00319     byteorder = BigEndian;
00320     ver = DefaultStreamVersion;
00321     noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
00322     q_status = Ok;
00323 }

QDataStream::QDataStream ( const QByteArray a  ) 

Constructs a read-only data stream that operates on byte array a. Use QDataStream(QByteArray*, int) if you want to write to a byte array.

Since QByteArray is not a QIODevice subclass, internally a QBuffer is created to wrap the byte array.

Definition at line 333 of file qdatastream.cpp.

References a, BigEndian, buf, byteorder, DefaultStreamVersion, dev, noswap, Ok, owndev, q_status, QIODevice::ReadOnly, and ver.

00334 {
00335     QBuffer *buf = new QBuffer;
00336     buf->setData(a);
00337     buf->open(QIODevice::ReadOnly);
00338     dev = buf;
00339     owndev = true;
00340     byteorder = BigEndian;
00341     ver = DefaultStreamVersion;
00342     noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
00343     q_status = Ok;
00344 }

QDataStream::~QDataStream (  )  [virtual]

Destroys the data stream.

The destructor will not affect the current I/O device, unless it is an internal I/O device (e.g. a QBuffer) processing a QByteArray passed in the constructor, in which case the internal I/O device is destroyed.

Definition at line 355 of file qdatastream.cpp.

References dev, and owndev.

00356 {
00357     if (owndev)
00358         delete dev;
00359 }


Member Function Documentation

QIODevice * QDataStream::device (  )  const [inline]

Returns the I/O device currently set.

See also:
setDevice(), unsetDevice()

Definition at line 170 of file qdatastream.h.

Referenced by Server::sendFortune(), and Translator::squeeze().

00171 { return dev; }

void QDataStream::setDevice ( QIODevice d  ) 

void QDataStream::setDevice(QIODevice *d)

Sets the I/O device to d.

See also:
device(), unsetDevice()

Definition at line 378 of file qdatastream.cpp.

References d, dev, and owndev.

Referenced by HelpDialog::loadIndexFile(), and unsetDevice().

00379 {
00380     if (owndev) {
00381         delete dev;
00382         owndev = false;
00383     }
00384     dev = d;
00385 }

void QDataStream::unsetDevice (  ) 

Unsets the I/O device. This is the same as calling setDevice(0).

See also:
device(), setDevice()

Definition at line 393 of file qdatastream.cpp.

References setDevice().

00394 {
00395     setDevice(0);
00396 }

Here is the call graph for this function:

bool QDataStream::atEnd (  )  const

Returns true if the I/O device has reached the end position (end of the stream or file) or if there is no I/O device set; otherwise returns false.

See also:
QIODevice::atEnd()

Definition at line 409 of file qdatastream.cpp.

References QIODevice::atEnd(), and dev.

Referenced by HelpDialog::getAllContents(), and operator>>().

00410 {
00411     return dev ? dev->atEnd() : true;
00412 }

Here is the call graph for this function:

QDataStream::Status QDataStream::status (  )  const

Returns the status of the data stream.

See also:
Status setStatus() resetStatus()

Definition at line 420 of file qdatastream.cpp.

References q_status.

Referenced by operator>>().

00421 {
00422     return q_status;
00423 }

void QDataStream::setStatus ( Status  status  ) 

Sets the status of the data stream to the status given.

See also:
Status status() resetStatus()

Definition at line 440 of file qdatastream.cpp.

References Ok, and q_status.

Referenced by QHostAddress::operator>>(), QByteArray::operator>>(), operator>>(), operator>>(), and readBytes().

00441 {
00442     if (q_status == Ok)
00443         q_status = status;
00444 }

void QDataStream::resetStatus (  ) 

Resets the status of the data stream.

See also:
Status status() setStatus()

Definition at line 430 of file qdatastream.cpp.

References Ok, and q_status.

Referenced by operator>>().

00431 {
00432     q_status = Ok;
00433 }

QDataStream::ByteOrder QDataStream::byteOrder (  )  const [inline]

Returns the current byte order setting -- either BigEndian or LittleEndian.

See also:
setByteOrder()

Definition at line 173 of file qdatastream.h.

References byteorder.

Referenced by operator<<(), and operator>>().

00174 { return byteorder; }

void QDataStream::setByteOrder ( ByteOrder  bo  ) 

Sets the serialization byte order to bo.

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()

Definition at line 472 of file qdatastream.cpp.

References BigEndian, byteorder, LittleEndian, and noswap.

00473 {
00474     byteorder = bo;
00475     if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
00476         noswap = (byteorder == BigEndian);
00477     else
00478         noswap = (byteorder == LittleEndian);
00479 }

int QDataStream::version (  )  const [inline]

Returns the version number of the data serialization format.

See also:
setVersion(), Version

Definition at line 176 of file qdatastream.h.

References ver.

Referenced by QByteArray::operator<<(), operator<<(), operator<<(), operator>>(), operator>>(), and QTreeWidgetItem::read().

00177 { return ver; }

void QDataStream::setVersion ( int  v  )  [inline]

Sets the version number of the data serialization format to v.

You don't have to set a version if you are using the current version of Qt, but for your own custom binary formats we recommend that you do; see {Versioning} in the Detailed Description.

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 used by QDataStream.

Qt Version QDataStream Version Qt 4.2 8 Qt 4.0 7 Qt 3.3 6 Qt 3.1, 3.2 5 Qt 3.0 4 Qt 2.1, 2.2, 2.3 3 Qt 2.0 2 Qt 1.x 1

The Version enum provides symbolic constants for the different versions of Qt. For example:

        QDataStream out(file);
        out.setVersion(QDataStream::Qt_4_0);

See also:
version(), Version

Definition at line 179 of file qdatastream.h.

References ver.

Referenced by Client::readFortune(), FortuneThread::run(), and Server::sendFortune().

00180 { ver = v; }

QDataStream & QDataStream::operator>> ( qint8 i  ) 

Reads a signed byte from the stream into i, and returns a reference to the stream.

Definition at line 584 of file qdatastream.cpp.

References c, CHECK_STREAM_PRECOND, dev, QIODevice::getChar(), ReadPastEnd, and setStatus().

00585 {
00586     i = 0;
00587     CHECK_STREAM_PRECOND(*this)
00588     char c;
00589     if (!dev->getChar(&c))
00590         setStatus(ReadPastEnd);
00591     else
00592         i = qint8(c);
00593     return *this;
00594 }

Here is the call graph for this function:

QDataStream & QDataStream::operator>> ( quint8 i  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads an unsigned byte from the stream into i, and returns a reference to the stream.

Definition at line 182 of file qdatastream.h.

00183 { return *this >> reinterpret_cast<qint8&>(i); }

QDataStream & QDataStream::operator>> ( qint16 i  ) 

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.

Definition at line 612 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, QIODevice::read(), ReadPastEnd, and setStatus().

00613 {
00614     i = 0;
00615     CHECK_STREAM_PRECOND(*this)
00616     if (noswap) {
00617         if (dev->read((char *)&i, 2) != 2) {
00618             i = 0;
00619             setStatus(ReadPastEnd);
00620         }
00621     } else {
00622         register uchar *p = (uchar *)(&i);
00623         char b[2];
00624         if (dev->read(b, 2) == 2) {
00625             *p++ = b[1];
00626             *p = b[0];
00627         } else {
00628             setStatus(ReadPastEnd);
00629         }
00630     }
00631     return *this;
00632 }

Here is the call graph for this function:

QDataStream & QDataStream::operator>> ( quint16 i  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads an unsigned 16-bit integer from the stream into i, and returns a reference to the stream.

Definition at line 185 of file qdatastream.h.

00186 { return *this >> reinterpret_cast<qint16&>(i); }

QDataStream & QDataStream::operator>> ( qint32 i  ) 

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.

Definition at line 650 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, QIODevice::read(), ReadPastEnd, and setStatus().

00651 {
00652     i = 0;
00653     CHECK_STREAM_PRECOND(*this)
00654     if (noswap) {
00655         if (dev->read((char *)&i, 4) != 4) {
00656             i = 0;
00657             setStatus(ReadPastEnd);
00658         }
00659     } else {                                        // swap bytes
00660         uchar *p = (uchar *)(&i);
00661         char b[4];
00662         if (dev->read(b, 4) == 4) {
00663             *p++ = b[3];
00664             *p++ = b[2];
00665             *p++ = b[1];
00666             *p   = b[0];
00667         } else {
00668             setStatus(ReadPastEnd);
00669         }
00670     }
00671     return *this;
00672 }

Here is the call graph for this function:

QDataStream & QDataStream::operator>> ( quint32 i  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads an unsigned 32-bit integer from the stream into i, and returns a reference to the stream.

Definition at line 188 of file qdatastream.h.

00189 { return *this >> reinterpret_cast<qint32&>(i); }

QDataStream & QDataStream::operator>> ( qint64 i  ) 

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 64-bit integer from the stream into i, and returns a reference to the stream.

Definition at line 689 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, QIODevice::read(), ReadPastEnd, setStatus(), and version().

00690 {
00691     i = qint64(0);
00692     CHECK_STREAM_PRECOND(*this)
00693     if (version() < 6) {
00694         quint32 i1, i2;
00695         *this >> i2 >> i1;
00696         i = ((quint64)i1 << 32) + i2;
00697     } else if (noswap) {                        // no conversion needed
00698         if (dev->read((char *)&i, 8) != 8) {
00699             i = qint64(0);
00700             setStatus(ReadPastEnd);
00701         }
00702     } else {                                        // swap bytes
00703         uchar *p = (uchar *)(&i);
00704         char b[8];
00705         if (dev->read(b, 8) == 8) {
00706             *p++ = b[7];
00707             *p++ = b[6];
00708             *p++ = b[5];
00709             *p++ = b[4];
00710             *p++ = b[3];
00711             *p++ = b[2];
00712             *p++ = b[1];
00713             *p   = b[0];
00714         } else {
00715             setStatus(ReadPastEnd);
00716         }
00717     }
00718     return *this;
00719 }

Here is the call graph for this function:

QDataStream & QDataStream::operator>> ( quint64 i  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads an unsigned 64-bit integer from the stream, into i, and returns a reference to the stream.

Definition at line 191 of file qdatastream.h.

00192 { return *this >> reinterpret_cast<qint64&>(i); }

QDataStream & QDataStream::operator>> ( bool &  i  ) 

Reads a boolean value from the stream into i. Returns a reference to the stream.

Definition at line 725 of file qdatastream.cpp.

00726 {
00727     qint8 v;
00728     *this >> v;
00729     i = !!v;
00730     return *this;
00731 }

QDataStream & QDataStream::operator>> ( float &  f  ) 

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 IEEE 754 format. Returns a reference to the stream.

Definition at line 741 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, QIODevice::read(), ReadPastEnd, and setStatus().

00742 {
00743     f = 0.0f;
00744     CHECK_STREAM_PRECOND(*this)
00745     if (noswap) {
00746         if (dev->read((char *)&f, 4) != 4) {
00747             f = 0.0f;
00748             setStatus(ReadPastEnd);
00749         }
00750     } else {                                        // swap bytes
00751         uchar *p = (uchar *)(&f);
00752         char b[4];
00753         if (dev->read(b, 4) == 4) {
00754             *p++ = b[3];
00755             *p++ = b[2];
00756             *p++ = b[1];
00757             *p = b[0];
00758         } else {
00759             setStatus(ReadPastEnd);
00760         }
00761     }
00762     return *this;
00763 }

Here is the call graph for this function:

QDataStream & QDataStream::operator>> ( double &  f  ) 

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 IEEE 754 format. Returns a reference to the stream.

Definition at line 774 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, QIODevice::read(), ReadPastEnd, and setStatus().

00775 {
00776     f = 0.0;
00777     CHECK_STREAM_PRECOND(*this)
00778     if (noswap) {
00779         if (dev->read((char *)&f, 8) != 8) {
00780             f = 0.0;
00781             setStatus(ReadPastEnd);
00782         }
00783     } else {                                        // swap bytes
00784         register uchar *p = (uchar *)(&f);
00785         char b[8];
00786         if (dev->read(b, 8) == 8) {
00787             *p++ = b[7];
00788             *p++ = b[6];
00789             *p++ = b[5];
00790             *p++ = b[4];
00791             *p++ = b[3];
00792             *p++ = b[2];
00793             *p++ = b[1];
00794             *p   = b[0];
00795         } else {
00796             setStatus(ReadPastEnd);
00797         }
00798     }
00799     return *this;
00800 }

Here is the call graph for this function:

QDataStream & QDataStream::operator>> ( char *&  s  ) 

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 new -- the caller must destroy it with {delete[]}.

Definition at line 813 of file qdatastream.cpp.

References len, and readBytes().

00814 {
00815     uint len = 0;
00816     return readBytes(s, len);
00817 }

Here is the call graph for this function:

QDataStream & QDataStream::operator<< ( qint8  i  ) 

Writes a signed byte, i, to the stream and returns a reference to the stream.

Definition at line 909 of file qdatastream.cpp.

References CHECK_STREAM_PRECOND, dev, and QIODevice::putChar().

00910 {
00911     CHECK_STREAM_PRECOND(*this)
00912     dev->putChar(i);
00913     return *this;
00914 }

Here is the call graph for this function:

QDataStream & QDataStream::operator<< ( quint8  i  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes an unsigned byte, i, to the stream and returns a reference to the stream.

Definition at line 194 of file qdatastream.h.

00195 { return *this << qint8(i); }

QDataStream & QDataStream::operator<< ( qint16  i  ) 

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 932 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, and QIODevice::write().

00933 {
00934     CHECK_STREAM_PRECOND(*this)
00935     if (noswap) {
00936         dev->write((char *)&i, sizeof(qint16));
00937     } else {                                        // swap bytes
00938         register uchar *p = (uchar *)(&i);
00939         char b[2];
00940         b[1] = *p++;
00941         b[0] = *p;
00942         dev->write(b, 2);
00943     }
00944     return *this;
00945 }

Here is the call graph for this function:

QDataStream & QDataStream::operator<< ( quint16  i  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes an unsigned 16-bit integer, i, to the stream and returns a reference to the stream.

Definition at line 197 of file qdatastream.h.

00198 { return *this << qint16(i); }

QDataStream & QDataStream::operator<< ( qint32  i  ) 

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 954 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, and QIODevice::write().

00955 {
00956     CHECK_STREAM_PRECOND(*this)
00957     if (noswap) {
00958         dev->write((char *)&i, sizeof(qint32));
00959     } else {                                        // swap bytes
00960         register uchar *p = (uchar *)(&i);
00961         char b[4];
00962         b[3] = *p++;
00963         b[2] = *p++;
00964         b[1] = *p++;
00965         b[0] = *p;
00966         dev->write(b, 4);
00967     }
00968     return *this;
00969 }

Here is the call graph for this function:

QDataStream & QDataStream::operator<< ( quint32  i  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes an unsigned integer, i, to the stream as a 32-bit unsigned integer (quint32). Returns a reference to the stream.

Definition at line 200 of file qdatastream.h.

00201 { return *this << qint32(i); }

QDataStream & QDataStream::operator<< ( qint64  i  ) 

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 64-bit integer, i, to the stream and returns a reference to the stream.

Definition at line 986 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, version(), and QIODevice::write().

00987 {
00988     CHECK_STREAM_PRECOND(*this)
00989     if (version() < 6) {
00990   quint32 i1 = i & 0xffffffff;
00991   quint32 i2 = i >> 32;
00992   *this << i2 << i1;
00993     } else if (noswap) {                        // no conversion needed
00994         dev->write((char *)&i, sizeof(qint64));
00995     } else {                                        // swap bytes
00996         register uchar *p = (uchar *)(&i);
00997         char b[8];
00998         b[7] = *p++;
00999         b[6] = *p++;
01000         b[5] = *p++;
01001         b[4] = *p++;
01002         b[3] = *p++;
01003         b[2] = *p++;
01004         b[1] = *p++;
01005         b[0] = *p;
01006         dev->write(b, 8);
01007     }
01008     return *this;
01009 }

Here is the call graph for this function:

QDataStream & QDataStream::operator<< ( quint64  i  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes an unsigned 64-bit integer, i, to the stream and returns a reference to the stream.

Definition at line 203 of file qdatastream.h.

00204 { return *this << qint64(i); }

QDataStream & QDataStream::operator<< ( bool  i  ) 

Writes a boolean value, i, to the stream. Returns a reference to the stream.

Definition at line 1024 of file qdatastream.cpp.

References CHECK_STREAM_PRECOND, dev, and QIODevice::putChar().

01025 {
01026     CHECK_STREAM_PRECOND(*this)
01027     dev->putChar(qint8(i));
01028     return *this;
01029 }

Here is the call graph for this function:

QDataStream & QDataStream::operator<< ( float  f  ) 

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 IEEE 754 format. Returns a reference to the stream.

Definition at line 1038 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, g, noswap, p, and QIODevice::write().

01039 {
01040     CHECK_STREAM_PRECOND(*this)
01041     float g = f;                                // fixes float-on-stack problem
01042     if (noswap) {                                // no conversion needed
01043         dev->write((char *)&g, sizeof(float));
01044     } else {                                // swap bytes
01045         register uchar *p = (uchar *)(&g);
01046         char b[4];
01047         b[3] = *p++;
01048         b[2] = *p++;
01049         b[1] = *p++;
01050         b[0] = *p;
01051         dev->write(b, 4);
01052     }
01053     return *this;
01054 }

Here is the call graph for this function:

QDataStream & QDataStream::operator<< ( double  f  ) 

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 IEEE 754 format. Returns a reference to the stream.

Definition at line 1064 of file qdatastream.cpp.

References b, CHECK_STREAM_PRECOND, dev, noswap, p, and QIODevice::write().

01065 {
01066     CHECK_STREAM_PRECOND(*this)
01067     if (noswap) {
01068         dev->write((char *)&f, sizeof(double));
01069     } else {
01070         register uchar *p = (uchar *)(&f);
01071         char b[8];
01072         b[7] = *p++;
01073         b[6] = *p++;
01074         b[5] = *p++;
01075         b[4] = *p++;
01076         b[3] = *p++;
01077         b[2] = *p++;
01078         b[1] = *p++;
01079         b[0] = *p;
01080         dev->write(b, 8);
01081     }
01082     return *this;
01083 }

Here is the call graph for this function:

QDataStream & QDataStream::operator<< ( const char *  s  ) 

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 1095 of file qdatastream.cpp.

References len, qstrlen(), and writeRawData().

01096 {
01097     if (!s) {
01098         *this << (quint32)0;
01099         return *this;
01100     }
01101     uint len = qstrlen(s) + 1;                        // also write null terminator
01102     *this << (quint32)len;                        // write length specifier
01103     writeRawData(s, len);
01104     return *this;
01105 }

Here is the call graph for this function:

QDataStream & QDataStream::readBytes ( char *&  s,
uint &  l 
)

Reads the buffer s from the stream and returns a reference to the stream.

The buffer s is allocated using new. Destroy it with the delete[] operator.

The l parameter is set to the length of the buffer. If the string read is empty, l is set to 0 and s is set to a null pointer.

The serialization format is a quint32 length specifier first, then l bytes of data.

See also:
readRawData(), writeBytes()

Definition at line 837 of file qdatastream.cpp.

References CHECK_STREAM_PRECOND, dev, len, qMin(), QIODevice::read(), ReadPastEnd, and setStatus().

Referenced by operator>>().

00838 {
00839     s = 0;
00840     l = 0;
00841     CHECK_STREAM_PRECOND(*this)
00842 
00843     quint32 len;
00844     *this >> len;
00845     if (len == 0)
00846         return *this;
00847 
00848     const quint32 Step = 1024 * 1024;
00849     quint32 allocated = 0;
00850     char *prevBuf = 0;
00851     char *curBuf = 0;
00852 
00853     do {
00854         int blockSize = qMin(Step, len - allocated);
00855         prevBuf = curBuf;
00856         curBuf = new char[allocated + blockSize + 1];
00857         if (prevBuf) {
00858             memcpy(curBuf, prevBuf, allocated);
00859             delete [] prevBuf;
00860         }
00861         if (dev->read(curBuf + allocated, blockSize) != blockSize) {
00862             delete [] curBuf;
00863             setStatus(ReadPastEnd);
00864             return *this;
00865         }
00866         allocated += blockSize;
00867     } while (allocated < len);
00868 
00869     s = curBuf;
00870     s[len] = '\0';
00871     l = (uint)len;
00872     return *this;
00873 }

Here is the call graph for this function:

int QDataStream::readRawData ( char *  s,
int  len 
)

Reads at most len bytes from the stream into s and returns the number of bytes read. If an error occurs, this function returns -1.

The buffer s must be preallocated. The data is not encoded.

See also:
readBytes(), QIODevice::read(), writeRawData()

Definition at line 884 of file qdatastream.cpp.

References CHECK_STREAM_PRECOND, dev, and QIODevice::read().

Referenced by QByteArray::operator>>(), and operator>>().

00885 {
00886     CHECK_STREAM_PRECOND(-1)
00887     return dev->read(s, len);
00888 }

Here is the call graph for this function:

QDataStream & QDataStream::writeBytes ( const char *  s,
uint  len 
)

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 quint32, followed by len bytes from s. Note that the data is not encoded.

See also:
writeRawData(), readBytes()

Definition at line 1118 of file qdatastream.cpp.

References CHECK_STREAM_PRECOND, and writeRawData().

Referenced by QByteArray::operator<<(), and operator<<().

01119 {
01120     CHECK_STREAM_PRECOND(*this)
01121     *this << (quint32)len;                        // write length specifier
01122     if (len)
01123         writeRawData(s, len);
01124     return *this;
01125 }

Here is the call graph for this function:

int QDataStream::writeRawData ( const char *  s,
int  len 
)

Writes len bytes from s to the stream. Returns the number of bytes actually written, or -1 on error. The data is not encoded.

See also:
writeBytes(), QIODevice::write(), readRawData()

Definition at line 1136 of file qdatastream.cpp.

References CHECK_STREAM_PRECOND, dev, and QIODevice::write().

Referenced by operator<<(), writeBytes(), QPdfEnginePrivate::writeCompressed(), and QPdfEnginePrivate::xprintf().

01137 {
01138     CHECK_STREAM_PRECOND(-1)
01139     return dev->write(s, len);
01140 }

Here is the call graph for this function:

int QDataStream::skipRawData ( int  len  ) 

Since:
4.1
Skips len bytes from the device. Returns the number of bytes actually skipped, or -1 on error.

This is equivalent to calling readRawData() on a buffer of length len and ignoring the buffer.

See also:
QIODevice::seek()

Definition at line 1153 of file qdatastream.cpp.

References buf, CHECK_STREAM_PRECOND, dev, QIODevice::isSequential(), n, QIODevice::pos(), qMin(), QIODevice::read(), QIODevice::seek(), and QIODevice::size().

01154 {
01155     CHECK_STREAM_PRECOND(-1)
01156 
01157     if (dev->isSequential()) {
01158         char buf[4096];
01159         int sumRead = 0;
01160 
01161         while (len > 0) {
01162             int blockSize = qMin(len, (int)sizeof(buf));
01163             int n = dev->read(buf, blockSize);
01164             if (n == -1)
01165                 return -1;
01166             if (n == 0)
01167                 return sumRead;
01168 
01169             sumRead += n;
01170             len -= blockSize;
01171         }
01172         return sumRead;
01173     } else {
01174         quint64 pos = dev->pos();
01175         len = qMin(int(dev->size() - pos), len);
01176         if (!dev->seek(pos + len))
01177             return -1;
01178         return len;
01179     }
01180 }

Here is the call graph for this function:


Member Data Documentation

QDataStreamPrivate* QDataStream::d [private]

Definition at line 155 of file qdatastream.h.

Referenced by QDataStream(), and setDevice().

QIODevice* QDataStream::dev [private]

Definition at line 157 of file qdatastream.h.

Referenced by atEnd(), operator<<(), operator>>(), QDataStream(), readBytes(), readRawData(), setDevice(), skipRawData(), writeRawData(), and ~QDataStream().

bool QDataStream::owndev [private]

Definition at line 158 of file qdatastream.h.

Referenced by QDataStream(), setDevice(), and ~QDataStream().

bool QDataStream::noswap [private]

Definition at line 159 of file qdatastream.h.

Referenced by operator<<(), operator>>(), QDataStream(), and setByteOrder().

ByteOrder QDataStream::byteorder [private]

Definition at line 160 of file qdatastream.h.

Referenced by byteOrder(), QDataStream(), and setByteOrder().

int QDataStream::ver [private]

Definition at line 161 of file qdatastream.h.

Referenced by QDataStream(), setVersion(), and version().

Status QDataStream::q_status [private]

Definition at line 162 of file qdatastream.h.

Referenced by QDataStream(), resetStatus(), setStatus(), and status().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 17:06:40 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1