~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qdatastream.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/src/corelib/io/qdatastream.cpp -->
 
6
<head>
 
7
    <title>Qt 4.0: QDataStream Class Reference</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QDataStream Class Reference</h1>
 
21
<p>The QDataStream class provides serialization of binary data to a <a href="qiodevice.html">QIODevice</a>. <a href="#details">More...</a></p>
 
22
<pre>#include &lt;QDataStream&gt;</pre><p>Part of the <a href="qtcore.html">QtCore</a> module.</p>
 
23
<p><b>Note:</b> All the functions in this class are <a href="threads.html#reentrant">reentrant</a>.</p>
 
24
<ul>
 
25
<li><a href="qdatastream-members.html">List of all members, including inherited members</a></li>
 
26
<li><a href="qdatastream-qt3.html">Qt 3 support members</a></li>
 
27
</ul>
 
28
<a name="public-types"></a>
 
29
<h3>Public Types</h3>
 
30
<ul>
 
31
<li><div class="fn"/>enum <b><a href="qdatastream.html#ByteOrder-enum">ByteOrder</a></b> { BigEndian, LittleEndian }</li>
 
32
<li><div class="fn"/>enum <b><a href="qdatastream.html#Status-enum">Status</a></b> { Ok, ReadPastEnd, ReadCorruptData }</li>
 
33
<li><div class="fn"/>enum <b><a href="qdatastream.html#Version-enum">Version</a></b> { Qt_1_0, Qt_2_0, Qt_2_1, Qt_3_0, ..., Qt_4_0 }</li>
 
34
</ul>
 
35
<a name="public-functions"></a>
 
36
<h3>Public Functions</h3>
 
37
<ul>
 
38
<li><div class="fn"/><b><a href="qdatastream.html#QDataStream">QDataStream</a></b> ()</li>
 
39
<li><div class="fn"/><b><a href="qdatastream.html#QDataStream-2">QDataStream</a></b> ( QIODevice * <i>d</i> )</li>
 
40
<li><div class="fn"/><b><a href="qdatastream.html#QDataStream-4">QDataStream</a></b> ( QByteArray * <i>a</i>, QIODevice::OpenMode <i>mode</i> )</li>
 
41
<li><div class="fn"/><b><a href="qdatastream.html#QDataStream-5">QDataStream</a></b> ( const QByteArray &amp; <i>a</i> )</li>
 
42
<li><div class="fn"/>virtual <b><a href="qdatastream.html#dtor.QDataStream">~QDataStream</a></b> ()</li>
 
43
<li><div class="fn"/>bool <b><a href="qdatastream.html#atEnd">atEnd</a></b> () const</li>
 
44
<li><div class="fn"/>ByteOrder <b><a href="qdatastream.html#byteOrder">byteOrder</a></b> () const</li>
 
45
<li><div class="fn"/>QIODevice * <b><a href="qdatastream.html#device">device</a></b> () const</li>
 
46
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#readBytes">readBytes</a></b> ( char *&amp; <i>s</i>, uint &amp; <i>l</i> )</li>
 
47
<li><div class="fn"/>int <b><a href="qdatastream.html#readRawData">readRawData</a></b> ( char * <i>s</i>, int <i>len</i> )</li>
 
48
<li><div class="fn"/>void <b><a href="qdatastream.html#resetStatus">resetStatus</a></b> ()</li>
 
49
<li><div class="fn"/>void <b><a href="qdatastream.html#setByteOrder">setByteOrder</a></b> ( ByteOrder <i>bo</i> )</li>
 
50
<li><div class="fn"/>void <b><a href="qdatastream.html#setDevice">setDevice</a></b> ( QIODevice * <i>d</i> )</li>
 
51
<li><div class="fn"/>void <b><a href="qdatastream.html#setStatus">setStatus</a></b> ( Status <i>status</i> )</li>
 
52
<li><div class="fn"/>void <b><a href="qdatastream.html#setVersion">setVersion</a></b> ( int <i>v</i> )</li>
 
53
<li><div class="fn"/>Status <b><a href="qdatastream.html#status">status</a></b> () const</li>
 
54
<li><div class="fn"/>void <b><a href="qdatastream.html#unsetDevice">unsetDevice</a></b> ()</li>
 
55
<li><div class="fn"/>int <b><a href="qdatastream.html#version">version</a></b> () const</li>
 
56
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#writeBytes">writeBytes</a></b> ( const char * <i>s</i>, uint <i>len</i> )</li>
 
57
<li><div class="fn"/>int <b><a href="qdatastream.html#writeRawData">writeRawData</a></b> ( const char * <i>s</i>, int <i>len</i> )</li>
 
58
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt">operator&lt;&lt;</a></b> ( qint8 <i>i</i> )</li>
 
59
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-2">operator&lt;&lt;</a></b> ( bool <i>i</i> )</li>
 
60
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-3">operator&lt;&lt;</a></b> ( quint8 <i>i</i> )</li>
 
61
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-4">operator&lt;&lt;</a></b> ( quint16 <i>i</i> )</li>
 
62
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-5">operator&lt;&lt;</a></b> ( qint16 <i>i</i> )</li>
 
63
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-6">operator&lt;&lt;</a></b> ( qint32 <i>i</i> )</li>
 
64
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-7">operator&lt;&lt;</a></b> ( quint64 <i>i</i> )</li>
 
65
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-8">operator&lt;&lt;</a></b> ( qint64 <i>i</i> )</li>
 
66
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-9">operator&lt;&lt;</a></b> ( quint32 <i>i</i> )</li>
 
67
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-10">operator&lt;&lt;</a></b> ( float <i>f</i> )</li>
 
68
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-11">operator&lt;&lt;</a></b> ( double <i>f</i> )</li>
 
69
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-lt-lt-12">operator&lt;&lt;</a></b> ( const char * <i>s</i> )</li>
 
70
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt">operator&gt;&gt;</a></b> ( qint8 &amp; <i>i</i> )</li>
 
71
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-2">operator&gt;&gt;</a></b> ( bool &amp; <i>i</i> )</li>
 
72
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-3">operator&gt;&gt;</a></b> ( quint8 &amp; <i>i</i> )</li>
 
73
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-4">operator&gt;&gt;</a></b> ( quint16 &amp; <i>i</i> )</li>
 
74
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-5">operator&gt;&gt;</a></b> ( qint16 &amp; <i>i</i> )</li>
 
75
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-6">operator&gt;&gt;</a></b> ( quint32 &amp; <i>i</i> )</li>
 
76
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-7">operator&gt;&gt;</a></b> ( qint32 &amp; <i>i</i> )</li>
 
77
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-8">operator&gt;&gt;</a></b> ( quint64 &amp; <i>i</i> )</li>
 
78
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-9">operator&gt;&gt;</a></b> ( qint64 &amp; <i>i</i> )</li>
 
79
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-10">operator&gt;&gt;</a></b> ( float &amp; <i>f</i> )</li>
 
80
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-11">operator&gt;&gt;</a></b> ( double &amp; <i>f</i> )</li>
 
81
<li><div class="fn"/>QDataStream &amp; <b><a href="qdatastream.html#operator-gt-gt-12">operator&gt;&gt;</a></b> ( char *&amp; <i>s</i> )</li>
 
82
</ul>
 
83
<a name="details"></a>
 
84
<hr />
 
85
<h2>Detailed Description</h2>
 
86
<p>The QDataStream class provides serialization of binary data to a <a href="qiodevice.html">QIODevice</a>.</p>
 
87
<p>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.</p>
 
88
<p>You can also use a data stream to read/write <a href="#raw">raw unencoded binary data</a>. If you want a &quot;parsing&quot; input stream, see <a href="qtextstream.html">QTextStream</a>.</p>
 
89
<p>The QDataStream class implements the serialization of C++'s basic data types, like <tt>char</tt>, <tt>short</tt>, <tt>int</tt>, <tt>char *</tt>, etc. Serialization of more complex data is accomplished by breaking up the data into primitive units.</p>
 
90
<p>A data stream cooperates closely with a <a href="qiodevice.html">QIODevice</a>. A <a href="qiodevice.html">QIODevice</a> represents an input/output medium one can read data from and write data to. The <a href="qfile.html">QFile</a> class is an example of an I/O device.</p>
 
91
<p>Example (write binary data to a stream):</p>
 
92
<pre>&nbsp;   QFile file(&quot;file.dat&quot;);
 
93
    file.open(QIODevice::WriteOnly);
 
94
    QDataStream out(&amp;file);   // we will serialize the data into the file
 
95
    out &lt;&lt; &quot;the answer is&quot;;   // serialize a string
 
96
    out &lt;&lt; (qint32)42;        // serialize an integer</pre>
 
97
<p>Example (read binary data from a stream):</p>
 
98
<pre>&nbsp;   QFile file(&quot;file.dat&quot;);
 
99
    file.open(QIODevice::ReadOnly);
 
100
    QDataStream in(&amp;file);    // read the data serialized from the file
 
101
    QString str;
 
102
    qint32 a;
 
103
    in &gt;&gt; str &gt;&gt; a;           // extract &quot;the answer is&quot; and 42</pre>
 
104
<p>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 <a href="qbrush.html">QBrush</a>, <a href="qcolor.html">QColor</a>, <a href="qdatetime.html">QDateTime</a>, <a href="qfont.html">QFont</a>, <a href="qpixmap.html">QPixmap</a>, <a href="qstring.html">QString</a>, <a href="qvariant.html">QVariant</a> and many others. For the complete list of all Qt types supporting data streaming see the <a href="datastreamformat.html">Format of the QDataStream operators</a>.</p>
 
105
<p>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.</p>
 
106
<p>To take one example, a <tt>char *</tt> 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 <tt>char *</tt> string, 4 bytes are read to create the 32-bit length value, then that many characters for the <tt>char *</tt> string including the '\0' terminator are read.</p>
 
107
<p>The initial I/O device is usually set in the constructor, but can be changed with <a href="qdatastream.html#setDevice">setDevice</a>(). If you've reached the end of the data (or if there is no I/O device set) <a href="qdatastream.html#atEnd">atEnd</a>() will return true.</p>
 
108
<a name="versioning"></a>
 
109
<h3>Versioning</h3>
 
110
<p>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 (<a href="qdatastream.html#version">version</a>()) is used for reading and writing. If you need both forward and backward compatibility, you can hardcode the version number in the application:</p>
 
111
<pre>&nbsp;   stream.setVersion(QDataStream::Qt_4_0);</pre>
 
112
<p>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:</p>
 
113
<pre>&nbsp;   QFile file(&quot;file.xxx&quot;);
 
114
    file.open(QIODevice::WriteOnly);
 
115
    QDataStream out(&amp;file);
 
116
 
 
117
    // Write a header with a &quot;magic number&quot; and a version
 
118
    out &lt;&lt; (quint32)0xA0B0C0D0;
 
119
    out &lt;&lt; (qint32)123;
 
120
 
 
121
    out.setVersion(QDataStream::Qt_4_0);
 
122
 
 
123
    // Write the data
 
124
    out &lt;&lt; lots_of_interesting_data;</pre>
 
125
<p>Then read it in with:</p>
 
126
<pre>&nbsp;   QFile file(&quot;file.xxx&quot;);
 
127
    file.open(QIODevice::ReadOnly);
 
128
    QDataStream in(&amp;file);
 
129
 
 
130
    // Read and check the header
 
131
    quint32 magic;
 
132
    in &gt;&gt; magic;
 
133
    if (magic != 0xA0B0C0D0)
 
134
        return XXX_BAD_FILE_FORMAT;
 
135
 
 
136
    // Read the version
 
137
    qint32 version;
 
138
    in &gt;&gt; version;
 
139
    if (version &lt; 100)
 
140
        return XXX_BAD_FILE_TOO_OLD;
 
141
    if (version &gt; 123)
 
142
        return XXX_BAD_FILE_TOO_NEW;
 
143
 
 
144
    if (version &lt;= 110)
 
145
        in.setVersion(QDataStream::Qt_3_2);
 
146
    else
 
147
        in.setVersion(QDataStream::Qt_4_0);
 
148
 
 
149
    // Read the data
 
150
    in &gt;&gt; lots_of_interesting_data;
 
151
    if (version &gt;= 120)
 
152
        in &gt;&gt; data_new_in_XXX_version_1_2;
 
153
    in &gt;&gt; other_interesting_data;</pre>
 
154
<p>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.</p>
 
155
<a name="raw"></a><a name="reading-and-writing-raw-binary-data"></a>
 
156
<h3>Reading and writing raw binary data</h3>
 
157
<p>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 <tt>char *</tt> using <a href="qdatastream.html#readRawData">readRawData</a>(). Similarly data can be written to the stream using <a href="qdatastream.html#writeRawData">writeRawData</a>(). Note that any encoding/decoding of the data must be done by you.</p>
 
158
<p>A similar pair of functions is <a href="qdatastream.html#readBytes">readBytes</a>() and <a href="qdatastream.html#writeBytes">writeBytes</a>(). These differ from their <i>raw</i> counterparts as follows: <a href="qdatastream.html#readBytes">readBytes</a>() reads a <a href="qtglobal.html#quint32-typedef">quint32</a> which is taken to be the length of the data to be read, then that number of bytes is read into the preallocated <tt>char *</tt>; <a href="qdatastream.html#writeBytes">writeBytes</a>() writes a <a href="qtglobal.html#quint32-typedef">quint32</a> containing the length of the data, followed by the data. Note that any encoding/decoding of the data (apart from the length <a href="qtglobal.html#quint32-typedef">quint32</a>) must be done by you.</p>
 
159
<p>See also <a href="qtextstream.html">QTextStream</a> and <a href="qvariant.html">QVariant</a>.</p>
 
160
<hr />
 
161
<h2>Member Type Documentation</h2>
 
162
<h3 class="fn"><a name="ByteOrder-enum"></a>enum QDataStream::ByteOrder</h3>
 
163
<p>The byte order used for reading/writing the data.</p>
 
164
<table border="1" cellpadding="2" cellspacing="1" width="100%">
 
165
<tr><th width="25%">Constant</th><th width="15%">Value</th><th width="60%">Description</th></tr>
 
166
<tr><td valign="top"><tt>QDataStream::BigEndian</tt></td><td align="center" valign="top"><tt>QSysInfo::BigEndian</tt></td><td valign="top">Most significant byte first (the default)</td></tr>
 
167
<tr><td valign="top"><tt>QDataStream::LittleEndian</tt></td><td align="center" valign="top"><tt>QSysInfo::LittleEndian</tt></td><td valign="top">Less significant byte first</td></tr>
 
168
</table>
 
169
<h3 class="fn"><a name="Status-enum"></a>enum QDataStream::Status</h3>
 
170
<p>This enum describes the current status of the data stream.</p>
 
171
<table border="1" cellpadding="2" cellspacing="1" width="100%">
 
172
<tr><th width="25%">Constant</th><th width="15%">Value</th><th width="60%">Description</th></tr>
 
173
<tr><td valign="top"><tt>QDataStream::Ok</tt></td><td align="center" valign="top"><tt>0</tt></td><td valign="top">The data stream is operating normally.</td></tr>
 
174
<tr><td valign="top"><tt>QDataStream::ReadPastEnd</tt></td><td align="center" valign="top"><tt>1</tt></td><td valign="top">The data stream has read past the end of the data in the underlying device.</td></tr>
 
175
<tr><td valign="top"><tt>QDataStream::ReadCorruptData</tt></td><td align="center" valign="top"><tt>2</tt></td><td valign="top">The data stream has read corrupt data.</td></tr>
 
176
</table>
 
177
<h3 class="fn"><a name="Version-enum"></a>enum QDataStream::Version</h3>
 
178
<p>This enum provides symbolic synonyms for the data serialization format version numbers.</p>
 
179
<table border="1" cellpadding="2" cellspacing="1" width="100%">
 
180
<tr><th width="25%">Constant</th><th width="15%">Value</th><th width="60%">Description</th></tr>
 
181
<tr><td valign="top"><tt>QDataStream::Qt_1_0</tt></td><td align="center" valign="top"><tt>1</tt></td><td valign="top">Version 1 (Qt 1.x)</td></tr>
 
182
<tr><td valign="top"><tt>QDataStream::Qt_2_0</tt></td><td align="center" valign="top"><tt>2</tt></td><td valign="top">Version 2 (Qt 2.0)</td></tr>
 
183
<tr><td valign="top"><tt>QDataStream::Qt_2_1</tt></td><td align="center" valign="top"><tt>3</tt></td><td valign="top">Version 3 (Qt 2.1, 2.2, 2.3)</td></tr>
 
184
<tr><td valign="top"><tt>QDataStream::Qt_3_0</tt></td><td align="center" valign="top"><tt>4</tt></td><td valign="top">Version 4 (Qt 3.0)</td></tr>
 
185
<tr><td valign="top"><tt>QDataStream::Qt_3_1</tt></td><td align="center" valign="top"><tt>5</tt></td><td valign="top">Version 5 (Qt 3.1, 3.2)</td></tr>
 
186
<tr><td valign="top"><tt>QDataStream::Qt_3_3</tt></td><td align="center" valign="top"><tt>6</tt></td><td valign="top">Version 6 (Qt 3.3)</td></tr>
 
187
<tr><td valign="top"><tt>QDataStream::Qt_4_0</tt></td><td align="center" valign="top"><tt>7</tt></td><td valign="top">Version 7 (Qt 4.0)</td></tr>
 
188
</table>
 
189
<p>See also <a href="qdatastream.html#setVersion">setVersion</a>() and <a href="qdatastream.html#version">version</a>().</p>
 
190
<hr />
 
191
<h2>Member Function Documentation</h2>
 
192
<h3 class="fn"><a name="QDataStream"></a>QDataStream::QDataStream ()</h3>
 
193
<p>Constructs a data stream that has no I/O device.</p>
 
194
<p>See also <a href="qdatastream.html#setDevice">setDevice</a>().</p>
 
195
<h3 class="fn"><a name="QDataStream-2"></a>QDataStream::QDataStream ( <a href="qiodevice.html">QIODevice</a> * <i>d</i> )</h3>
 
196
<p>Constructs a data stream that uses the I/O device <i>d</i>.</p>
 
197
<p><b>Warning:</b> If you use QSocket or QSocketDevice as the I/O device <i>d</i> for reading data, you must make sure that enough data is available on the socket for the operation to successfully proceed; <a href="qdatastream.html">QDataStream</a> does not have any means to handle or recover from short-reads.</p>
 
198
<p>See also <a href="qdatastream.html#setDevice">setDevice</a>() and <a href="qdatastream.html#device">device</a>().</p>
 
199
<h3 class="fn"><a name="QDataStream-4"></a>QDataStream::QDataStream ( <a href="qbytearray.html">QByteArray</a> * <i>a</i>, <a href="qiodevice.html#OpenModeFlag-enum">QIODevice::OpenMode</a> <i>mode</i> )</h3>
 
200
<p>Constructs a data stream that operates on a byte array, <i>a</i>. The <i>mode</i> describes how the device is to be used.</p>
 
201
<p>Alternatively, you can use <a href="qdatastream.html">QDataStream</a>(const <a href="qbytearray.html">QByteArray</a> &amp;) if you just want to read from a byte array.</p>
 
202
<p>Since <a href="qbytearray.html">QByteArray</a> is not a <a href="qiodevice.html">QIODevice</a> subclass, internally a <a href="qbuffer.html">QBuffer</a> is created to wrap the byte array.</p>
 
203
<h3 class="fn"><a name="QDataStream-5"></a>QDataStream::QDataStream ( const <a href="qbytearray.html">QByteArray</a> &amp; <i>a</i> )</h3>
 
204
<p>Constructs a read-only data stream that operates on byte array <i>a</i>. Use <a href="qdatastream.html">QDataStream</a>(<a href="qbytearray.html">QByteArray</a>*, int) if you want to write to a byte array.</p>
 
205
<p>Since <a href="qbytearray.html">QByteArray</a> is not a <a href="qiodevice.html">QIODevice</a> subclass, internally a <a href="qbuffer.html">QBuffer</a> is created to wrap the byte array.</p>
 
206
<h3 class="fn"><a name="dtor.QDataStream"></a>QDataStream::~QDataStream ()&nbsp;&nbsp;<tt> [virtual]</tt></h3>
 
207
<p>Destroys the data stream.</p>
 
208
<p>The destructor will not affect the current I/O device, unless it is an internal I/O device (e.g. a <a href="qbuffer.html">QBuffer</a>) processing a <a href="qbytearray.html">QByteArray</a> passed in the <i>constructor</i>, in which case the internal I/O device is destroyed.</p>
 
209
<h3 class="fn"><a name="atEnd"></a>bool QDataStream::atEnd () const</h3>
 
210
<p>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.</p>
 
211
<p>See also <a href="qiodevice.html#atEnd">QIODevice::atEnd</a>().</p>
 
212
<h3 class="fn"><a name="byteOrder"></a><a href="qdatastream.html#ByteOrder-enum">ByteOrder</a> QDataStream::byteOrder () const</h3>
 
213
<p>Returns the current byte order setting -- either <tt>BigEndian</tt> or <tt>LittleEndian</tt>.</p>
 
214
<p>See also <a href="qdatastream.html#setByteOrder">setByteOrder</a>().</p>
 
215
<h3 class="fn"><a name="device"></a><a href="qiodevice.html">QIODevice</a> * QDataStream::device () const</h3>
 
216
<p>Returns the I/O device currently set.</p>
 
217
<p>See also <a href="qdatastream.html#setDevice">setDevice</a>() and <a href="qdatastream.html#unsetDevice">unsetDevice</a>().</p>
 
218
<h3 class="fn"><a name="readBytes"></a>QDataStream &amp; QDataStream::readBytes ( char *&amp; <i>s</i>, <a href="qtglobal.html#uint-typedef">uint</a> &amp; <i>l</i> )</h3>
 
219
<p>Reads the buffer <i>s</i> from the stream and returns a reference to the stream.</p>
 
220
<p>The buffer <i>s</i> is allocated using <tt>new</tt>. Destroy it with the <tt>delete[]</tt> operator.</p>
 
221
<p>The <i>l</i> parameter is set to the length of the buffer. If the string read is empty, <i>l</i> is set to 0 and <i>s</i> is set to a null pointer.</p>
 
222
<p>The serialization format is a <a href="qtglobal.html#quint32-typedef">quint32</a> length specifier first, then <i>l</i> bytes of data.</p>
 
223
<p>See also <a href="qdatastream.html#readRawData">readRawData</a>() and <a href="qdatastream.html#writeBytes">writeBytes</a>().</p>
 
224
<h3 class="fn"><a name="readRawData"></a>int QDataStream::readRawData ( char * <i>s</i>, int <i>len</i> )</h3>
 
225
<p>Reads <i>len</i> bytes from the stream into <i>s</i> and returns a reference to the stream.</p>
 
226
<p>The buffer <i>s</i> must be preallocated. The data is <i>not</i> encoded.</p>
 
227
<p>See also <a href="qdatastream.html#readBytes">readBytes</a>(), <a href="qiodevice.html#read">QIODevice::read</a>(), and <a href="qdatastream.html#writeRawData">writeRawData</a>().</p>
 
228
<h3 class="fn"><a name="resetStatus"></a>void QDataStream::resetStatus ()</h3>
 
229
<p>Resets the status of the data stream.</p>
 
230
<p>See also <a href="qdatastream.html#Status-enum">Status</a>, <a href="qdatastream.html#status">status</a>(), and <a href="qdatastream.html#setStatus">setStatus</a>().</p>
 
231
<h3 class="fn"><a name="setByteOrder"></a>void QDataStream::setByteOrder ( <a href="qdatastream.html#ByteOrder-enum">ByteOrder</a> <i>bo</i> )</h3>
 
232
<p>Sets the serialization byte order to <i>bo</i>.</p>
 
233
<p>The <i>bo</i> parameter can be <tt>QDataStream::BigEndian</tt> or <tt>QDataStream::LittleEndian</tt>.</p>
 
234
<p>The default setting is big endian. We recommend leaving this setting unless you have special requirements.</p>
 
235
<p>See also <a href="qdatastream.html#byteOrder">byteOrder</a>().</p>
 
236
<h3 class="fn"><a name="setDevice"></a>void QDataStream::setDevice ( <a href="qiodevice.html">QIODevice</a> * <i>d</i> )</h3>
 
237
<p>void QDataStream::setDevice(<a href="qiodevice.html">QIODevice</a> *d)</p>
 
238
<p>Sets the I/O device to <i>d</i>.</p>
 
239
<p>See also <a href="qdatastream.html#device">device</a>() and <a href="qdatastream.html#unsetDevice">unsetDevice</a>().</p>
 
240
<h3 class="fn"><a name="setStatus"></a>void QDataStream::setStatus ( <a href="qdatastream.html#Status-enum">Status</a> <i>status</i> )</h3>
 
241
<p>Sets the status of the data stream to the <i>status</i> given.</p>
 
242
<p>See also <a href="qdatastream.html#Status-enum">Status</a>, <a href="qdatastream.html#status">status</a>(), and <a href="qdatastream.html#resetStatus">resetStatus</a>().</p>
 
243
<h3 class="fn"><a name="setVersion"></a>void QDataStream::setVersion ( int <i>v</i> )</h3>
 
244
<p>Sets the version number of the data serialization format to <i>v</i>.</p>
 
245
<p>You don't <i>have</i> 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 <a href="qdatastream.html#versioning">Versioning</a> in the Detailed Description.</p>
 
246
<p>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 <a href="qdatastream.html">QDataStream</a>.</p>
 
247
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
248
<tr valign="top" bgcolor="#a2c511"><th>Qt Version</th><th><a href="qdatastream.html">QDataStream</a> Version</th></tr>
 
249
<tr valign="top" bgcolor="#f0f0f0"><td>Qt 4.0</td><td>7</td></tr>
 
250
<tr valign="top" bgcolor="#e0e0e0"><td>Qt 3.3</td><td>6</td></tr>
 
251
<tr valign="top" bgcolor="#f0f0f0"><td>Qt 3.1, 3.2</td><td>5</td></tr>
 
252
<tr valign="top" bgcolor="#e0e0e0"><td>Qt 3.0</td><td>4</td></tr>
 
253
<tr valign="top" bgcolor="#f0f0f0"><td>Qt 2.1, 2.2, 2.3</td><td>3</td></tr>
 
254
<tr valign="top" bgcolor="#e0e0e0"><td>Qt 2.0</td><td>2</td></tr>
 
255
<tr valign="top" bgcolor="#f0f0f0"><td>Qt 1.x</td><td>1</td></tr>
 
256
</table>
 
257
<p>The <a href="qdatastream.html#Version-enum">Version</a> enum provides symbolic constants for the different versions of Qt. For example:</p>
 
258
<pre>&nbsp;   QDataStream out(file);
 
259
    out.setVersion(QDataStream::Qt_4_0);</pre>
 
260
<p>See also <a href="qdatastream.html#version">version</a>() and <a href="qdatastream.html#Version-enum">Version</a>.</p>
 
261
<h3 class="fn"><a name="status"></a><a href="qdatastream.html#Status-enum">Status</a> QDataStream::status () const</h3>
 
262
<p>Returns the status of the data stream.</p>
 
263
<p>See also <a href="qdatastream.html#Status-enum">Status</a>, <a href="qdatastream.html#setStatus">setStatus</a>(), and <a href="qdatastream.html#resetStatus">resetStatus</a>().</p>
 
264
<h3 class="fn"><a name="unsetDevice"></a>void QDataStream::unsetDevice ()</h3>
 
265
<p>Unsets the I/O device. This is the same as calling setDevice(0).</p>
 
266
<p>See also <a href="qdatastream.html#device">device</a>() and <a href="qdatastream.html#setDevice">setDevice</a>().</p>
 
267
<h3 class="fn"><a name="version"></a>int QDataStream::version () const</h3>
 
268
<p>Returns the version number of the data serialization format.</p>
 
269
<p>See also <a href="qdatastream.html#setVersion">setVersion</a>() and <a href="qdatastream.html#Version-enum">Version</a>.</p>
 
270
<h3 class="fn"><a name="writeBytes"></a>QDataStream &amp; QDataStream::writeBytes ( const char * <i>s</i>, <a href="qtglobal.html#uint-typedef">uint</a> <i>len</i> )</h3>
 
271
<p>Writes the length specifier <i>len</i> and the buffer <i>s</i> to the stream and returns a reference to the stream.</p>
 
272
<p>The <i>len</i> is serialized as a <a href="qtglobal.html#quint32-typedef">quint32</a>, followed by <i>len</i> bytes from <i>s</i>. Note that the data is <i>not</i> encoded.</p>
 
273
<p>See also <a href="qdatastream.html#writeRawData">writeRawData</a>() and <a href="qdatastream.html#readBytes">readBytes</a>().</p>
 
274
<h3 class="fn"><a name="writeRawData"></a>int QDataStream::writeRawData ( const char * <i>s</i>, int <i>len</i> )</h3>
 
275
<p>Writes <i>len</i> bytes from <i>s</i> to the stream and returns a reference to the stream. The data is <i>not</i> encoded.</p>
 
276
<p>See also <a href="qdatastream.html#writeBytes">writeBytes</a>(), <a href="qiodevice.html#write">QIODevice::write</a>(), and <a href="qdatastream.html#readRawData">readRawData</a>().</p>
 
277
<h3 class="fn"><a name="operator-lt-lt"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( <a href="qtglobal.html#qint8-typedef">qint8</a> <i>i</i> )</h3>
 
278
<p>Writes a signed byte, <i>i</i>, to the stream and returns a reference to the stream.</p>
 
279
<h3 class="fn"><a name="operator-lt-lt-2"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( bool <i>i</i> )</h3>
 
280
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
281
<p>Writes a boolean value, <i>i</i>, to the stream. Returns a reference to the stream.</p>
 
282
<h3 class="fn"><a name="operator-lt-lt-3"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( <a href="qtglobal.html#quint8-typedef">quint8</a> <i>i</i> )</h3>
 
283
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
284
<p>Writes an unsigned byte, <i>i</i>, to the stream and returns a reference to the stream.</p>
 
285
<h3 class="fn"><a name="operator-lt-lt-4"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( <a href="qtglobal.html#quint16-typedef">quint16</a> <i>i</i> )</h3>
 
286
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
287
<p>Writes an unsigned 16-bit integer, <i>i</i>, to the stream and returns a reference to the stream.</p>
 
288
<h3 class="fn"><a name="operator-lt-lt-5"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( <a href="qtglobal.html#qint16-typedef">qint16</a> <i>i</i> )</h3>
 
289
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
290
<p>Writes a signed 16-bit integer, <i>i</i>, to the stream and returns a reference to the stream.</p>
 
291
<h3 class="fn"><a name="operator-lt-lt-6"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( <a href="qtglobal.html#qint32-typedef">qint32</a> <i>i</i> )</h3>
 
292
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
293
<p>Writes a signed 32-bit integer, <i>i</i>, to the stream and returns a reference to the stream.</p>
 
294
<h3 class="fn"><a name="operator-lt-lt-7"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( <a href="qtglobal.html#quint64-typedef">quint64</a> <i>i</i> )</h3>
 
295
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
296
<p>Writes an unsigned 64-bit integer, <i>i</i>, to the stream and returns a reference to the stream.</p>
 
297
<h3 class="fn"><a name="operator-lt-lt-8"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( <a href="qtglobal.html#qint64-typedef">qint64</a> <i>i</i> )</h3>
 
298
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
299
<p>Writes a signed 64-bit integer, <i>i</i>, to the stream and returns a reference to the stream.</p>
 
300
<h3 class="fn"><a name="operator-lt-lt-9"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( <a href="qtglobal.html#quint32-typedef">quint32</a> <i>i</i> )</h3>
 
301
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
302
<p>Writes an unsigned integer, <i>i</i>, to the stream as a 32-bit unsigned integer (<a href="qtglobal.html#quint32-typedef">quint32</a>). Returns a reference to the stream.</p>
 
303
<h3 class="fn"><a name="operator-lt-lt-10"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( float <i>f</i> )</h3>
 
304
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
305
<p>Writes a 32-bit floating point number, <i>f</i>, to the stream using the standard IEEE 754 format. Returns a reference to the stream.</p>
 
306
<h3 class="fn"><a name="operator-lt-lt-11"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( double <i>f</i> )</h3>
 
307
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
308
<p>Writes a 64-bit floating point number, <i>f</i>, to the stream using the standard IEEE 754 format. Returns a reference to the stream.</p>
 
309
<h3 class="fn"><a name="operator-lt-lt-12"></a>QDataStream &amp; QDataStream::operator&lt;&lt; ( const char * <i>s</i> )</h3>
 
310
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
311
<p>Writes the '\0'-terminated string <i>s</i> to the stream and returns a reference to the stream.</p>
 
312
<p>The string is serialized using <a href="qdatastream.html#writeBytes">writeBytes</a>().</p>
 
313
<h3 class="fn"><a name="operator-gt-gt"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( <a href="qtglobal.html#qint8-typedef">qint8</a> &amp; <i>i</i> )</h3>
 
314
<p>Reads a signed byte from the stream into <i>i</i>, and returns a reference to the stream.</p>
 
315
<h3 class="fn"><a name="operator-gt-gt-2"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( bool &amp; <i>i</i> )</h3>
 
316
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
317
<p>Reads a boolean value from the stream into <i>i</i>. Returns a reference to the stream.</p>
 
318
<h3 class="fn"><a name="operator-gt-gt-3"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( <a href="qtglobal.html#quint8-typedef">quint8</a> &amp; <i>i</i> )</h3>
 
319
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
320
<p>Reads an unsigned byte from the stream into <i>i</i>, and returns a reference to the stream.</p>
 
321
<h3 class="fn"><a name="operator-gt-gt-4"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( <a href="qtglobal.html#quint16-typedef">quint16</a> &amp; <i>i</i> )</h3>
 
322
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
323
<p>Reads an unsigned 16-bit integer from the stream into <i>i</i>, and returns a reference to the stream.</p>
 
324
<h3 class="fn"><a name="operator-gt-gt-5"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( <a href="qtglobal.html#qint16-typedef">qint16</a> &amp; <i>i</i> )</h3>
 
325
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
326
<p>Reads a signed 16-bit integer from the stream into <i>i</i>, and returns a reference to the stream.</p>
 
327
<h3 class="fn"><a name="operator-gt-gt-6"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( <a href="qtglobal.html#quint32-typedef">quint32</a> &amp; <i>i</i> )</h3>
 
328
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
329
<p>Reads an unsigned 32-bit integer from the stream into <i>i</i>, and returns a reference to the stream.</p>
 
330
<h3 class="fn"><a name="operator-gt-gt-7"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( <a href="qtglobal.html#qint32-typedef">qint32</a> &amp; <i>i</i> )</h3>
 
331
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
332
<p>Reads a signed 32-bit integer from the stream into <i>i</i>, and returns a reference to the stream.</p>
 
333
<h3 class="fn"><a name="operator-gt-gt-8"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( <a href="qtglobal.html#quint64-typedef">quint64</a> &amp; <i>i</i> )</h3>
 
334
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
335
<p>Reads an unsigned 64-bit integer from the stream, into <i>i</i>, and returns a reference to the stream.</p>
 
336
<h3 class="fn"><a name="operator-gt-gt-9"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( <a href="qtglobal.html#qint64-typedef">qint64</a> &amp; <i>i</i> )</h3>
 
337
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
338
<p>Reads a signed 64-bit integer from the stream into <i>i</i>, and returns a reference to the stream.</p>
 
339
<h3 class="fn"><a name="operator-gt-gt-10"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( float &amp; <i>f</i> )</h3>
 
340
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
341
<p>Reads a 32-bit floating point number from the stream into <i>f</i>, using the standard IEEE 754 format. Returns a reference to the stream.</p>
 
342
<h3 class="fn"><a name="operator-gt-gt-11"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( double &amp; <i>f</i> )</h3>
 
343
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
344
<p>Reads a 64-bit floating point number from the stream into <i>f</i>, using the standard IEEE 754 format. Returns a reference to the stream.</p>
 
345
<h3 class="fn"><a name="operator-gt-gt-12"></a>QDataStream &amp; QDataStream::operator&gt;&gt; ( char *&amp; <i>s</i> )</h3>
 
346
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
347
<p>Reads the '\0'-terminated string <i>s</i> from the stream and returns a reference to the stream.</p>
 
348
<p>Space for the string is allocated using <tt>new</tt> -- the caller must destroy it with <tt>delete[]</tt>.</p>
 
349
<p /><address><hr /><div align="center">
 
350
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
351
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
352
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
353
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
354
</tr></table></div></address></body>
 
355
</html>