memorystream: Memory-based streams

This module defines the MemoryStream class that allows creating memory-based streams.

See also: The Stream class is defined in the io module.

Class MemoryStream

Inherits Stream

class MemoryStream([contents as Str])
Construct a MemoryStream instance. If the contents argument is provided, the stream initially contains this string argument. Otherwise, the stream is initially empty. The stream pointer will always be initially at the start of the stream. MemoryStream instances are always buffered and support both reading and writing.

MemoryStream methods

MemoryStream supports all the Stream methods and the following additional methods:

seek(n as Int)
Move the stream pointer to the specified index. 0 refers to the start of the stream, 1 to the position between the first and the second character, and so on.
pos() as Int
Return the current stream pointer index.
size() as Int
Return the length of the stream contents.
contents() as Str
Return the contents of the stream as a string. This method works also if the stream is closed.


import memorystream

def Main()
  var s = MemoryStream("Line 1" + Newline + "Line 2")
  s.readLn() -- Result: "Line 1"
  s.readLn() -- Result: "Line 2"