Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
You c<strong>an</strong> also copy, move <strong><strong>an</strong>d</strong> delete files, get the contents of directories <strong><strong>an</strong>d</strong> files as well as setting<br />
file permissions <strong><strong>an</strong>d</strong> printing files all from within Smal ltalk. The messages used for these functions are<br />
all fairly intuitive <strong><strong>an</strong>d</strong> readily available in the <strong>Smalltalk</strong> documentation.<br />
13.3.3 H<strong><strong>an</strong>d</strong>ling file IO errors<br />
You should always wrap your file access code up in a block <strong><strong>an</strong>d</strong> use the valueNowOrOnUnwindDo:<br />
message to evaluate it. This is a special version of value, which will execute the block passed to it as a<br />
parameter if <strong>an</strong> exception is raised, while the block it was sent to is executing. For example, if you try<br />
<strong><strong>an</strong>d</strong> read to a file which does not exist, then <strong>an</strong> exception will be raised <strong><strong>an</strong>d</strong> your system will be left in<br />
<strong>an</strong> unstable state with streams still open. However, if you use the valueNowOrOnUnwindDo:<br />
message, then you c<strong>an</strong> define what should happen if such <strong>an</strong> event occurs. For example, you could<br />
specify that the stream should be closed. This is a very good way of ensuring that your file accesses are<br />
h<strong><strong>an</strong>d</strong>led tidily.<br />
For example, consider the load: method defined below. This method accesses a file, reading each<br />
line at a time. Once all the lines in the file have been accessed it terminates.<br />
load: filename<br />
|newFile newLineChar stream readingBlock item student |<br />
newFile := filename value asFilename.<br />
newFile exists<br />
ifTrue:<br />
[newLineChar := Character cr.<br />
stream := newFile readStream.<br />
contents := OrderedCollection new: 250.<br />
readingBlock :=<br />
[[stream atEnd]<br />
whileFalse:<br />
[item := stream upTo: newLineChar.<br />
contents add: item.]].<br />
Cursor read showWhile:<br />
[readingBlock valueNowOrOnUnwindDo:<br />
[stream close]]].<br />
ifFalse: [Dialog warn: 'File does not exist!!']<br />
There are three things to note about this method from a <strong>Smalltalk</strong> point of view.<br />
• The first is that we read in one line of the file at a time by telling the (read) stream that we w<strong>an</strong>ted to<br />
read up to a carriage return. To do this we send the upTo: message to the stream with the<br />
newLineChar as the parameter. You c<strong>an</strong> use this to read in text up to <strong>an</strong>y character in the input<br />
stream.<br />
• If the file exists, a block is created which co ntains the code which will read in the data in the file.<br />
This block is assigned to a variable readingBlock. This block is evaluated by sending it the<br />
valueNowOrOnUnwindDo: message. Notice that if <strong>an</strong>ything happens to raise <strong>an</strong> error during<br />
these operations ( e.g. the file system becomes full), the block passed as a parameter to the<br />
valueNowOrOnUnwindDo: message will be executed. In this case, it will close the stream (i.e. it<br />
will close the file). Thus, if <strong>an</strong>ything goes wrong during the processing of the file, the file will be<br />
closed <strong><strong>an</strong>d</strong> the system left in a stable state. Indeed when the last statement has been executed in the<br />
write block, the block passed as a parameter will be executed. Thus the stream will also be closed.<br />
This is a very good way of h<strong><strong>an</strong>d</strong>ling the process of closing the link to files.<br />
• The other point is that the whole of the read expression (i.e.<br />
readingBlock<br />
valueNowOrOnUnwindDo: [stream close]) is passed in a block to the showWhile:<br />
message. This message is part of the cursor protocol <strong><strong>an</strong>d</strong> is used to specify the type of cursor to be<br />
displayed while a particular operation is being performed. In this case, the read cursor will be<br />
displayed while the text file is being read in. This provides some visual feedback to the user.<br />
13.4 The Binary <strong>Object</strong> Streaming Service<br />
The Binary <strong>Object</strong> Streaming Service (or BOSS for short) is a very useful <strong><strong>an</strong>d</strong> effective way of storing<br />
information in the form of objects (inst<strong>an</strong>ces). If you were to save information to <strong>an</strong> ordinary text file,<br />
then all you could save would be the ASCII version of the data held by the object. You would then need<br />
114