1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qdatastream.h"
43 #include "qdatastream_p.h"
45 #ifndef QT_NO_DATASTREAM
57 \brief The QDataStream class provides serialization of binary data
63 A data stream is a binary stream of encoded information which is
64 100% independent of the host computer's operating system, CPU or
65 byte order. For example, a data stream that is written by a PC
66 under Windows can be read by a Sun SPARC running Solaris.
68 You can also use a data stream to read/write \l{raw}{raw
69 unencoded binary data}. If you want a "parsing" input stream, see
72 The QDataStream class implements the serialization of C++'s basic
73 data types, like \c char, \c short, \c int, \c{char *}, etc.
74 Serialization of more complex data is accomplished by breaking up
75 the data into primitive units.
77 A data stream cooperates closely with a QIODevice. A QIODevice
78 represents an input/output medium one can read data from and write
79 data to. The QFile class is an example of an I/O device.
81 Example (write binary data to a stream):
83 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 0
85 Example (read binary data from a stream):
87 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 1
89 Each item written to the stream is written in a predefined binary
90 format that varies depending on the item's type. Supported Qt
91 types include QBrush, QColor, QDateTime, QFont, QPixmap, QString,
92 QVariant and many others. For the complete list of all Qt types
93 supporting data streaming see the \l{Format of the QDataStream
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 doc/src/snippets/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 doc/src/snippets/code/src_corelib_io_qdatastream.cpp 3
132 Then read it in with:
134 \snippet doc/src/snippets/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 \target Serializing Qt Classes
160 \section1 Reading and writing other Qt classes.
162 In addition to the overloaded stream operators documented here,
163 any Qt classes that you might want to serialize to a QDataStream
164 will have appropriate stream operators declared as non-member of
168 QDataStream &operator<<(QDataStream &, const QXxx &);
169 QDataStream &operator>>(QDataStream &, QXxx &);
172 For example, here are the stream operators declared as non-members
176 QDataStream & operator<< (QDataStream& stream, const QImage& image);
177 QDataStream & operator>> (QDataStream& stream, QImage& image);
180 To see if your favorite Qt class has similar stream operators
181 defined, check the \bold {Related Non-Members} section of the
182 class's documentation page.
184 \sa QTextStream QVariant
188 \enum QDataStream::ByteOrder
190 The byte order used for reading/writing the data.
192 \value BigEndian Most significant byte first (the default)
193 \value LittleEndian Least significant byte first
197 \enum QDataStream::FloatingPointPrecision
199 The precision of floating point numbers used for reading/writing the data. This will only have
200 an effect if the version of the data stream is Qt_4_6 or higher.
202 \warning The floating point precision must be set to the same value on the object that writes
203 and the object that reads the data stream.
205 \value SinglePrecision All floating point numbers in the data stream have 32-bit precision.
206 \value DoublePrecision All floating point numbers in the data stream have 64-bit precision.
208 \sa setFloatingPointPrecision(), floatingPointPrecision()
212 \enum QDataStream::Status
214 This enum describes the current status of the data stream.
216 \value Ok The data stream is operating normally.
217 \value ReadPastEnd The data stream has read past the end of the
218 data in the underlying device.
219 \value ReadCorruptData The data stream has read corrupt data.
222 /*****************************************************************************
223 QDataStream member functions
224 *****************************************************************************/
226 #undef CHECK_STREAM_PRECOND
228 #define CHECK_STREAM_PRECOND(retVal) \
230 qWarning("QDataStream: No device"); \
234 #define CHECK_STREAM_PRECOND(retVal) \
241 DefaultStreamVersion
= QDataStream::Qt_4_6
244 // ### 5.0: when streaming invalid QVariants, just the type should
245 // be written, no "data" after it
248 Constructs a data stream that has no I/O device.
253 QDataStream::QDataStream()
257 byteorder
= BigEndian
;
258 ver
= DefaultStreamVersion
;
259 noswap
= QSysInfo::ByteOrder
== QSysInfo::BigEndian
;
264 Constructs a data stream that uses the I/O device \a d.
266 \warning If you use QSocket or QSocketDevice as the I/O device \a d
267 for reading data, you must make sure that enough data is available
268 on the socket for the operation to successfully proceed;
269 QDataStream does not have any means to handle or recover from
272 \sa setDevice(), device()
275 QDataStream::QDataStream(QIODevice
*d
)
277 dev
= d
; // set device
279 byteorder
= BigEndian
; // default byte order
280 ver
= DefaultStreamVersion
;
281 noswap
= QSysInfo::ByteOrder
== QSysInfo::BigEndian
;
287 \fn QDataStream::QDataStream(QByteArray *array, int mode)
290 Constructs a data stream that operates on the given \a array. The
291 \a mode specifies how the byte array is to be used, and is
292 usually either QIODevice::ReadOnly or QIODevice::WriteOnly.
294 QDataStream::QDataStream(QByteArray
*a
, int mode
)
296 QBuffer
*buf
= new QBuffer(a
);
297 #ifndef QT_NO_QOBJECT
298 buf
->blockSignals(true);
300 buf
->open(QIODevice::OpenMode(mode
));
303 byteorder
= BigEndian
;
304 ver
= DefaultStreamVersion
;
305 noswap
= QSysInfo::ByteOrder
== QSysInfo::BigEndian
;
311 \fn QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode mode)
313 Constructs a data stream that operates on a byte array, \a a. The
314 \a mode describes how the device is to be used.
316 Alternatively, you can use QDataStream(const QByteArray &) if you
317 just want to read from a byte array.
319 Since QByteArray is not a QIODevice subclass, internally a QBuffer
320 is created to wrap the byte array.
323 QDataStream::QDataStream(QByteArray
*a
, QIODevice::OpenMode flags
)
325 QBuffer
*buf
= new QBuffer(a
);
326 #ifndef QT_NO_QOBJECT
327 buf
->blockSignals(true);
332 byteorder
= BigEndian
;
333 ver
= DefaultStreamVersion
;
334 noswap
= QSysInfo::ByteOrder
== QSysInfo::BigEndian
;
339 Constructs a read-only data stream that operates on byte array \a a.
340 Use QDataStream(QByteArray*, int) if you want to write to a byte
343 Since QByteArray is not a QIODevice subclass, internally a QBuffer
344 is created to wrap the byte array.
346 QDataStream::QDataStream(const QByteArray
&a
)
348 QBuffer
*buf
= new QBuffer
;
349 #ifndef QT_NO_QOBJECT
350 buf
->blockSignals(true);
353 buf
->open(QIODevice::ReadOnly
);
356 byteorder
= BigEndian
;
357 ver
= DefaultStreamVersion
;
358 noswap
= QSysInfo::ByteOrder
== QSysInfo::BigEndian
;
363 Destroys the data stream.
365 The destructor will not affect the current I/O device, unless it is
366 an internal I/O device (e.g. a QBuffer) processing a QByteArray
367 passed in the \e constructor, in which case the internal I/O device
371 QDataStream::~QDataStream()
379 \fn QIODevice *QDataStream::device() const
381 Returns the I/O device currently set, or 0 if no
382 device is currently set.
388 void QDataStream::setDevice(QIODevice *d)
390 Sets the I/O device to \a d, which can be 0
391 to unset to current I/O device.
396 void QDataStream::setDevice(QIODevice
*d
)
407 Unsets the I/O device.
408 Use setDevice(0) instead.
411 void QDataStream::unsetDevice()
418 \fn bool QDataStream::atEnd() const
420 Returns true if the I/O device has reached the end position (end of
421 the stream or file) or if there is no I/O device set; otherwise
424 \sa QIODevice::atEnd()
427 bool QDataStream::atEnd() const
429 return dev
? dev
->atEnd() : true;
433 Returns the floating point precision of the data stream.
437 \sa FloatingPointPrecision setFloatingPointPrecision()
439 QDataStream::FloatingPointPrecision
QDataStream::floatingPointPrecision() const
441 return d
== 0 ? QDataStream::DoublePrecision
: d
->floatingPointPrecision
;
445 Sets the floating point precision of the data stream to \a precision. If the floating point precision is
446 DoublePrecision and the version of the data stream is Qt_4_6 or higher, all floating point
447 numbers will be written and read with 64-bit precision. If the floating point precision is
448 SinglePrecision and the version is Qt_4_6 or higher, all floating point numbers will be written
449 and read with 32-bit precision.
451 For versions prior to Qt_4_6, the precision of floating point numbers in the data stream depends
452 on the stream operator called.
454 The default is DoublePrecision.
456 \warning This property must be set to the same value on the object that writes and the object
457 that reads the data stream.
461 void QDataStream::setFloatingPointPrecision(QDataStream::FloatingPointPrecision precision
)
464 d
.reset(new QDataStreamPrivate());
465 d
->floatingPointPrecision
= precision
;
469 Returns the status of the data stream.
471 \sa Status setStatus() resetStatus()
474 QDataStream::Status
QDataStream::status() const
480 Resets the status of the data stream.
482 \sa Status status() setStatus()
484 void QDataStream::resetStatus()
490 Sets the status of the data stream to the \a status given.
492 \sa Status status() resetStatus()
494 void QDataStream::setStatus(Status status
)
500 /*!\fn bool QDataStream::eof() const
506 \fn int QDataStream::byteOrder() const
508 Returns the current byte order setting -- either BigEndian or
515 Sets the serialization byte order to \a bo.
517 The \a bo parameter can be QDataStream::BigEndian or
518 QDataStream::LittleEndian.
520 The default setting is big endian. We recommend leaving this
521 setting unless you have special requirements.
526 void QDataStream::setByteOrder(ByteOrder bo
)
529 if (QSysInfo::ByteOrder
== QSysInfo::BigEndian
)
530 noswap
= (byteorder
== BigEndian
);
532 noswap
= (byteorder
== LittleEndian
);
537 \fn bool QDataStream::isPrintableData() const
539 In Qt 4, this function always returns false.
541 \sa setPrintableData()
545 \fn void QDataStream::setPrintableData(bool enable)
547 In Qt 3, this function enabled output in a human-readable
548 format if \a enable was false.
550 In Qt 4, QDataStream no longer provides a human-readable output.
551 This function does nothing.
555 \enum QDataStream::Version
557 This enum provides symbolic synonyms for the data serialization
558 format version numbers.
560 \value Qt_1_0 Version 1 (Qt 1.x)
561 \value Qt_2_0 Version 2 (Qt 2.0)
562 \value Qt_2_1 Version 3 (Qt 2.1, 2.2, 2.3)
563 \value Qt_3_0 Version 4 (Qt 3.0)
564 \value Qt_3_1 Version 5 (Qt 3.1, 3.2)
565 \value Qt_3_3 Version 6 (Qt 3.3)
566 \value Qt_4_0 Version 7 (Qt 4.0, Qt 4.1)
567 \value Qt_4_1 Version 7 (Qt 4.0, Qt 4.1)
568 \value Qt_4_2 Version 8 (Qt 4.2)
569 \value Qt_4_3 Version 9 (Qt 4.3)
570 \value Qt_4_4 Version 10 (Qt 4.4)
571 \value Qt_4_5 Version 11 (Qt 4.5)
572 \value Qt_4_6 Version 12 (Qt 4.6)
574 \sa setVersion(), version()
578 \fn int QDataStream::version() const
580 Returns the version number of the data serialization format.
582 \sa setVersion(), Version
586 \fn void QDataStream::setVersion(int v)
588 Sets the version number of the data serialization format to \a v.
590 You don't \e have to set a version if you are using the current
591 version of Qt, but for your own custom binary formats we
592 recommend that you do; see \l{Versioning} in the Detailed
595 In order to accommodate new functionality, the datastream
596 serialization format of some Qt classes has changed in some
597 versions of Qt. If you want to read data that was created by an
598 earlier version of Qt, or write data that can be read by a
599 program that was compiled with an earlier version of Qt, use this
600 function to modify the serialization format used by QDataStream.
603 \header \i Qt Version \i QDataStream Version
607 \row \i Qt 4.0, 4.1 \i 7
609 \row \i Qt 3.1, 3.2 \i 5
611 \row \i Qt 2.1, 2.2, 2.3 \i 3
616 The \l Version enum provides symbolic constants for the different
617 versions of Qt. For example:
619 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 5
621 \sa version(), Version
624 /*****************************************************************************
625 QDataStream read functions
626 *****************************************************************************/
629 \fn QDataStream &QDataStream::operator>>(quint8 &i)
632 Reads an unsigned byte from the stream into \a i, and returns a
633 reference to the stream.
637 Reads a signed byte from the stream into \a i, and returns a
638 reference to the stream.
641 QDataStream
&QDataStream::operator>>(qint8
&i
)
644 CHECK_STREAM_PRECOND(*this)
646 if (!dev
->getChar(&c
))
647 setStatus(ReadPastEnd
);
655 \fn QDataStream &QDataStream::operator>>(quint16 &i)
658 Reads an unsigned 16-bit integer from the stream into \a i, and
659 returns a reference to the stream.
665 Reads a signed 16-bit integer from the stream into \a i, and
666 returns a reference to the stream.
669 QDataStream
&QDataStream::operator>>(qint16
&i
)
672 CHECK_STREAM_PRECOND(*this)
674 if (dev
->read((char *)&i
, 2) != 2) {
676 setStatus(ReadPastEnd
);
685 if (dev
->read(b
, 2) == 2) {
690 setStatus(ReadPastEnd
);
698 \fn QDataStream &QDataStream::operator>>(quint32 &i)
701 Reads an unsigned 32-bit integer from the stream into \a i, and
702 returns a reference to the stream.
708 Reads a signed 32-bit integer from the stream into \a i, and
709 returns a reference to the stream.
712 QDataStream
&QDataStream::operator>>(qint32
&i
)
715 CHECK_STREAM_PRECOND(*this)
717 if (dev
->read((char *)&i
, 4) != 4) {
719 setStatus(ReadPastEnd
);
721 } else { // swap bytes
728 if (dev
->read(b
, 4) == 4) {
735 setStatus(ReadPastEnd
);
742 \fn QDataStream &QDataStream::operator>>(quint64 &i)
745 Reads an unsigned 64-bit integer from the stream, into \a i, and
746 returns a reference to the stream.
752 Reads a signed 64-bit integer from the stream into \a i, and
753 returns a reference to the stream.
756 QDataStream
&QDataStream::operator>>(qint64
&i
)
759 CHECK_STREAM_PRECOND(*this)
763 i
= ((quint64
)i1
<< 32) + i2
;
764 } else if (noswap
) { // no conversion needed
765 if (dev
->read((char *)&i
, 8) != 8) {
767 setStatus(ReadPastEnd
);
769 } else { // swap bytes
777 if (dev
->read(b
, 8) == 8) {
788 setStatus(ReadPastEnd
);
795 Reads a boolean value from the stream into \a i. Returns a
796 reference to the stream.
798 QDataStream
&QDataStream::operator>>(bool &i
)
809 Reads a floating point number from the stream into \a f,
810 using the standard IEEE 754 format. Returns a reference to the
813 \sa setFloatingPointPrecision()
816 QDataStream
&QDataStream::operator>>(float &f
)
818 if (version() >= QDataStream::Qt_4_6
819 && floatingPointPrecision() == QDataStream::DoublePrecision
) {
827 CHECK_STREAM_PRECOND(*this)
829 if (dev
->read((char *)&f
, 4) != 4) {
831 setStatus(ReadPastEnd
);
833 } else { // swap bytes
841 if (dev
->read(b
, 4) == 4) {
848 setStatus(ReadPastEnd
);
854 #if defined(Q_DOUBLE_FORMAT)
855 #define Q_DF(x) Q_DOUBLE_FORMAT[(x)] - '0'
861 Reads a floating point number from the stream into \a f,
862 using the standard IEEE 754 format. Returns a reference to the
865 \sa setFloatingPointPrecision()
868 QDataStream
&QDataStream::operator>>(double &f
)
870 if (version() >= QDataStream::Qt_4_6
871 && floatingPointPrecision() == QDataStream::SinglePrecision
) {
879 CHECK_STREAM_PRECOND(*this)
880 #ifndef Q_DOUBLE_FORMAT
882 if (dev
->read((char *)&f
, 8) != 8) {
884 setStatus(ReadPastEnd
);
886 } else { // swap bytes
893 if (dev
->read(b
, 8) == 8) {
904 setStatus(ReadPastEnd
);
908 //non-standard floating point format
915 if (dev
->read(b
, 8) == 8) {
937 setStatus(ReadPastEnd
);
947 Reads the '\0'-terminated string \a s from the stream and returns
948 a reference to the stream.
950 Space for the string is allocated using \c new -- the caller must
951 destroy it with \c{delete[]}.
954 QDataStream
&QDataStream::operator>>(char *&s
)
957 return readBytes(s
, len
);
962 Reads the buffer \a s from the stream and returns a reference to
965 The buffer \a s is allocated using \c new. Destroy it with the \c
968 The \a l parameter is set to the length of the buffer. If the
969 string read is empty, \a l is set to 0 and \a s is set to
972 The serialization format is a quint32 length specifier first,
973 then \a l bytes of data.
975 \sa readRawData(), writeBytes()
978 QDataStream
&QDataStream::readBytes(char *&s
, uint
&l
)
982 CHECK_STREAM_PRECOND(*this)
989 const quint32 Step
= 1024 * 1024;
990 quint32 allocated
= 0;
995 int blockSize
= qMin(Step
, len
- allocated
);
997 curBuf
= new char[allocated
+ blockSize
+ 1];
999 memcpy(curBuf
, prevBuf
, allocated
);
1002 if (dev
->read(curBuf
+ allocated
, blockSize
) != blockSize
) {
1004 setStatus(ReadPastEnd
);
1007 allocated
+= blockSize
;
1008 } while (allocated
< len
);
1017 Reads at most \a len bytes from the stream into \a s and returns the number of
1018 bytes read. If an error occurs, this function returns -1.
1020 The buffer \a s must be preallocated. The data is \e not encoded.
1022 \sa readBytes(), QIODevice::read(), writeRawData()
1025 int QDataStream::readRawData(char *s
, int len
)
1027 CHECK_STREAM_PRECOND(-1)
1028 return dev
->read(s
, len
);
1032 /*****************************************************************************
1033 QDataStream write functions
1034 *****************************************************************************/
1038 \fn QDataStream &QDataStream::operator<<(quint8 i)
1041 Writes an unsigned byte, \a i, to the stream and returns a
1042 reference to the stream.
1046 Writes a signed byte, \a i, to the stream and returns a reference
1050 QDataStream
&QDataStream::operator<<(qint8 i
)
1052 CHECK_STREAM_PRECOND(*this)
1059 \fn QDataStream &QDataStream::operator<<(quint16 i)
1062 Writes an unsigned 16-bit integer, \a i, to the stream and returns
1063 a reference to the stream.
1069 Writes a signed 16-bit integer, \a i, to the stream and returns a
1070 reference to the stream.
1073 QDataStream
&QDataStream::operator<<(qint16 i
)
1075 CHECK_STREAM_PRECOND(*this)
1077 dev
->write((char *)&i
, sizeof(qint16
));
1078 } else { // swap bytes
1096 Writes a signed 32-bit integer, \a i, to the stream and returns a
1097 reference to the stream.
1100 QDataStream
&QDataStream::operator<<(qint32 i
)
1102 CHECK_STREAM_PRECOND(*this)
1104 dev
->write((char *)&i
, sizeof(qint32
));
1105 } else { // swap bytes
1123 \fn QDataStream &QDataStream::operator<<(quint64 i)
1126 Writes an unsigned 64-bit integer, \a i, to the stream and returns a
1127 reference to the stream.
1133 Writes a signed 64-bit integer, \a i, to the stream and returns a
1134 reference to the stream.
1137 QDataStream
&QDataStream::operator<<(qint64 i
)
1139 CHECK_STREAM_PRECOND(*this)
1140 if (version() < 6) {
1141 quint32 i1
= i
& 0xffffffff;
1142 quint32 i2
= i
>> 32;
1144 } else if (noswap
) { // no conversion needed
1145 dev
->write((char *)&i
, sizeof(qint64
));
1146 } else { // swap bytes
1168 \fn QDataStream &QDataStream::operator<<(quint32 i)
1171 Writes an unsigned integer, \a i, to the stream as a 32-bit
1172 unsigned integer (quint32). Returns a reference to the stream.
1176 Writes a boolean value, \a i, to the stream. Returns a reference
1180 QDataStream
&QDataStream::operator<<(bool i
)
1182 CHECK_STREAM_PRECOND(*this)
1183 dev
->putChar(qint8(i
));
1190 Writes a floating point number, \a f, to the stream using
1191 the standard IEEE 754 format. Returns a reference to the stream.
1193 \sa setFloatingPointPrecision()
1196 QDataStream
&QDataStream::operator<<(float f
)
1198 if (version() >= QDataStream::Qt_4_6
1199 && floatingPointPrecision() == QDataStream::DoublePrecision
) {
1204 CHECK_STREAM_PRECOND(*this)
1205 float g
= f
; // fixes float-on-stack problem
1206 if (noswap
) { // no conversion needed
1207 dev
->write((char *)&g
, sizeof(float));
1208 } else { // swap bytes
1229 Writes a floating point number, \a f, to the stream using
1230 the standard IEEE 754 format. Returns a reference to the stream.
1232 \sa setFloatingPointPrecision()
1235 QDataStream
&QDataStream::operator<<(double f
)
1237 if (version() >= QDataStream::Qt_4_6
1238 && floatingPointPrecision() == QDataStream::SinglePrecision
) {
1243 CHECK_STREAM_PRECOND(*this)
1244 #ifndef Q_DOUBLE_FORMAT
1246 dev
->write((char *)&f
, sizeof(double));
1301 Writes the '\0'-terminated string \a s to the stream and returns a
1302 reference to the stream.
1304 The string is serialized using writeBytes().
1307 QDataStream
&QDataStream::operator<<(const char *s
)
1310 *this << (quint32
)0;
1313 uint len
= qstrlen(s
) + 1; // also write null terminator
1314 *this << (quint32
)len
; // write length specifier
1315 writeRawData(s
, len
);
1321 Writes the length specifier \a len and the buffer \a s to the
1322 stream and returns a reference to the stream.
1324 The \a len is serialized as a quint32, followed by \a len bytes
1325 from \a s. Note that the data is \e not encoded.
1327 \sa writeRawData(), readBytes()
1330 QDataStream
&QDataStream::writeBytes(const char *s
, uint len
)
1332 CHECK_STREAM_PRECOND(*this)
1333 *this << (quint32
)len
; // write length specifier
1335 writeRawData(s
, len
);
1341 Writes \a len bytes from \a s to the stream. Returns the
1342 number of bytes actually written, or -1 on error.
1343 The data is \e not encoded.
1345 \sa writeBytes(), QIODevice::write(), readRawData()
1348 int QDataStream::writeRawData(const char *s
, int len
)
1350 CHECK_STREAM_PRECOND(-1)
1351 return dev
->write(s
, len
);
1357 Skips \a len bytes from the device. Returns the number of bytes
1358 actually skipped, or -1 on error.
1360 This is equivalent to calling readRawData() on a buffer of length
1361 \a len and ignoring the buffer.
1363 \sa QIODevice::seek()
1365 int QDataStream::skipRawData(int len
)
1367 CHECK_STREAM_PRECOND(-1)
1369 if (dev
->isSequential()) {
1374 int blockSize
= qMin(len
, (int)sizeof(buf
));
1375 int n
= dev
->read(buf
, blockSize
);
1386 qint64 pos
= dev
->pos();
1387 qint64 size
= dev
->size();
1388 if (pos
+ len
> size
)
1390 if (!dev
->seek(pos
+ len
))
1398 \fn QDataStream &QDataStream::readRawBytes(char *str, uint len)
1400 Use readRawData() instead.
1404 \fn QDataStream &QDataStream::writeRawBytes(const char *str, uint len)
1406 Use writeRawData() instead.
1412 #endif // QT_NO_DATASTREAM