QDataStream Class Reference


The QDataStream class provides basic functions for serialization of binary data to a QIODevice. (details) (complete member list)

#include <qdstream.h>

Public Members


Detailed Description

The QDataStream class provides basic functions for 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 easily 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 (LSB first) breaks the portability. We therefore recommend keeping this setting unless you have special needs or 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 of how to serialize to a stream:

  QFile f( "file.dta" );
  f.open( IO_WriteOnly );               // open file for writing
  QDataStream s( &f );                  // serialize using f
  s << "the answer is";                 // serialize string
  s << 42;                              // serialize integer
  f.close();                            // done

Example of how to serialize from a stream:

  QFile f( "file.dta" );
  f.open( IO_ReadOnly );                // open file for reading
  QDataStream s( &f );                  // serialize using f
  char *str;
  int  a;
  s >> str >> a;                        // "the answer is" and 42
  f.close();                            // done
  delete str;                           // delete string

See also: QTextStream.


Member Function Documentation

QDataStream::QDataStream ()

Constructs a data stream that has no IO device.

QDataStream::QDataStream (QIODevice *d)

Constructs a data stream that uses the IO device d.

QDataStream::~QDataStream ()

Destroys the data stream.

The destructor will not affect the current IO device.

int QDataStream::byteOrder () const

Returns the current byte order setting.

See also: setByteOrder().

QIODevice * QDataStream::device () const

Returns the IO device currently set.

bool QDataStream::eos () const

Returns TRUE if the IO device has reached the end position (end of stream) 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.

bool QDataStream::isPrintableData () const

Returns TRUE if the printable data flag has been set.

See also: setPrintableData().

QDataStream & QDataStream::operator<< (UINT8 i)

Writen an unsigned byte to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< (UINT16 i)

Writes an unsigned 16-bit integer to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< (UINT32 i)

Writes an unsigned 32-bit integer to the stream and returns a reference to the stream.

QDataStream & QDataStream::operator<< (uint i)

Writes an unsigned integer to the stream as a 32-bit unsigned integer (UINT32). Returns a reference to the stream.

QDataStream & QDataStream::operator<< (int i)

Writes a signed integer to the stream as a 32-bit signed integer (INT32). Returns a reference to the stream.

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().

QDataStream & QDataStream::operator>> (UINT8 &i)

Reads an unsigned byte from the stream and returns a reference to the stream.

QDataStream & QDataStream::operator>> (UINT16 &i)

Reads an unsigned 16-bit integer from the stream and returns a reference to the stream.

QDataStream & QDataStream::operator>> (UINT32 &i)

Reads an unsigned 32-bit integer from the stream and returns a reference to the stream.

QDataStream & QDataStream::readBytes (char *&s, uint &l)

Reads the buffer s from the stream and returns a reference to the stream.

The buffer s will be allocated with new. Destroy it with the delete operator. If s cannot be allocated, s will be set to 0.

The l parameter will be set to the length of the buffer.

The serialization format is an UINT32 length specifier first, then the data (length bytes).

See also: readRawBytes().

QDataStream & QDataStream::readRawBytes (char *s, uint len)

Reads len bytes from the stream into e s and returns a reference to the stream.

The buffer s must be preallocated.

See also: readBytes() and QIODevice::readBlock().

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.

void QDataStream::setDevice (QIODevice *d)

Sets the IO device to d.

void QDataStream::setPrintableData (bool enable)

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 bigger output).

void QDataStream::unsetDevice ()

Unsets the IO device. This is the same as calling setDevice( 0 ).

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 UINT32, followed by len bytes from s.

See also: writeRawBytes().

QDataStream & QDataStream::writeRawBytes (const char *s, uint len)

Writes len bytes from s to the stream and returns a reference to the stream.

The len is serialized as an UINT32, followed by len bytes from s.

See also: writeBytes(), QIODevice::writeBlock().


This file is part of the Qt toolkit, copyright 1995 Troll Tech, all rights reserved.

It was generated from the following files:


Generated at 04:27, 1995/05/20 by the webmaster at Troll Tech