StreamReader and StreamWriter are not like their equivalents in other languages, as MoSync doesn't expose Streams in the same way. What they are is an attempt to be able to deal with data objects and local storage as if they were Streams.
StreamReader and StreamWriter provide an organised method for serial data access to your saved data. They can each read or write a wide variety of data types to and from a MoSync data object which can be stored on the local device. This means that you don't have to worry too much about the mechanics of reading or writing data in an organised way.
Strings are prepended with the length of the String, so you don't need to know the length in advance when you read it back.
Usage:
MAHandle myData = maCreatePlaceholder();
maCreateData(myData, 4000);
StreamWriter sw;
sw.setSource(myData);
sw.WriteString(myString.c_str, 40); //Writes 40 characters from a string
sw.WriteUInt16(13); //Writes the number 13 as an unsigned int in two bytes (short);
sw.WriteInt32(4832); //Writes the number 4832 as a signed int in four bytes;
sw.WriteDateTime(currentTime); //Writes a time_t int to the data
sw.WriteBytes(&buffer, 30); //Writes 30 bytes from a byte array.
StreamReader sr;
sr.setSource(myData);
lprintlfn("Saved String: %s", sr.ReadString().c_str());
lprintfln("Saved unsigned short %d", sr.ReadUInt16());
| Attachment | Size |
|---|---|
| StreamUtils.zip | 4.85 KB |