Object loading and unloading using streams
In the examples in the previous section, the dumping and loading were done using a file stream. However, dumping and loading can also be done on top of streams like ReadWriteStream in memory. Any stream that implements the WriteStream protocol is valid for dumping. The streams can be either on Strings (ReadWriteStream on: String new) or on ByteArrays (ReadWriteStream on: ByteArray new). Since VAST Platform runs on platforms with different character sets (such as ASCII, EBCDIC and Unicode), it is recommended that streams on ByteArrays be used whenever possible. Any stream that implements the ReadStream protocol is valid for loading.
Example: Using the Swapper with streams in memory
The next example shows how dumping and loading can be done with a ReadWriteStream. ObjectDumper and ObjectLoader use the message isBytes: to ensure that the stream does not use DBStrings (on platforms where this is the default behavior).
| dumper targetObject stream loader|
targetObject:= 2@7. "This could be a more complex Smalltalk object."
stream := ReadWriteStream on: ByteArray new.
dumper := ObjectDumper new.
dumper unload: targetObject intoStream: stream.
dumper hasErrorOccurred
ifTrue: [self error: dumper currentErrorString].
stream reset. "Repositions at the beginning of the stream."
loader := ObjectLoader new.
targetObject := loader loadFromStream: stream.
stream close. "This is not really necessary for streams in memory."
loader hasErrorOccurred
ifTrue: self error: loader currentErrorString].
targetObject inspect.
 
Tips
If repeated dumping is to be done, creating an ObjectDumper and an ObjectLoader that are reused will provide a significant performance improvement over repeatedly creating new instances of these objects because you can avoid the setup cost of creating a dumper and loader.
 
Last modified date: 06/02/2020