indigo. io. datastream

Created30.06.2005
Last modified10.08.2005

Provides a platform-independent DataStream to serialize binary data.

Summary
This exception gets thrown by DataStream if a format error occurs.
Creates a new exception with the message msg.
This class provides serialization of binary data in a platform-independent way.
This enum specifies the byte order used for serializing data.
Creates a new, unconnected DataStream.
Creates a new DataStream that is connected to the IODevice dev.
Returns the device that is currently connected to the stream.
Returns the ByteOrder data is serialized in.
Writes the contents of data directly to the stream.
Reads the contents of data directly from the stream.
Writes a single byte to the stream.
Writes an array of bytes to the stream.
Reads a single byte from the stream.
Reads an array of bytes from the stream.

DataStreamException

This exception gets thrown by DataStream if a format error occurs.  Note that the underlying stream will still throw IODeviceExceptions on errors.  Receive the error description with toString().  You should also throw this exception in your serialization functions if they encounter an error.

Summary
Creates a new exception with the message msg.

Functions

this()

public this(char[] msg)

Creates a new exception with the message msg.

DataStream

This class provides serialization of binary data in a platform-independent way.  It will work with any IODevice.  To serialize some data to a File, for example, write code like this:

int someInt;
wchar[] someWString;
// ...

try
{
auto File file = new File("data.bin");
file.open(IODevice.WriteOnly | IODevice.Truncate);

DataStream stream = new DataStream(file);

stream << cast(uint) 0x3579; // "Magic number".
stream << someInt;
stream << someWString;
}
catch (Exception e)
{
printf("Serialization failed.\n");
e.print();
}

This example already includes full error handling.  DataStream provides the shifting operators for all basic D types and dynamic arrays of them.  Reading back the data is roughly the same (error handling stripped):

file.open(IODevice.ReadOnly);
// ...

uint magic;
stream >> magic;

if (magic != 0x3579)
throw new DataStreamException("Unrecognized file format.");

stream >> someInt;
stream >> someWString;

If DataStream cannot read what you requested because the stream signals EOF, it will throw a DataStreamException.  Note that it cannot recover from uncomplete reading operations, thus you always have to provide enough data (this is particularly important when reading from “slow” devices like sockets).

As you can see in the above examples, it is very important to write only fixed-size types, or “fix” their size with a cast before writing.  Do not send types or expressions of unknown size (like size_t, someArray.length, 2983922) into the stream.  Luckily, the D basic types all have a fixed size ─ except real, which consequently DataStream has no shifting operators for.

There are also two functions to deal with raw binary data: writeRawData() and readRawData().  You can control the stream’s behaviour with device and byteOrder.  Note that the default ByteOrder is LittleEndian, in contrast to Qt’s QDataStream where it is BigEndian.  You have to switch it if you try to read files from Qt.

If you want to make your own types serializable with DataStream, give them these two member functions:

void readFrom(DataStream st);
void writeTo(DataStream st);

Containers like Vector will automatically provide these functions if your types do as well.  Note that this will not work for classes, as they need to be allocated at first.  You have two options to make them serializable:

  • Provide a default constructor this().  The container will create a new object with this constructor, and then call readFrom() on it.
  • Provide a static function createFrom() which returns a new object created from the stream.  With this approach it is even possible to serialize objects created from derived classes, and save them into a container that manages references to their base class.
static MyClass createFrom(DataStream st);

The second method is the one preferred by the Indigo containers.

Summary
This enum specifies the byte order used for serializing data.
Creates a new, unconnected DataStream.
Creates a new DataStream that is connected to the IODevice dev.
Returns the device that is currently connected to the stream.
Returns the ByteOrder data is serialized in.
Writes the contents of data directly to the stream.
Reads the contents of data directly from the stream.
Writes a single byte to the stream.
Writes an array of bytes to the stream.
Reads a single byte from the stream.
Reads an array of bytes from the stream.

Enumerations

ByteOrder

This enum specifies the byte order used for serializing data.

LittleEndianLess significant byte first (the default).
BigEndianMost significant byte first.

Functions and Properties

this()

this()

Creates a new, unconnected DataStream.  Provide a device with device.

this(IODevice)

this(IODevice dev)

Creates a new DataStream that is connected to the IODevice dev.

device

Read

Returns the device that is currently connected to the stream.

Write

Changes the connected device.  You can provide null as the device, which will simply unconnect from the current device (so it can be freed).

byteOrder

Read

Returns the ByteOrder data is serialized in.

Write

Sets the ByteOrder data is serialized in.

writeRawData()

final void writeRawData(void[] data)

Writes the contents of data directly to the stream.  Note that no encoding of the data is done, thus you have to care for machine independentness yourself.  Avoid this function if possible.

See also readRawData().

readRawData()

final void readRawData(void[] data)

Reads the contents of data directly from the stream.  Note that no decoding of the data is done, thus you have to care for machine independentness yourself.  Avoid this function if possible.

See also writeRawData().

Operators

opShl(byte)

final DataStream opShl(byte v)

Writes a single byte to the stream.  There are overloads of this function for every basic D type.

opShl(byte[])

final DataStream opShl(byte[] v)

Writes an array of bytes to the stream.  Before the array’s contents the length of the array is saved.  There are overloads of this function for every array of basic D types.

opShr(byte)

final DataStream opShr(out byte v)

Reads a single byte from the stream.  There are overloads of this function for every basic D type.

opShr()

final DataStream opShr(inout byte[] v)

Reads an array of bytes from the stream.  At first the length of the array is read, and the provided array length is changed accordingly.  That can lead to reallocation of it, but it need not to.  Consider the old contents of v trashed.  There are overloads of this function for every array of basic D types.  Character arrays are automatically null-terminated to make them usable with C functions.

This class provides serialization of binary data in a platform-independent way.
public this(char[] msg)
Creates a new exception with the message msg.
this()
Creates a new, unconnected DataStream.
this(IODevice dev)
Creates a new DataStream that is connected to the IODevice dev.
This class provides a common interface for devices that support reading and writing of blocks of data, such as File.
This enum specifies the byte order used for serializing data.
final void writeRawData(void[] data)
Writes the contents of data directly to the stream.
final void readRawData(void[] data)
Reads the contents of data directly from the stream.
final DataStream opShl(byte v)
Writes a single byte to the stream.
final DataStream opShl(byte[] v)
Writes an array of bytes to the stream.
final DataStream opShr(out byte v)
Reads a single byte from the stream.
final DataStream opShr(inout byte[] v)
Reads an array of bytes from the stream.
This exception gets thrown by IODevice and its subclasses if an I/O error occurs.
This class provides an interface to read from and write to files.
This exception gets thrown by DataStream if a format error occurs.
Returns the device that is currently connected to the stream.
Returns the ByteOrder data is serialized in.
Wraps the builtin dynamic arrays.