1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdatastream.h"
43 #include "qdatastream_p.h"
45 #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
58 \brief The QDataStream class provides serialization of binary data
64 A data stream is a binary stream of encoded information which is
65 100% independent of the host computer's operating system, CPU or
66 byte order. For example, a data stream that is written by a PC
67 under Windows can be read by a Sun SPARC running Solaris.
69 You can also use a data stream to read/write \l{raw}{raw
70 unencoded binary data}. If you want a "parsing" input stream, see
73 The QDataStream class implements the serialization of C++'s basic
74 data types, like \c char, \c short, \c int, \c{char *}, etc.
75 Serialization of more complex data is accomplished by breaking up
76 the data into primitive units.
78 A data stream cooperates closely with a QIODevice. A QIODevice
79 represents an input/output medium one can read data from and write
80 data to. The QFile class is an example of an I/O device.
82 Example (write binary data to a stream):
84 \snippet code/src_corelib_io_qdatastream.cpp 0
86 Example (read binary data from a stream):
88 \snippet code/src_corelib_io_qdatastream.cpp 1
90 Each item written to the stream is written in a predefined binary
91 format that varies depending on the item's type. Supported Qt
92 types include QBrush, QColor, QDateTime, QFont, QPixmap, QString,
93 QVariant and many others. For the complete list of all Qt types
94 supporting data streaming see \l{Serializing Qt Data Types}.
96 For integers it is best to always cast to a Qt integer type for
97 writing, and to read back into the same Qt integer type. This
98 ensures that you get integers of the size you want and insulates
99 you from compiler and platform differences.
101 To take one example, a \c{char *} string is written as a 32-bit
102 integer equal to the length of the string including the '\\0' byte,
103 followed by all the characters of the string including the
104 '\\0' byte. When reading a \c{char *} string, 4 bytes are read to
105 create the 32-bit length value, then that many characters for the
106 \c {char *} string including the '\\0' terminator are read.
108 The initial I/O device is usually set in the constructor, but can be
109 changed with setDevice(). If you've reached the end of the data
110 (or if there is no I/O device set) atEnd() will return true.
114 QDataStream's binary format has evolved since Qt 1.0, and is
115 likely to continue evolving to reflect changes done in Qt. When
116 inputting or outputting complex types, it's very important to
117 make sure that the same version of the stream (version()) is used
118 for reading and writing. If you need both forward and backward
119 compatibility, you can hardcode the version number in the
122 \snippet code/src_corelib_io_qdatastream.cpp 2
124 If you are producing a new binary data format, such as a file
125 format for documents created by your application, you could use a
126 QDataStream to write the data in a portable format. Typically, you
127 would write a brief header containing a magic string and a version
128 number to give yourself room for future expansion. For example:
130 \snippet code/src_corelib_io_qdatastream.cpp 3
132 Then read it in with:
134 \snippet code/src_corelib_io_qdatastream.cpp 4
136 You can select which byte order to use when serializing data. The
137 default setting is big endian (MSB first). Changing it to little
138 endian breaks the portability (unless the reader also changes to
139 little endian). We recommend keeping this setting unless you have
140 special requirements.
143 \section1 Reading and writing raw binary data
145 You may wish to read/write your own raw binary data to/from the
146 data stream directly. Data may be read from the stream into a
147 preallocated \c{char *} using readRawData(). Similarly data can be
148 written to the stream using writeRawData(). Note that any
149 encoding/decoding of the data must be done by you.
151 A similar pair of functions is readBytes() and writeBytes(). These
152 differ from their \e raw counterparts as follows: readBytes()
153 reads a quint32 which is taken to be the length of the data to be
154 read, then that number of bytes is read into the preallocated
155 \c{char *}; writeBytes() writes a quint32 containing the length of the
156 data, followed by the data. Note that any encoding/decoding of
157 the data (apart from the length quint32) must be done by you.
159 \section1 Reading and writing Qt collection classes
161 The Qt container classes can also be serialized to a QDataStream.
162 These include QList, QLinkedList, QVector, QSet, QHash, and QMap.
163 The stream operators are declared as non-members of the classes.
165 \target Serializing Qt Classes
166 \section1 Reading and writing other Qt classes.
168 In addition to the overloaded stream operators documented here,
169 any Qt classes that you might want to serialize to a QDataStream
170 will have appropriate stream operators declared as non-member of
174 QDataStream &operator<<(QDataStream &, const QXxx &);
175 QDataStream &operator>>(QDataStream &, QXxx &);
178 For example, here are the stream operators declared as non-members
182 QDataStream & operator<< (QDataStream& stream, const QImage& image);
183 QDataStream & operator>> (QDataStream& stream, QImage& image);
186 To see if your favorite Qt class has similar stream operators
187 defined, check the \b {Related Non-Members} section of the
188 class's documentation page.
190 \sa QTextStream, QVariant
194 \enum QDataStream::ByteOrder
196 The byte order used for reading/writing the data.
198 \value BigEndian Most significant byte first (the default)
199 \value LittleEndian Least significant byte first
203 \enum QDataStream::FloatingPointPrecision
205 The precision of floating point numbers used for reading/writing the data. This will only have
206 an effect if the version of the data stream is Qt_4_6 or higher.
208 \warning The floating point precision must be set to the same value on the object that writes
209 and the object that reads the data stream.
211 \value SinglePrecision All floating point numbers in the data stream have 32-bit precision.
212 \value DoublePrecision All floating point numbers in the data stream have 64-bit precision.
214 \sa setFloatingPointPrecision(), floatingPointPrecision()
218 \enum QDataStream::Status
220 This enum describes the current status of the data stream.
222 \value Ok The data stream is operating normally.
223 \value ReadPastEnd The data stream has read past the end of the
224 data in the underlying device.
225 \value ReadCorruptData The data stream has read corrupt data.
226 \value WriteFailed The data stream cannot write to the underlying device.
229 /*****************************************************************************
230 QDataStream member functions
231 *****************************************************************************/
233 #undef CHECK_STREAM_PRECOND
235 #define CHECK_STREAM_PRECOND(retVal) \
237 qWarning("QDataStream: No device"); \
241 #define CHECK_STREAM_PRECOND(retVal) \
247 #define CHECK_STREAM_WRITE_PRECOND(retVal) \
248 CHECK_STREAM_PRECOND(retVal) \
249 if (q_status != Ok) \
253 DefaultStreamVersion = QDataStream::Qt_5_0
256 // ### 5.0: when streaming invalid QVariants, just the type should
257 // be written, no "data" after it
260 Constructs a data stream that has no I/O device.
265 QDataStream::QDataStream()
269 byteorder = BigEndian;
270 ver = DefaultStreamVersion;
271 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
276 Constructs a data stream that uses the I/O device \a d.
278 \warning If you use QSocket or QSocketDevice as the I/O device \a d
279 for reading data, you must make sure that enough data is available
280 on the socket for the operation to successfully proceed;
281 QDataStream does not have any means to handle or recover from
284 \sa setDevice(), device()
287 QDataStream::QDataStream(QIODevice *d)
289 dev = d; // set device
291 byteorder = BigEndian; // default byte order
292 ver = DefaultStreamVersion;
293 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
298 \fn QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode mode)
300 Constructs a data stream that operates on a byte array, \a a. The
301 \a mode describes how the device is to be used.
303 Alternatively, you can use QDataStream(const QByteArray &) if you
304 just want to read from a byte array.
306 Since QByteArray is not a QIODevice subclass, internally a QBuffer
307 is created to wrap the byte array.
310 QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode flags)
312 QBuffer *buf = new QBuffer(a);
313 #ifndef QT_NO_QOBJECT
314 buf->blockSignals(true);
319 byteorder = BigEndian;
320 ver = DefaultStreamVersion;
321 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
326 Constructs a read-only data stream that operates on byte array \a a.
327 Use QDataStream(QByteArray*, int) if you want to write to a byte
330 Since QByteArray is not a QIODevice subclass, internally a QBuffer
331 is created to wrap the byte array.
333 QDataStream::QDataStream(const QByteArray &a)
335 QBuffer *buf = new QBuffer;
336 #ifndef QT_NO_QOBJECT
337 buf->blockSignals(true);
340 buf->open(QIODevice::ReadOnly);
343 byteorder = BigEndian;
344 ver = DefaultStreamVersion;
345 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
350 Destroys the data stream.
352 The destructor will not affect the current I/O device, unless it is
353 an internal I/O device (e.g. a QBuffer) processing a QByteArray
354 passed in the \e constructor, in which case the internal I/O device
358 QDataStream::~QDataStream()
366 \fn QIODevice *QDataStream::device() const
368 Returns the I/O device currently set, or 0 if no
369 device is currently set.
375 void QDataStream::setDevice(QIODevice *d)
377 Sets the I/O device to \a d, which can be 0
378 to unset to current I/O device.
383 void QDataStream::setDevice(QIODevice *d)
394 Unsets the I/O device.
395 Use setDevice(0) instead.
398 void QDataStream::unsetDevice()
405 \fn bool QDataStream::atEnd() const
407 Returns true if the I/O device has reached the end position (end of
408 the stream or file) or if there is no I/O device set; otherwise
411 \sa QIODevice::atEnd()
414 bool QDataStream::atEnd() const
416 return dev ? dev->atEnd() : true;
420 Returns the floating point precision of the data stream.
424 \sa FloatingPointPrecision, setFloatingPointPrecision()
426 QDataStream::FloatingPointPrecision QDataStream::floatingPointPrecision() const
428 return d == 0 ? QDataStream::DoublePrecision : d->floatingPointPrecision;
432 Sets the floating point precision of the data stream to \a precision. If the floating point precision is
433 DoublePrecision and the version of the data stream is Qt_4_6 or higher, all floating point
434 numbers will be written and read with 64-bit precision. If the floating point precision is
435 SinglePrecision and the version is Qt_4_6 or higher, all floating point numbers will be written
436 and read with 32-bit precision.
438 For versions prior to Qt_4_6, the precision of floating point numbers in the data stream depends
439 on the stream operator called.
441 The default is DoublePrecision.
443 \warning This property must be set to the same value on the object that writes and the object
444 that reads the data stream.
448 void QDataStream::setFloatingPointPrecision(QDataStream::FloatingPointPrecision precision)
451 d.reset(new QDataStreamPrivate());
452 d->floatingPointPrecision = precision;
456 Returns the status of the data stream.
458 \sa Status, setStatus(), resetStatus()
461 QDataStream::Status QDataStream::status() const
467 Resets the status of the data stream.
469 \sa Status, status(), setStatus()
471 void QDataStream::resetStatus()
477 Sets the status of the data stream to the \a status given.
479 Subsequent calls to setStatus() are ignored until resetStatus()
482 \sa Status, status(), resetStatus()
484 void QDataStream::setStatus(Status status)
491 \fn int QDataStream::byteOrder() const
493 Returns the current byte order setting -- either BigEndian or
500 Sets the serialization byte order to \a bo.
502 The \a bo parameter can be QDataStream::BigEndian or
503 QDataStream::LittleEndian.
505 The default setting is big endian. We recommend leaving this
506 setting unless you have special requirements.
511 void QDataStream::setByteOrder(ByteOrder bo)
514 if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
515 noswap = (byteorder == BigEndian);
517 noswap = (byteorder == LittleEndian);
522 \enum QDataStream::Version
524 This enum provides symbolic synonyms for the data serialization
525 format version numbers.
527 \value Qt_1_0 Version 1 (Qt 1.x)
528 \value Qt_2_0 Version 2 (Qt 2.0)
529 \value Qt_2_1 Version 3 (Qt 2.1, 2.2, 2.3)
530 \value Qt_3_0 Version 4 (Qt 3.0)
531 \value Qt_3_1 Version 5 (Qt 3.1, 3.2)
532 \value Qt_3_3 Version 6 (Qt 3.3)
533 \value Qt_4_0 Version 7 (Qt 4.0, Qt 4.1)
534 \value Qt_4_1 Version 7 (Qt 4.0, Qt 4.1)
535 \value Qt_4_2 Version 8 (Qt 4.2)
536 \value Qt_4_3 Version 9 (Qt 4.3)
537 \value Qt_4_4 Version 10 (Qt 4.4)
538 \value Qt_4_5 Version 11 (Qt 4.5)
539 \value Qt_4_6 Version 12 (Qt 4.6, Qt 4.7, Qt 4.8)
540 \value Qt_4_7 Same as Qt_4_6.
541 \value Qt_4_8 Same as Qt_4_6.
542 \value Qt_4_9 Same as Qt_4_6.
543 \value Qt_5_0 Same as Qt_4_6.
545 \sa setVersion(), version()
549 \fn int QDataStream::version() const
551 Returns the version number of the data serialization format.
553 \sa setVersion(), Version
557 \fn void QDataStream::setVersion(int v)
559 Sets the version number of the data serialization format to \a v.
561 You don't \e have to set a version if you are using the current
562 version of Qt, but for your own custom binary formats we
563 recommend that you do; see \l{Versioning} in the Detailed
566 To accommodate new functionality, the datastream serialization
567 format of some Qt classes has changed in some versions of Qt. If
568 you want to read data that was created by an earlier version of
569 Qt, or write data that can be read by a program that was compiled
570 with an earlier version of Qt, use this function to modify the
571 serialization format used by QDataStream.
574 \header \li Qt Version \li QDataStream Version
575 \row \li Qt 4.6 \li 12
576 \row \li Qt 4.5 \li 11
577 \row \li Qt 4.4 \li 10
578 \row \li Qt 4.3 \li 9
579 \row \li Qt 4.2 \li 8
580 \row \li Qt 4.0, 4.1 \li 7
581 \row \li Qt 3.3 \li 6
582 \row \li Qt 3.1, 3.2 \li 5
583 \row \li Qt 3.0 \li 4
584 \row \li Qt 2.1, 2.2, 2.3 \li 3
585 \row \li Qt 2.0 \li 2
586 \row \li Qt 1.x \li 1
589 The \l Version enum provides symbolic constants for the different
590 versions of Qt. For example:
592 \snippet code/src_corelib_io_qdatastream.cpp 5
594 \sa version(), Version
597 /*****************************************************************************
598 QDataStream read functions
599 *****************************************************************************/
602 \fn QDataStream &QDataStream::operator>>(quint8 &i)
605 Reads an unsigned byte from the stream into \a i, and returns a
606 reference to the stream.
610 Reads a signed byte from the stream into \a i, and returns a
611 reference to the stream.
614 QDataStream &QDataStream::operator>>(qint8 &i)
617 CHECK_STREAM_PRECOND(*this)
619 if (!dev->getChar(&c))
620 setStatus(ReadPastEnd);
628 \fn QDataStream &QDataStream::operator>>(quint16 &i)
631 Reads an unsigned 16-bit integer from the stream into \a i, and
632 returns a reference to the stream.
638 Reads a signed 16-bit integer from the stream into \a i, and
639 returns a reference to the stream.
642 QDataStream &QDataStream::operator>>(qint16 &i)
645 CHECK_STREAM_PRECOND(*this)
646 if (dev->read((char *)&i, 2) != 2) {
648 setStatus(ReadPastEnd);
659 \fn QDataStream &QDataStream::operator>>(quint32 &i)
662 Reads an unsigned 32-bit integer from the stream into \a i, and
663 returns a reference to the stream.
669 Reads a signed 32-bit integer from the stream into \a i, and
670 returns a reference to the stream.
673 QDataStream &QDataStream::operator>>(qint32 &i)
676 CHECK_STREAM_PRECOND(*this)
677 if (dev->read((char *)&i, 4) != 4) {
679 setStatus(ReadPastEnd);
689 \fn QDataStream &QDataStream::operator>>(quint64 &i)
692 Reads an unsigned 64-bit integer from the stream, into \a i, and
693 returns a reference to the stream.
699 Reads a signed 64-bit integer from the stream into \a i, and
700 returns a reference to the stream.
703 QDataStream &QDataStream::operator>>(qint64 &i)
706 CHECK_STREAM_PRECOND(*this)
710 i = ((quint64)i1 << 32) + i2;
712 if (dev->read((char *)&i, 8) != 8) {
714 setStatus(ReadPastEnd);
725 Reads a boolean value from the stream into \a i. Returns a
726 reference to the stream.
728 QDataStream &QDataStream::operator>>(bool &i)
739 Reads a floating point number from the stream into \a f,
740 using the standard IEEE 754 format. Returns a reference to the
743 \sa setFloatingPointPrecision()
746 QDataStream &QDataStream::operator>>(float &f)
748 if (version() >= QDataStream::Qt_4_6
749 && floatingPointPrecision() == QDataStream::DoublePrecision) {
757 CHECK_STREAM_PRECOND(*this)
758 if (dev->read((char *)&f, 4) != 4) {
760 setStatus(ReadPastEnd);
767 x.val2 = qbswap(*reinterpret_cast<quint32 *>(&f));
777 Reads a floating point number from the stream into \a f,
778 using the standard IEEE 754 format. Returns a reference to the
781 \sa setFloatingPointPrecision()
784 QDataStream &QDataStream::operator>>(double &f)
786 if (version() >= QDataStream::Qt_4_6
787 && floatingPointPrecision() == QDataStream::SinglePrecision) {
795 CHECK_STREAM_PRECOND(*this)
796 if (dev->read((char *)&f, 8) != 8) {
798 setStatus(ReadPastEnd);
805 x.val2 = qbswap(*reinterpret_cast<quint64 *>(&f));
816 Reads the '\\0'-terminated string \a s from the stream and returns
817 a reference to the stream.
819 Space for the string is allocated using \c new -- the caller must
820 destroy it with \c{delete[]}.
823 QDataStream &QDataStream::operator>>(char *&s)
826 return readBytes(s, len);
831 Reads the buffer \a s from the stream and returns a reference to
834 The buffer \a s is allocated using \c new. Destroy it with the \c
837 The \a l parameter is set to the length of the buffer. If the
838 string read is empty, \a l is set to 0 and \a s is set to
841 The serialization format is a quint32 length specifier first,
842 then \a l bytes of data.
844 \sa readRawData(), writeBytes()
847 QDataStream &QDataStream::readBytes(char *&s, uint &l)
851 CHECK_STREAM_PRECOND(*this)
858 const quint32 Step = 1024 * 1024;
859 quint32 allocated = 0;
864 int blockSize = qMin(Step, len - allocated);
866 curBuf = new char[allocated + blockSize + 1];
868 memcpy(curBuf, prevBuf, allocated);
871 if (dev->read(curBuf + allocated, blockSize) != blockSize) {
873 setStatus(ReadPastEnd);
876 allocated += blockSize;
877 } while (allocated < len);
886 Reads at most \a len bytes from the stream into \a s and returns the number of
887 bytes read. If an error occurs, this function returns -1.
889 The buffer \a s must be preallocated. The data is \e not encoded.
891 \sa readBytes(), QIODevice::read(), writeRawData()
894 int QDataStream::readRawData(char *s, int len)
896 CHECK_STREAM_PRECOND(-1)
897 return dev->read(s, len);
901 /*****************************************************************************
902 QDataStream write functions
903 *****************************************************************************/
907 \fn QDataStream &QDataStream::operator<<(quint8 i)
910 Writes an unsigned byte, \a i, to the stream and returns a
911 reference to the stream.
915 Writes a signed byte, \a i, to the stream and returns a reference
919 QDataStream &QDataStream::operator<<(qint8 i)
921 CHECK_STREAM_WRITE_PRECOND(*this)
922 if (!dev->putChar(i))
923 q_status = WriteFailed;
929 \fn QDataStream &QDataStream::operator<<(quint16 i)
932 Writes an unsigned 16-bit integer, \a i, to the stream and returns
933 a reference to the stream.
939 Writes a signed 16-bit integer, \a i, to the stream and returns a
940 reference to the stream.
943 QDataStream &QDataStream::operator<<(qint16 i)
945 CHECK_STREAM_WRITE_PRECOND(*this)
949 if (dev->write((char *)&i, sizeof(qint16)) != sizeof(qint16))
950 q_status = WriteFailed;
957 Writes a signed 32-bit integer, \a i, to the stream and returns a
958 reference to the stream.
961 QDataStream &QDataStream::operator<<(qint32 i)
963 CHECK_STREAM_WRITE_PRECOND(*this)
967 if (dev->write((char *)&i, sizeof(qint32)) != sizeof(qint32))
968 q_status = WriteFailed;
973 \fn QDataStream &QDataStream::operator<<(quint64 i)
976 Writes an unsigned 64-bit integer, \a i, to the stream and returns a
977 reference to the stream.
983 Writes a signed 64-bit integer, \a i, to the stream and returns a
984 reference to the stream.
987 QDataStream &QDataStream::operator<<(qint64 i)
989 CHECK_STREAM_WRITE_PRECOND(*this)
991 quint32 i1 = i & 0xffffffff;
992 quint32 i2 = i >> 32;
998 if (dev->write((char *)&i, sizeof(qint64)) != sizeof(qint64))
999 q_status = WriteFailed;
1005 \fn QDataStream &QDataStream::operator<<(quint32 i)
1008 Writes an unsigned integer, \a i, to the stream as a 32-bit
1009 unsigned integer (quint32). Returns a reference to the stream.
1013 Writes a boolean value, \a i, to the stream. Returns a reference
1017 QDataStream &QDataStream::operator<<(bool i)
1019 CHECK_STREAM_WRITE_PRECOND(*this)
1020 if (!dev->putChar(qint8(i)))
1021 q_status = WriteFailed;
1028 Writes a floating point number, \a f, to the stream using
1029 the standard IEEE 754 format. Returns a reference to the stream.
1031 \sa setFloatingPointPrecision()
1034 QDataStream &QDataStream::operator<<(float f)
1036 if (version() >= QDataStream::Qt_4_6
1037 && floatingPointPrecision() == QDataStream::DoublePrecision) {
1042 CHECK_STREAM_WRITE_PRECOND(*this)
1043 float g = f; // fixes float-on-stack problem
1050 x.val2 = qbswap(x.val2);
1053 if (dev->write((char *)&g, sizeof(float)) != sizeof(float))
1054 q_status = WriteFailed;
1062 Writes a floating point number, \a f, to the stream using
1063 the standard IEEE 754 format. Returns a reference to the stream.
1065 \sa setFloatingPointPrecision()
1068 QDataStream &QDataStream::operator<<(double f)
1070 if (version() >= QDataStream::Qt_4_6
1071 && floatingPointPrecision() == QDataStream::SinglePrecision) {
1076 CHECK_STREAM_WRITE_PRECOND(*this)
1078 if (dev->write((char *)&f, sizeof(double)) != sizeof(double))
1079 q_status = WriteFailed;
1086 x.val2 = qbswap(x.val2);
1087 if (dev->write((char *)&x.val2, sizeof(double)) != sizeof(double))
1088 q_status = WriteFailed;
1097 Writes the '\\0'-terminated string \a s to the stream and returns a
1098 reference to the stream.
1100 The string is serialized using writeBytes().
1103 QDataStream &QDataStream::operator<<(const char *s)
1106 *this << (quint32)0;
1109 uint len = qstrlen(s) + 1; // also write null terminator
1110 *this << (quint32)len; // write length specifier
1111 writeRawData(s, len);
1117 Writes the length specifier \a len and the buffer \a s to the
1118 stream and returns a reference to the stream.
1120 The \a len is serialized as a quint32, followed by \a len bytes
1121 from \a s. Note that the data is \e not encoded.
1123 \sa writeRawData(), readBytes()
1126 QDataStream &QDataStream::writeBytes(const char *s, uint len)
1128 CHECK_STREAM_WRITE_PRECOND(*this)
1129 *this << (quint32)len; // write length specifier
1131 writeRawData(s, len);
1137 Writes \a len bytes from \a s to the stream. Returns the
1138 number of bytes actually written, or -1 on error.
1139 The data is \e not encoded.
1141 \sa writeBytes(), QIODevice::write(), readRawData()
1144 int QDataStream::writeRawData(const char *s, int len)
1146 CHECK_STREAM_WRITE_PRECOND(-1)
1147 int ret = dev->write(s, len);
1149 q_status = WriteFailed;
1156 Skips \a len bytes from the device. Returns the number of bytes
1157 actually skipped, or -1 on error.
1159 This is equivalent to calling readRawData() on a buffer of length
1160 \a len and ignoring the buffer.
1162 \sa QIODevice::seek()
1164 int QDataStream::skipRawData(int len)
1166 CHECK_STREAM_PRECOND(-1)
1168 if (dev->isSequential()) {
1173 int blockSize = qMin(len, (int)sizeof(buf));
1174 int n = dev->read(buf, blockSize);
1185 qint64 pos = dev->pos();
1186 qint64 size = dev->size();
1187 if (pos + len > size)
1189 if (!dev->seek(pos + len))
1197 #endif // QT_NO_DATASTREAM