← index

StringStream

inherits Object

A text stream: the UTF-8 view of a byte conduit, usually obtained from [IO]File#stringStream, a socket's stringStream, or [IO]Stdin.

Reading is line-oriented — readLine (nil at EOF), eachLine:, readAll — and throws a catchable ParseError on invalid UTF-8; multibyte characters split across reads are reassembled. Writing is write: / writeln: with plain Strings. Like every stream, reads park the task, not the scheduler.

var out = ([IO]File.create:'/tmp/lines.txt').stringStream
out.writeln:'alpha'
out.close
var st = ([IO]File.open:'/tmp/lines.txt').stringStream
st.readLine     "* -> 'alpha'
st.readLine     "* -> nil
st.close
[IO]File.delete:'/tmp/lines.txt'

Class methods

over:

A text stream that *consumes* a ByteStream: the connection, read-ahead, and any pending writes transfer, and the byte stream is left closed. Equivalent to the byte stream's own stringStream.

native

over: do:

Like over:, but scoped: run the block with the text stream and close it on every exit path (normal, throw, or cancel); answers the block's value.

native

Instance methods

close

Flush any buffered writes and close the stream (idempotent). On a write-codec stream (gzip) this also finishes the encoder (writing the trailer); a failed finish throws. Further operations throw. Returns nil.

native

closed?

Whether the stream has been closed.

native

codecWrap:String

Wrap the stream in a named codec IN PLACE (see the ByteStream twin) — later reads decode the transformed bytes as UTF-8, later writes go through the transform; answers the receiver. Prefer the sugar (gunzip / gzip).

native

eachLine:

Run the block on each remaining line (terminators stripped, as readLine) until EOF; answers self. The whole-file loop: st.eachLine:{ |line| ... }.

native

flush!

Hand any buffered written bytes to the OS now; a no-op on a write-through (socket) stream. Returns nil.

native

gunzip

As ByteStream#gunzip, decoding the gunzipped bytes as UTF-8 text — gzipped logs read line by line without materializing anything.

core/09-codecs.qn:71

gzip

As ByteStream#gzip, for text: write:/writeln: land gzip-compressed — a .log.gz written line by line. Close deliberately (see ByteStream#gzip).

core/09-codecs.qn:75

read

Whatever text is available right now: the largest valid-UTF-8 prefix of the buffered bytes, as a String (a trailing partial code point is kept back for the next read). An empty String means EOF; a truly invalid byte, or EOF in the middle of a character, throws a ParseError.

native

readAll

Read to EOF and answer the whole remainder as one String (throws a ParseError on invalid UTF-8). The one-line way to slurp a file:

var out = [IO]File.create:'/tmp/notes.txt'
out.writeAll:'hi'.asBytes
out.close
([IO]File.open:'/tmp/notes.txt').stringStream.readAll     "* -> 'hi'
[IO]File.delete:'/tmp/notes.txt'

native

readLine

The next line as a String, its trailing \n (or \r\n) stripped; nil at EOF. An empty line answers ''; a final line without a newline is returned once, then nil. Throws a ParseError if the line is not valid UTF-8.

native

write:String

Write the String's UTF-8 bytes, without a trailing newline. Buffered on a file write stream, straight through on a socket. Returns nil.

native

writeln:String

write: plus a trailing newline — the line-oriented half of the filter idiom [IO]Stdin.eachLine:{ |l| out.writeln:l }. Returns nil.

native