Send to Printer


ST 4U 333: Binary Object Storage in VA Smalltalk

January 18, 2013 11:51:32.923

Today's Smalltalk 4 You looks at the Swapper framework in VA, which can be used to save and restore arbitrary objects to disk in a binary format. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:


If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Today we'll take a look at how you can easily store objects in a binary format in external files. VA provides the Swapper framework for this, and it's pretty easy to use. Our example uses a simple point, but you can use theSwapper to store arbitrary objects to a file:

"Binary Storage using the Swapper"
 targetObject:= 2@7. 
(stream := CfsWriteFileStream open: 'testfile.swp') isCfsError
	ifTrue: [self error: stream printString]. 
"Set to binary mode"
stream isBytes: true.  
dumper := ObjectDumper new.  
dumper unload: targetObject intoStream: stream. 
stream close. 
dumper hasErrorOccurred
	ifTrue: [self error: dumper currentErrorString].

Note the you don't really check for exceptions; that's all done inside the framework. Rather, you check for errors after your code executes. Once you've got the objects on disk, you'll eventually want them back in Smalltalk:

"load the object from the binary file"
 (stream := CfsReadFileStream open: 'testfile.swp') 
	isCfsError   ifTrue: [self error: stream printString]. 
"set binary"
stream isBytes: true.  
loader := ObjectLoader new. 
targetObject := loader loadFromStream: stream. 
stream close. 
loader hasErrorOccurred   
	ifTrue: [self error: loader currentErrorString]. 
targetObject inspect.

Execute that, and you should see your Point object:


Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: , ,

[st4u333-iPhone.m4v ( Size: 3535051 )]

posted by James Robertson

 Share Tweet This