Simple object loading and unloading using files
In most cases, the need to use the Swapper comes from the fact that an application has to store objects in files, and to retrieve them later. "Example: Using the Swapper as a simple object dumper" shows how an ObjectDumper can be created and used to dump an object. The example following it, "Example: Using the Swapper as a simple object loader", shows how the dumped object can be loaded into an image using an ObjectLoader.
Example: Using the Swapper as a simple object dumper
| dumper targetObject stream |
targetObject:= 2@7. "This could be a more complex Smalltalk object."
(stream := CfsWriteFileStream open: 'testfile.swp') isCfsError
ifTrue: [
self error: stream printString].
"Makes sure DBString will never be returned as result of #next:, etc."
stream isBytes: true.
dumper := ObjectDumper new.
dumper unload: targetObject intoStream: stream.
stream close.
dumper hasErrorOccurred
ifTrue: [self error: dumper currentErrorString].
Example: Using the Swapper as a simple object loader
| loader targetObject stream |
(stream := CfsReadFileStream open: 'testfile.swp') isCfsError
ifTrue: [self error: stream printString].
"Makes sure DBString will never be returned as result of #next:, etc."
stream isBytes: true.
loader := ObjectLoader new.
targetObject := loader loadFromStream: stream.
stream close.
loader hasErrorOccurred
ifTrue: self error: loader currentErrorString].
targetObject inspect.
Last modified date: 01/29/2015