CopperSpice API
1.9.2
|
The QDataStream class provides serialization of binary data to a QIODevice. More...
Public Types | |
enum | ByteOrder |
enum | FloatingPointPrecision |
enum | Status |
enum | Version |
Public Methods | |
QDataStream () | |
QDataStream (const QByteArray &buffer) | |
QDataStream (QByteArray *buffer, QIODevice::OpenMode mode) | |
QDataStream (QIODevice *device) | |
virtual | ~QDataStream () |
bool | atEnd () const |
ByteOrder | byteOrder () const |
QIODevice * | device () const |
FloatingPointPrecision | floatingPointPrecision () const |
QDataStream & | operator<< (bool i) |
QDataStream & | operator<< (const char *str) |
QDataStream & | operator<< (double f) |
QDataStream & | operator<< (float f) |
QDataStream & | operator<< (long i) |
QDataStream & | operator<< (qint16 i) |
QDataStream & | operator<< (qint32 i) |
QDataStream & | operator<< (qint64 i) |
QDataStream & | operator<< (qint8 i) |
QDataStream & | operator<< (quint16 i) |
QDataStream & | operator<< (quint32 i) |
QDataStream & | operator<< (quint64 i) |
QDataStream & | operator<< (quint8 i) |
QDataStream & | operator<< (unsigned long i) |
QDataStream & | operator>> (bool &i) |
QDataStream & | operator>> (char *&str) |
QDataStream & | operator>> (double &f) |
QDataStream & | operator>> (float &f) |
QDataStream & | operator>> (long &i) |
QDataStream & | operator>> (qint16 &i) |
QDataStream & | operator>> (qint32 &i) |
QDataStream & | operator>> (qint64 &i) |
QDataStream & | operator>> (qint8 &i) |
QDataStream & | operator>> (quint16 &i) |
QDataStream & | operator>> (quint32 &i) |
QDataStream & | operator>> (quint64 &i) |
QDataStream & | operator>> (quint8 &i) |
QDataStream & | operator>> (unsigned long &i) |
QDataStream & | readBytes (char *&buffer, uint &len) |
int | readRawData (char *buffer, int len) |
void | resetStatus () |
void | setByteOrder (ByteOrder order) |
void | setDevice (QIODevice *device) |
void | setFloatingPointPrecision (FloatingPointPrecision precision) |
void | setStatus (Status status) |
void | setVersion (int version) |
int | skipRawData (int len) |
Status | status () const |
int | version () const |
QDataStream & | writeBytes (const char *buffer, uint len) |
int | writeRawData (const char *buffer, int len) |
Related Functions | |
These are not member functions | |
QDataStream & | operator<< (QDataStream &stream, const QFlatMap< Key, Val, C > &flatmap) |
QDataStream & | operator<< (QDataStream &stream, const QHash< Key, Val, Hash, KeyEqual > &hash) |
QDataStream & | operator<< (QDataStream &stream, const QLinkedList< T > &list) |
QDataStream & | operator<< (QDataStream &stream, const QList< T > &list) |
QDataStream & | operator<< (QDataStream &stream, const QMap< Key, Val, C > &map) |
QDataStream & | operator<< (QDataStream &stream, const QMultiHash< Key, Val, Hash, KeyEqual > &hash) |
QDataStream & | operator<< (QDataStream &stream, const QMultiMap< Key, Val, C > &map) |
QDataStream & | operator<< (QDataStream &stream, const QSet< T > &set) |
QDataStream & | operator<< (QDataStream &stream, const QVector< T > &vector) |
QDataStream & | operator>> (QDataStream &stream, QFlatMap< Key, Val, C > &flatmap) |
QDataStream & | operator>> (QDataStream &stream, QHash< Key, Val, Hash, KeyEqual > &hash) |
QDataStream & | operator>> (QDataStream &stream, QLinkedList< T > &list) |
QDataStream & | operator>> (QDataStream &stream, QList< T > &list) |
QDataStream & | operator>> (QDataStream &stream, QMap< Key, Val, C > &map) |
QDataStream & | operator>> (QDataStream &stream, QMultiHash< Key, Val, Hash, KeyEqual > &hash) |
QDataStream & | operator>> (QDataStream &stream, QMultiMap< Key, Val, C > &map) |
QDataStream & | operator>> (QDataStream &stream, QSet< T > &set) |
QDataStream & | operator>> (QDataStream &stream, QVector< T > &vector) |
The QDataStream class provides serialization of binary data to a QIODevice. A QIODevice represents an input/output medium one can read data from and write data to. The QFile class is an example of an I/O device.
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. You can use a data stream to read/write raw unencoded binary data. If you want a text based input stream refer to QTextStream.
The QDataStream class implements the serialization of C++'s basic data types, like char
, short
, int
, char *
, etc. Serialization of more complex data is accomplished by breaking up the data into primitive units.
The following is an example to write binary data to a stream.
The following is an example to read binary data from a stream.
Each item written to the stream is written in a predefined binary format that varies depending on the item's type. Supported types include QBrush, QColor, QDateTime, QFont, QPixmap, QString, QVariant and many others. For the complete list of supported streaming data types refer to Serializing Data Types.
For integers it is best to always cast to a CopperSpice integer type for writing, and to read back into the same CopperSpice integer type. This ensures that you get integers of the size you want and insulates you from compiler and platform differences.
To take one example, a char *
string is written as a 32-bit integer equal to the length of the string including the '\0' byte, followed by all the characters of the string including the '\0' byte. When reading a char *
string, 4 bytes are read to create the 32-bit length value, then that many characters for the char *
string including the '\0' terminator are read.
The initial I/O device is usually set in the constructor, but can be changed with setDevice(). If you have reached the end of the data (or if there is no I/O device set) atEnd() will return true.
When inputting or outputting complex types, it is very important to make sure the same version of the stream (version()) is used for reading and writing. If you need both forward and backward compatibility, you can hardcode the version number in the application:
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.
Then read it in with the following code.
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.
Data may be read from a stream into a preallocated char *
using readRawData(). In addition, data can be written to a stream using writeRawData(). Any encoding/decoding of the data must be done in your program.
A similar pair of functions is readBytes() and writeBytes(). These differ from their raw counterparts as follows: readBytes() reads a quint32 which is taken to be the length of the data to be read, then that number of bytes is read into the preallocated char *
, writeBytes() writes a quint32 containing the length of the data, followed by the data. Any encoding/decoding of the data (apart from the length quint32) must be done in your code.
The CopperSpice container classes can be written to or read from a QDataStream. These classes include QList, QLinkedList, QVector, QSet, QHash, QMultiMap, QMap, and QMultiMap. Most of the stream operators are functions.
The following is an example of how a non container class supports writing to or reading from a QDataStream. QXxx can be a variety of different classes. Review the QDataStream documentation for a full list.
The following operator functions support reading and writing a QImage.
The byte order used for reading/writing the data.
Constant | Value | Description |
---|---|---|
QDataStream::BigEndian | QSysInfo::BigEndian | Most significant byte first (the default) |
QDataStream::LittleEndian | QSysInfo::LittleEndian | Least significant byte first |
The precision of floating point numbers used for reading/writing the data.
Constant | Value | Description |
---|---|---|
QDataStream::SinglePrecision | 0 | All floating point numbers in the data stream have 32-bit precision. |
QDataStream::DoublePrecision | 1 | All floating point numbers in the data stream have 64-bit precision. |
enum QDataStream::Status |
This enum describes the current status of the data stream.
Constant | Value | Description |
---|---|---|
QDataStream::Ok | 0 | The data stream is operating normally. |
QDataStream::ReadPastEnd | 1 | The data stream has read past the end of the data in the underlying device. |
QDataStream::ReadCorruptData | 2 | The data stream has read corrupt data. |
QDataStream::WriteFailed | 3 | The data stream can not write to the underlying device. |
enum QDataStream::Version |
This enum provides symbolic synonyms for the data serialization format version numbers.
Constant | Value | Description |
---|---|---|
QDataStream::CS_1_0 | 128 | Version 1.0 |
QDataStream::CS_1_1 | 128 | Version 1.0 |
QDataStream::CS_1_2 | 128 | Version 1.0 |
QDataStream::CS_1_3 | 128 | Version 1.0 |
QDataStream::CS_1_4 | 128 | Version 1.0 |
QDataStream::CS_1_5 | 128 | Version 1.0 |
QDataStream::CS_1_6 | 128 | Version 1.0 |
QDataStream::CS_1_7 | 128 | Version 1.0 |
QDataStream::QDataStream | ( | ) |
Constructs a data stream that has no I/O device.
|
explicit |
Constructs a data stream that uses the given I/O device.
QDataStream::QDataStream | ( | QByteArray * | buffer, |
QIODevice::OpenMode | mode | ||
) |
Constructs a data stream that operates on a byte array buffer. The mode describes how the device is to be used. Alternatively, you can use QDataStream(const QByteArray &) if you just want to read from a byte array.
QDataStream::QDataStream | ( | const QByteArray & | buffer | ) |
Constructs a read only data stream that operates on byte array buffer. Use QDataStream(QByteArray*, int) if you want to write to a byte array.
|
virtual |
Destroys the data stream.
The destructor will not affect the current I/O device, unless it is an internal I/O device (e.g. a QBuffer) processing a QByteArray passed in the constructor, in which case the internal I/O device is destroyed.
bool QDataStream::atEnd | ( | ) | const |
Returns true if the I/O device has reached the end position (end of the stream or file) or if there is no I/O device set, otherwise returns false.
|
inline |
Returns the current byte order setting, either BigEndian or LittleEndian.
|
inline |
Returns the I/O device currently set or a nullptr if no device is currently set.
FloatingPointPrecision QDataStream::floatingPointPrecision | ( | ) | const |
Returns the floating point precision of the data stream.
QDataStream & QDataStream::operator<< | ( | bool | i | ) |
Writes a boolean i to the stream and returns a reference to the stream.
QDataStream & QDataStream::operator<< | ( | const char * | str | ) |
Writes a null terminated string str to the stream and returns a reference to the stream. The string is serialized using writeBytes().
QDataStream & QDataStream::operator<< | ( | double | f | ) |
Writes a floating point number f to the stream using the standard IEEE 754 format. Returns a reference to the stream.
QDataStream & QDataStream::operator<< | ( | float | f | ) |
Writes a floating point number f to the stream using the standard IEEE 754 format. Returns a reference to the stream.
QDataStream & QDataStream::operator<< | ( | long | i | ) |
Writes a long i to the stream and returns a reference to the stream.
QDataStream & QDataStream::operator<< | ( | qint16 | i | ) |
Writes a signed 16-bit integer i to the stream and returns a reference to the stream.
QDataStream & QDataStream::operator<< | ( | qint32 | i | ) |
Writes a signed 32-bit integer i to the stream and returns a reference to the stream.
QDataStream & QDataStream::operator<< | ( | qint64 | i | ) |
Writes a signed 64-bit integer i to the stream and returns a reference to the stream.
QDataStream & QDataStream::operator<< | ( | qint8 | i | ) |
Writes a signed 8-bit integer i to the stream and returns a reference to the stream.
|
inline |
Writes an unsigned 16-bit integer i to the stream and returns a reference to the stream.
|
inline |
Writes an unsigned 32-bit integer i to the stream and returns a reference to the stream.
|
inline |
Writes an unsigned 64-bit integer i to the stream and returns a reference to the stream.
|
inline |
Writes an unsigned 8-bit integer i to the stream and returns a reference to the stream.
QDataStream & QDataStream::operator<< | ( | unsigned long | i | ) |
Writes an unsigned long i to the stream and returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | bool & | i | ) |
Reads a boolean from the stream into i and returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | char *& | str | ) |
Reads a null terminated string str from the stream and returns a reference to the stream. Space for the string is allocated using new
, the caller must destroy it with delete[]
.
QDataStream & QDataStream::operator>> | ( | double & | f | ) |
Reads a floating point number from the stream into f using the standard IEEE 754 format. Returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | float & | f | ) |
Reads a floating point number from the stream into f using the standard IEEE 754 format. Returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | long & | i | ) |
Reads a long from the stream into i and returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | qint16 & | i | ) |
Reads a signed 16-bit integer from the stream into i and returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | qint32 & | i | ) |
Reads a signed 32-bit integer from the stream into i and returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | qint64 & | i | ) |
Reads a signed 64-bit integer from the stream into i and returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | qint8 & | i | ) |
Reads a signed 8-bit integer from the stream into i and returns a reference to the stream.
|
inline |
Reads an unsigned 16-bit integer from the stream into i and returns a reference to the stream.
|
inline |
Reads an unsigned 32-bit integer from the stream into i and returns a reference to the stream.
|
inline |
Reads an unsigned 64-bit integer from the stream into i and returns a reference to the stream.
|
inline |
Reads an unsigned 8-bit integer from the stream into i and returns a reference to the stream.
QDataStream & QDataStream::operator>> | ( | unsigned long & | i | ) |
Reads a unsigned long from the stream into i and returns a reference to the stream.
QDataStream & QDataStream::readBytes | ( | char *& | buffer, |
uint & | len | ||
) |
Reads the buffer from the stream and returns a reference to the stream. The buffer s is allocated using new
. Destroy it with the delete[]
operator.
The len parameter is set to the length of the buffer. If the string read is empty, len is set to 0 and buffer is set to a null pointer. The serialization format is a quint32 length specifier first, then len bytes of data.
int QDataStream::readRawData | ( | char * | buffer, |
int | len | ||
) |
Reads at most len bytes from the stream into s and returns the number of bytes read. If an error occurs, this method returns -1. The buffer s must be preallocated. The data is not encoded.
void QDataStream::resetStatus | ( | ) |
Resets the status of the data stream.
void QDataStream::setByteOrder | ( | ByteOrder | order | ) |
Sets the serialization byte order to order. The order parameter can be QDataStream::BigEndian or QDataStream::LittleEndian.
The default setting is big endian. We recommend leaving this setting unless you have special requirements.
void QDataStream::setDevice | ( | QIODevice * | device | ) |
Sets the I/O device to device which can be nullptr to unset the current I/O device.
void QDataStream::setFloatingPointPrecision | ( | FloatingPointPrecision | precision | ) |
Sets the floating point precision of the data stream to precision.
If the floating point precision is DoublePrecision all floating point numbers will be written and read with 64-bit precision. If the floating point precision is SinglePrecision all floating point numbers will be written and read with 32-bit precision.
The default is DoublePrecision.
void QDataStream::setStatus | ( | Status | status | ) |
Sets the status of the data stream to the status given. Subsequent calls to setStatus() are ignored until resetStatus() is called.
|
inline |
Sets the version number of the data serialization format to version.
To accommodate new functionality the datastream serialization format may change. If you want to read data which was created by an earlier version of CopperSpice or write data which can be read by a program compiled with an earlier version, use this function to modify the serialization format used by QDataStream.
The Version enum provides symbolic constants for the different versions.
int QDataStream::skipRawData | ( | int | len | ) |
Skips len bytes from the device. Returns the number of bytes actually skipped, or -1 on error. This is equivalent to calling readRawData() on a buffer of length len and ignoring the buffer.
Status QDataStream::status | ( | ) | const |
Returns the status of the data stream.
|
inline |
Returns the version number of the data serialization format.
QDataStream & QDataStream::writeBytes | ( | const char * | buffer, |
uint | len | ||
) |
Writes the length specifier len and buffer to the stream and returns a reference to the stream. The len is serialized as a quint32, followed by len bytes from buffer. The data is not encoded.
int QDataStream::writeRawData | ( | const char * | buffer, |
int | len | ||
) |
Writes len bytes from buffer to the stream. Returns the number of bytes actually written, or -1 on error. The data is not encoded.
|
related |
Writes the given flatmap to the stream. Returns a reference to the stream. This function requires the type T to support operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Writes the given hash to the stream. Returns a reference to the stream. Requires the type T to support operator<<().
Refer to Serializing Data Types for additional information.
|
related |
Writes the given list to the stream. Returns a reference to the stream. This function requires the type T to support operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Writes the given list to the stream. Returns a reference to the stream. This function requires the type T to support operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Writes the given map to the stream. Returns a reference to the stream. This function requires the types Key and Val to support operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Writes the given hash to the stream. Returns a reference to the stream. This function requires the type T to support operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Writes the given map to the stream. Returns a reference to the stream. This function requires the key and value types to implement operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Writes the given set to the stream. Returns a reference to the stream. Returns a reference to the stream. This function requires the type T to support operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Writes the given vector to the stream. Returns a reference to the stream. This function requires the type T to support operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given flatmap. Returns a reference to the stream. This function requires the type T to support operator>>()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given hash. Returns a reference to the stream. Requires the type T to support operator>>().
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given list. Returns a reference to the stream. This function requires the type T to support operator>>()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given list. Returns a reference to the stream. This function requires the type T to support operator>>()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given map. Returns a reference to the stream. This function requires the types Key and Val to support operator>>()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given hash. Returns a reference to the stream. This function requires the type T to support operator>>()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given map. Returns a reference to the stream. This function requires the key and value types to implement operator>>()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given set. Returns a reference to the stream. This function requires the type T to support operator>>()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given vector. Returns a reference to the stream. This function requires the type T to support operator>>()
.
Refer to Serializing Data Types for additional information.