Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
to reconstruct the data into objects when you read the text file. This would me<strong>an</strong> that you would have to<br />
invent some way of indicating that certain data was associated with c ertain objects <strong><strong>an</strong>d</strong> that certain<br />
objects were related (if you were dealing with a composite object). All of this would be very time<br />
consuming <strong><strong>an</strong>d</strong> error prone. It would also be very unlikely that the ASCII data written out by someone<br />
else would be in the right format for your system.<br />
The BOSS is provided by VisualWorks as a way around this. It allows objects to be directly stored<br />
to a file in a compact <strong><strong>an</strong>d</strong> encoded form. You do not need to convert the objects into <strong>an</strong>ything special<br />
nor do you need to worry abou t reconstructing the objects when you load them back in. In addition<br />
everyone else who uses the BOSS will be able to read <strong><strong>an</strong>d</strong> write to your BOSS files. The action of<br />
writing objects to a BOSS file is referred to by <strong>Smalltalk</strong>ers as BOSSing out (or in) the objects.<br />
13.4.1 Saving to BOSS<br />
It is possible to “BOSS out” a single object, a series of objects or a whole collection of objects. It is also<br />
possible to BOSS out a composite object. If <strong>an</strong> object references <strong>an</strong>other object, then both the original<br />
object <strong><strong>an</strong>d</strong> the objects which it contains are saved to the BOSS file. The BOSS file is <strong>an</strong> incremental file<br />
so it is possible to add objects to a BOSS file at different times (i.e. in different executions of the same<br />
image). It is also possible to read the BOSS file one objec t at a time (assuming the objects were written<br />
incrementally). This me<strong>an</strong>s that you do not have to read the whole file to access the third object etc.<br />
The procedure for creating a BOSS file is relatively straight forward <strong><strong>an</strong>d</strong> is outlined below:<br />
1. Create a dat a stream to write the objects to. This is usually a write stream onto a specified<br />
filename.<br />
2. Create the Binary<strong>Object</strong>Storage system (BOSS) object. This is done using the inst<strong>an</strong>ce creation<br />
method onNew: with the data stream as the parameter.<br />
3. Using the nextPut: message save each object to the BOSS object. Each object you wish to<br />
save is passed as the parameter of the nextPut: message.<br />
4. Once you have finished BOSSing out the object you c<strong>an</strong> close it by sending it the message close.<br />
If you w<strong>an</strong>t to save a whole co llection of objects to the BOSS file you c<strong>an</strong> do so by using the<br />
nextPutAll: message (instead of the nextPut: message). Each object will be stored separately,<br />
which allows each object to be retrieved separately at a later date.<br />
If you wish to append <strong>an</strong> obje ct to <strong>an</strong> existing BOSS file then you create a read -append data stream<br />
(rather th<strong>an</strong> a write data stream), then create the BOSS file using the onOld: message (rather th<strong>an</strong> the<br />
onNew: message). Finally, you need to move the file pointer to the end of the BOSS file. You c<strong>an</strong> do<br />
that by sending the setToEnd message to the BOSS object.<br />
A simple example of creating <strong><strong>an</strong>d</strong> saving to a BOSS file is presented below. The bosFileName is set<br />
to a string representing <strong>an</strong> appropriate file on the host computer system.<br />
valuesFileStream := bosFileName asFilename writeStream.<br />
bosFileStream := Binary<strong>Object</strong>Storage onNew: valuesFileStream.<br />
bosFileStream nextPutAll: aList.<br />
valuesFileStream close.<br />
This example saves all the elements in the variable aList to the bosFile indicated by the<br />
bosFileName. It then closes the file.<br />
A point to note is that it c<strong>an</strong> be a good idea to wrap the writing of objects to the BOSS file with a<br />
valueNowOrOnUnwindDo: message. This message is used to ensure that the BOSS file you ar<br />
writing to is closed safely even if <strong>an</strong> abnormal interruption occurs. That is, the file will be closed <strong><strong>an</strong>d</strong><br />
freed up in <strong>an</strong> appropriate m<strong>an</strong>ner. The message is sent to a block in which you define the write<br />
expression. It takes a second block as its argument, which executes if there is a problem or when the<br />
first block terminates. For example, we could have written the write part of the above as:<br />
[bosFileStream nextPutAll: aList] valueNowOrOnUnwindDo:<br />
[valuesFileStream close].<br />
e<br />
115