Stream Overview

Damien Cassou, Stéphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

What You Will Learn

Streams

0.2.

API Overview

Reading Elements

| strean |
stream := #($a $b $c $d $e $f) readStream.

stream next.
> $a

stream upTo: $d.
>  #($b $c)

stream position.
> 4

stream upToEnd
> #($e $f)

Writing Elements

stream := (Array new: 6) writeStream.

stream nextPut: 1.
stream contents.
> #(1)

stream nextPutAll: #(4 8 2 6 7).
stream contents.
> #(1 4 8 2 6 7)

Writing to a File

stream := 'hello.txt' asFileReference writeStream.
stream nextPutAll: 'Hello Pharo!'.
stream close.

Reading from a File

stream := 'hello.txt' asFileReference readStream.

stream next.
> $H

stream upToEnd.
> 'ello Pharo!'

stream close

Creating Collections from a Stream

When you want to create a collection by writing to a stream:

stream := OrderedCollection new writeStream.
stream nextPut: 1.
stream contents

is equivalent to

OrderedCollection
   streamContents: [:stream | stream nextPut: 1]

Both scripts return a collection with number 1 inside.

What You Should Know

/