Compilable code example of using the Symbian Streaming API. Demonstrates how to store the whole object with the boolean, integer and descriptor data fields to and from stream.
// Let the data format be:
//
//
// Note, that when dealing with packaged data, we have to be careful about
// the concrete size in bytes. Descriptors will be saved using Symbian
// default format for descriptors. For TBool we use the default conversion
// to TInt
void CEmployee::ExternalizeL( RWriteStream &aStream ) const
{
// Write the used protocol version first
aStream << KEmployeeProtocolVersion;
aStream << static_cast ( iAge );
aStream << static_cast ( iFired );
aStream << static_cast ( iName->Length() );
aStream << *iName;
aStream << static_cast ( iSurname->Length() );
aStream << *iSurname;
}
/**
* Demo class to externalize/internalize
*/
class CEmployee : public CBase
{
...
private:
HBufC* iName;
HBufC* iSurname;
TInt iAge;
TBool iFired;
};
// The actual test
void DemoStreamingL( CConsoleBase& aConsole )
{
// TBuf8, not TBuf. Storage format is not dependent on the
// machine architecture
TBuf8 storage;
RDesWriteStream writer( storage );
// Whatever happens, resources will be cleaned up correctly
CleanupClosePushL( writer );
...
// Store the source object
writer << *source;
CleanupStack::PopAndDestroy( source );
// Close the stream. It also commits changes
CleanupStack::PopAndDestroy( &writer );
// And read the data to the different object
...
RDesReadStream reader ( storage );
CleanupClosePushL( reader );
reader >> *dest;
...
CleanupStack::PopAndDestroy( &reader );
CleanupStack::PopAndDestroy( dest );
}

If you don't know how to start this example ExeDll, try ExeLauncher.
For the full real-world example of sreaming to files, have a look at the Creation of a high score table (Part II): saving to a file store tutorial on NewLC.com
| Attachment | Size |
|---|---|
| StreamingExample.zip | 5.08 KB |