Stream classes
Streams offer a means to sequentially access elements of a collection.
Also, there are classes to access files, directories, pipes or sockets.
The most useful stream classes are:
Use the systemBrowser to have a more detailed look into the implementation.
ReadStreams can be used to process elements of a collection sequentially.
The typical use of readStreams is:
- creation:
aStream := ReadStream on:aCollection
aCollection readStream
- asking if the end is reached:
aStream atEnd ifTrue:[ ... ]
- reading the next element(s):
nextElement := aStream next
aCollection := aStream next:count
WriteStreams can be used to collect elements in a collection.
The typical use of writeStreams is:
- creation:
aStream := WriteStream on:aCollection
aCollection writeStream
- appending:
aStream nextPut:nextElement
aStream next:count put:nextElement
aStream nextPutAll:aCollection
- getting what we have collected so far:
collection := aStream contents
This is an abstract class - i.e. there are no instances of ExternalStream.
Instead, it provides the common behavior for all streams which are
associated with an external (OperatingSystem-) stream.
Examples are FileStream
, PipeStream
,
Socket
and DirectoryStream
.
Therefore, the following protocol is valid for all off the above mentioned
classes.
ExternalStreams elements are normally characters, however, they also
support a so called binary mode in which byte-valued
integers are processed.
Typical operations:
- appending a character (in text mode):
aStream nextPut:aCharacter
- appending a byte (in binary mode):
aStream nextPut:aByteValueInteger
- appending a byte (in any mode):
aStream nextPutByte:aByteValueInteger
- appending all elements of a collection (typically strings):
aStream nextPutAll:aCollection
- appending a carriage return (line end):
aStream cr
- reading a character:
aStream next
- reading a line:
aStream nextLine
- reading a number of characters:
aStream next:howMany
FileStreams support all of ExternalStreams protocol.
The can be created to read, write, readWrite or append from/to a file.
Creation:
- for reading:
aStream := FileStream readonlyFileNamed:aFilenameString
- to read/write an existing file:
aStream := FileStream oldFileNamed:aFilenameString
- to create a new file for writing:
aStream := FileStream newFileNamed:aFilenameString
For portability, you should use the companion class Filename
to create fileStreams:
- for reading:
aStream := aFilenameString asFilename readStream
- to read/write an existing file:
aStream := aFilenameString asFilename readWriteStream
- to create a new file for writing:
aStream := aFilenameString asFilename writeStream
- to append to an existing file:
aStream := aFilenameString asFilename appendingWriteStream
PipeStreams allow reading the output of any unix command or
sending text for input to a unix command.
Once created, they behave like any other ExternalStream.
Creation:
- for reading:
aStream := PipeStream readingFrom:aCommandString
- for writing:
aStream := PipeStream writingTo:aCommandString
Sockets allow the sending/receiving of unstructured data via TCP/IP connections.
Due to a somewhat more complex naming and connection procedure,
the protocol for Socket creation is a bit more complex than for the above
streams. See examples in the "doc/coding"
directory for more details.
Copyright © Claus Gittinger Development & Consulting, all rights reserved
(cg@ssw.de)