Utilities - StreamReader and StreamWriter

3 posts / 0 new
Last post
Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:
Utilities - StreamReader and StreamWriter

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

AttachmentSize
StreamUtils.zip4.85 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dgrasberger
dgrasberger's picture
Offline
Joined: 9 Jul 2011
Posts:

I altered StreadReader to take a stream length parameter to constructor so it can check length before calling maReadData() to avoid panics. Let me know if you're interested in my modifications.

Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Sure, post the code here and I'll update the release