The QDataStream class provides serialization of binary data to a QIODevice. More...
#include <qdatastream.h>
Public Types | |
enum | ByteOrder { BigEndian, LittleEndian } |
Private Member Functions | |
QDataStream (const QDataStream &) | |
QDataStream & | operator= (const QDataStream &) |
Private Attributes | |
QIODevice * | dev |
bool | owndev |
int | byteorder |
bool | printable |
bool | noswap |
int | ver |
The QDataStream class provides serialization of binary data to a QIODevice.
A data stream is a binary stream of encoded information which is 100% independent of the host computer operation system, CPU or byte order. A stream that is written by a PC under DOS/Windows can be read by a Sun SPARC running Solaris.
The QDataStream class implements serialization of primitive types, like char
, short
, int
, char*
etc. Serialization of more complex data is accomplished by breaking up the data into primitive units.
The programmer can select which byte order to use when serializing data. The default setting is big endian (MSB first). Changing it to little endian breaks the portability (unless the reader also changes to little endian). We recommend keeping this setting unless you have special requirements.
A data stream cooperates closely with a QIODevice. A QIODevice represents an input/output medium one can read data from and write data to. The QFile class is an example of an IO device.
Example (write data to a stream):
Example (read data from a stream):
In the last example, if you read into a QString instead of a char*
you do not have to delete it.
Normally, each item written to the stream is written in a fixed binary format. For example, a char*
is written as a 32-bit integer equal to the length of the string including the NUL byte, followed by all the characters of the string including the NUL byte. Similarly when reading a string, 4 bytes are read to create the 32-bit length value, then that many characters for the string including the NUL. For a complete description of all Qt types supporting data streaming see Format of the QDataStream operators .
If you want a "parsing" input stream, see QTextStream. If you just want the data to be human-readable to aid in debugging, you can set the data stream into printable data mode with setPrintableData(). The data is then written slower, in a human readable bloated form that is sufficient for debugging.
If you are producing a new binary data format, such as a file format for documents created by your application, you could use a QDataStream to write the data in a portable format. Typically, you would write a brief header containing a magic string and a version number to give yourself room for future expansion. For example:
Then read it in with:
Definition at line 47 of file qdatastream.h.
Enumerator | |
---|---|
BigEndian | |
LittleEndian |
Definition at line 62 of file qdatastream.h.
QDataStream::QDataStream | ( | ) |
Constructs a data stream that has no IO device.
Definition at line 195 of file qdatastream.cpp.
QDataStream::QDataStream | ( | QIODevice * | d | ) |
Constructs a data stream that uses the IO device d.
Definition at line 213 of file qdatastream.cpp.
QDataStream::QDataStream | ( | QByteArray | a, |
int | mode | ||
) |
Constructs a data stream that operates on a byte array through an internal QBuffer device.
The QArray::setRawData() function is not for the inexperienced.
Definition at line 242 of file qdatastream.cpp.
|
virtual |
Destructs the data stream.
The destructor will not affect the current IO device, unless it is an internal IO device processing a QByteArray passed in the constructor.
Definition at line 262 of file qdatastream.cpp.
|
private |
|
inline |
Returns TRUE if the IO device has reached the end position (end of stream or file) or if there is no IO device set.
Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position.
Definition at line 126 of file qdatastream.h.
|
inline |
Returns the current byte order setting - either BigEndian
or LittleEndian
.
Definition at line 132 of file qdatastream.h.
|
inline |
Returns the IO device currently set.
Definition at line 123 of file qdatastream.h.
|
inline |
Returns TRUE if the IO device has reached the end position (end of stream or file) or if there is no IO device set.
Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position.
Definition at line 129 of file qdatastream.h.
|
inline |
Returns TRUE if the printable data flag has been set.
Definition at line 135 of file qdatastream.h.
QDataStream & QDataStream::operator<< | ( | Q_INT8 | i | ) |
Writes a signed byte to the stream.
Definition at line 698 of file qdatastream.cpp.
|
inline |
Writes an unsigned byte to the stream and returns a reference to the stream.
Definition at line 159 of file qdatastream.h.
QDataStream & QDataStream::operator<< | ( | Q_INT16 | i | ) |
Writes a signed 16-bit integer to the stream and returns a reference to the stream.
Definition at line 727 of file qdatastream.cpp.
|
inline |
Writes an unsigned 16-bit integer to the stream and returns a reference to the stream.
Definition at line 162 of file qdatastream.h.
QDataStream & QDataStream::operator<< | ( | Q_INT32 | i | ) |
Writes a signed 32-bit integer to the stream and returns a reference to the stream.
Writes a signed integer to the stream as a 32-bit signed integer (Q_INT32). Returns a reference to the stream.
Definition at line 758 of file qdatastream.cpp.
|
inline |
Writes an unsigned 32-bit integer to the stream and returns a reference to the stream.
Definition at line 165 of file qdatastream.h.
QDataStream & QDataStream::operator<< | ( | Q_INT64 | i | ) |
Writes a signed 64-bit integer to the stream and returns a reference to the stream, or calls the Q_INT32-operator if 64 bit is not available.
Definition at line 790 of file qdatastream.cpp.
|
inline |
Writes an unsigned 64-bit integer to the stream and returns a reference to the stream, or uses the Q_UINT32-operator if 64 bit is not available.
Definition at line 168 of file qdatastream.h.
QDataStream & QDataStream::operator<< | ( | float | f | ) |
Writes a 32-bit floating point number to the stream using the standard IEEE754 format. Returns a reference to the stream.
Definition at line 836 of file qdatastream.cpp.
QDataStream & QDataStream::operator<< | ( | double | f | ) |
Writes a 64-bit floating point number to the stream using the standard IEEE754 format. Returns a reference to the stream.
Definition at line 866 of file qdatastream.cpp.
QDataStream & QDataStream::operator<< | ( | const char * | s | ) |
Writes the '\0'-terminated string s to the stream and returns a reference to the stream.
The string is serialized using writeBytes().
Definition at line 899 of file qdatastream.cpp.
|
private |
QDataStream & QDataStream::operator>> | ( | Q_INT8 & | i | ) |
Reads a signed byte from the stream.
Definition at line 429 of file qdatastream.cpp.
|
inline |
Reads an unsigned byte from the stream and returns a reference to the stream.
Definition at line 147 of file qdatastream.h.
QDataStream & QDataStream::operator>> | ( | Q_INT16 & | i | ) |
Reads a signed 16-bit integer from the stream and returns a reference to the stream.
Definition at line 457 of file qdatastream.cpp.
|
inline |
Reads an unsigned 16-bit integer from the stream and returns a reference to the stream.
Definition at line 150 of file qdatastream.h.
QDataStream & QDataStream::operator>> | ( | Q_INT32 & | i | ) |
Reads a signed 32-bit integer from the stream and returns a reference to the stream.
Definition at line 486 of file qdatastream.cpp.
|
inline |
Reads an unsigned 32-bit integer from the stream and returns a reference to the stream.
Definition at line 153 of file qdatastream.h.
QDataStream & QDataStream::operator>> | ( | Q_INT64 & | i | ) |
Reads a signed 64-bit integer from the stream and returns a reference to the stream, or uses the Q_UINT32 operator if 64 bit is not available.
Definition at line 516 of file qdatastream.cpp.
|
inline |
Reads an unsigned 64-bit integer from the stream and returns a reference to the stream, or uses the Q_UINT32 operator if 64 bit is not available.
Definition at line 156 of file qdatastream.h.
QDataStream & QDataStream::operator>> | ( | float & | f | ) |
Reads a 32-bit floating point number from the stream using the standard IEEE754 format. Returns a reference to the stream.
Definition at line 561 of file qdatastream.cpp.
QDataStream & QDataStream::operator>> | ( | double & | f | ) |
Reads a 64-bit floating point number from the stream using the standard IEEE754 format. Returns a reference to the stream.
Definition at line 586 of file qdatastream.cpp.
QDataStream & QDataStream::operator>> | ( | char *& | s | ) |
Reads the '\0'-terminated string s from the stream and returns a reference to the stream.
Space for the string is allocated using new
- the caller must eventually call delete[] on the value.
Definition at line 618 of file qdatastream.cpp.
QDataStream & QDataStream::readBytes | ( | char *& | s, |
uint & | l | ||
) |
Reads the buffer s from the stream and returns a reference to the stream.
The buffer s is allocated using new
. Destroy it with the delete
[] operator. If the length is zero or s cannot be allocated, s is set to 0.
The l parameter will be set to the length of the buffer.
The serialization format is an Q_UINT32 length specifier first, then the data (l bytes).
Definition at line 641 of file qdatastream.cpp.
QDataStream & QDataStream::readRawBytes | ( | char * | s, |
uint | len | ||
) |
Reads len bytes from the stream into s and returns a reference to the stream.
The buffer s must be preallocated.
Definition at line 669 of file qdatastream.cpp.
void QDataStream::setByteOrder | ( | int | bo | ) |
Sets the serialization byte order to bo.
The bo parameter can be QDataStream::BigEndian
or QDataStream::LittleEndian
.
The default setting is big endian. We recommend leaving this setting unless you have special requirements.
Definition at line 345 of file qdatastream.cpp.
void QDataStream::setDevice | ( | QIODevice * | d | ) |
void QDataStream::setDevice(QIODevice *d ) Sets the IO device to d.
Definition at line 281 of file qdatastream.cpp.
|
inline |
Sets or clears the printable data flag.
If this flag is set, the write functions will generate output that consists of printable characters (7 bit ASCII).
We recommend enabling printable data only for debugging purposes (it is slower and creates larger output).
Definition at line 138 of file qdatastream.h.
|
inline |
Sets the version number of the data serialization format.
In order to accomodate for new functionality, the datastream serialization format of some Qt classes has changed in some versions of Qt. If you want to read data that was created by an earlier version of Qt, or write data that can be read by a program that was compiled with an earlier version of Qt, use this function to modify the serialization format of QDataStream.
For Qt 1.x compatibility, use v == 1.
For Qt 2.0.x compatibility, use v == 2 (Not required for reading in Qt 2.1).
Definition at line 144 of file qdatastream.h.
void QDataStream::unsetDevice | ( | ) |
Unsets the IO device. This is the same as calling setDevice( 0 ).
Definition at line 295 of file qdatastream.cpp.
|
inline |
Returns the version number of the data serialization format. In Qt 2.1, this number is by default 3.
Definition at line 141 of file qdatastream.h.
QDataStream & QDataStream::writeBytes | ( | const char * | s, |
uint | len | ||
) |
Writes the length specifier len and the buffer s to the stream and returns a reference to the stream.
The len is serialized as an Q_UINT32, followed by len bytes from s.
Definition at line 921 of file qdatastream.cpp.
QDataStream & QDataStream::writeRawBytes | ( | const char * | s, |
uint | len | ||
) |
Writes len bytes from s to the stream and returns a reference to the stream.
Definition at line 938 of file qdatastream.cpp.
|
private |
Definition at line 106 of file qdatastream.h.
|
private |
Definition at line 104 of file qdatastream.h.
|
private |
Definition at line 108 of file qdatastream.h.
|
private |
Definition at line 105 of file qdatastream.h.
|
private |
Definition at line 107 of file qdatastream.h.
|
private |
Definition at line 109 of file qdatastream.h.