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.
13. Streams <strong><strong>an</strong>d</strong> Files<br />
13.1 <strong>Introduction</strong><br />
This chapter discusses the second most used class hierarchy in <strong>Smalltalk</strong>; the Stream classes. The<br />
Stream classes are used (amongst other things) for accessing files . The remainder of the chapter is<br />
structured in the following m<strong>an</strong>ner: Section two describes the Streams classes. Section three describes<br />
how the stream classes c<strong>an</strong> be used for file access. Section four describes <strong>an</strong> example of using files <strong><strong>an</strong>d</strong><br />
streams.<br />
13.2 Streams<br />
Figure 13.1: The structure of the Stream class hierarchy<br />
Streams are the second most used set of classes. Streams are objects which serve as sources or sinks of<br />
data. At first this concept c<strong>an</strong> seem a bit str<strong>an</strong>ge. The easiest way to think of streams are as streams of<br />
data (as opposed to water) either flowing from a pool of data or into a pool of data.<br />
There are a number of different types of stream in the <strong>Smalltalk</strong> system, <strong><strong>an</strong>d</strong> each c<strong>an</strong> have a<br />
surprisingly wide r<strong>an</strong>ge of uses. Figure 13.1 illustrates the structure of the stream class hierarchy. The<br />
Stream class is the root class of this hierarchy. Below this are stream classes for reading, writing, for<br />
accessing external files etc.<br />
A stream may be input only (ReadStream ), output only (WriteStream ) or input <strong><strong>an</strong>d</strong> output<br />
(ReadWriteStream). They c<strong>an</strong> be internal streams (i.e. that act as a source or sink for data internal to the<br />
image) or external streams (i.e. the source or sink for the data is external to the current image. A file is a<br />
typical example of <strong>an</strong> external source or sink). Peekable streams also have <strong>an</strong> internal record of their<br />
current position <strong><strong>an</strong>d</strong> c<strong>an</strong> look ahead in the stream for information.<br />
Streams have various access methods defined which allow the next item to be obtained or provided.<br />
Some of the more useful methods include:<br />
• next Answer the next object in the receiver stream <strong><strong>an</strong>d</strong> adv<strong>an</strong>ce the position indicator by one.<br />
• peek Answer the next object in the receiver stream, but do not adv<strong>an</strong>ce the position indicator.<br />
• nextPut: <strong>an</strong><strong>Object</strong> Write <strong>an</strong><strong>Object</strong> at the current position.<br />
• nextPutAll: aCollection Write all elements of the collection to the receiver stream.<br />
• atEnd Answer true if the stream is at the end else <strong>an</strong>swer false.<br />
Others include next: <strong>an</strong>Integer, peekFor: <strong>an</strong><strong>Object</strong> <strong><strong>an</strong>d</strong> skipTo: <strong>an</strong><strong>Object</strong>. What<br />
operations c<strong>an</strong> be performed on Streams depends on the class of stream being used. Figure 13.1<br />
illustrates the structure of the stream hierarchy.<br />
Typically streams are connected to files (using one of the ExternalStream’s subclasses) or to a<br />
collection of data (using one of the InternalStream’s subclasses). If a stream is connected to <strong>an</strong><br />
111