Stream API Functions

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen

This document lists most useful (and most often needed) functions. Be aware, that there are many more to be found in either the class references or via the builtin class browser.

Reference: Stream and ExternalStream and Socket

Back to Useful API Functions

Creation[Bearbeiten]

Internal Streams[Bearbeiten]

aCollection readStream => Stream
aString readStream => Stream

returns a stream to read from a general collection or especially from a string (which is a collection of characters)


aCollection writeStream
aString writeStream

returns a stream to write to a general collection or especially a string stream.
External Streams[Bearbeiten]

pathString asFilename readStream => Stream
pathString asFilename writeStream => Stream
pathString asFilename appendingWriteStream => Stream

returns a stream to read / write the file named aFilenameString


Socket newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName => SocketStream

returns a connected socket for reading and writing


PipeStream readingFrom:OS-command => PipeStream

returns a stream reading the output from an OS-command

Testing[Bearbeiten]

aStream atEnd => Boolean
aStream.atEnd() [JS]

returns true if no more elements are to be read

Reading[Bearbeiten]

Characters[Bearbeiten]

aStream next => Character | nil
aStream.next() [JS]

returns the next character (as character object)

Example:

 s := 'data.txt' asFilename readStream.
 s next.
 => character


aStream next:count => String
aStream.next(count) [JS]

returns the next count characters (as a string)

Example:

 s := 'data.txt' asFilename readStream.
 s next:5.
 => string-of-5-chars
Binary Bytes[Bearbeiten]

aStream nextByte => Integer | nil

returns the next byte (as an integer)


aStream nextBytes:count => ByteArray

returns the next count bytes (as a byte array)

Finish Writing[Bearbeiten]

When done with writing, external streams should be closed with:

aStream close => void

closes the stream; for file streams, any buffered output is now written. For sockets and pipes, the connection is shut down.

When done with writing to an internal stream, its contents can be retrieved with:

aStream contents => Collection

retrieves the elements written so far. For a string stream, that is the characters written so far as a string.

Writing[Bearbeiten]

aStream nextPut:someElement
aStream.nextPut(someElement) [JS]

Appends (writes) an element to the stream. For the special but most common case of a string- or file stream, the written element should be a character object.

aStream nextPutByte:anInteger

Appends (writes) a single byte. This is only allowed for string streams.

aStream cr

Appends (writes) a newline character (line-end character)

Example:

 s := 'data.txt' asFilename writeStream.
 s nextPut: $a.
 s nextPut: $b.
 s nextPutByte: 0x0A.
 s close.

Example:

 s :=  writeStream.
 s nextPut: $a.
 s nextPut: $b.
 s cr.
 s contents.

Example:

 s := #() writeStream.   
 "/ s writes to an Array; elements are arbitrary objects
 s nextPut: 1.
 s nextPut: 'two'.
 s nextPut: 3.14159.
 s contents
 -> #( 1 'two' 3.14159 )
nextPutAll:[Bearbeiten]

Appends (writes) a number of elements to the stream.

aStream nextPutAll:aCollectionOfElements
aStream.nextPutAll(aCollectionOfElements) [JS]

writes all elements from the argument to the stream. The collection's elements must be compatible with the streams element types. For string streams, this will usually be a string.

Example:

 s := 'data.txt' asFilename writeStream.
 s nextPutAll: 'abc'.
 s close.



Copyright © 2014-2024 eXept Software AG