Stream object

Properties
  Stream object has no properties.
Methods
Stream object has no public constructors so it is impossible to create it using new operator.
To create streams use static open*** methods.
openFile

(file-name [,mode]) stream | null

Static method. Opens the file which name is stored in the file-name string and returns an instance of Stream object. Operations allowed to the stream returned are defined by the mode parameter-string.

Script engine uses C/C++ runtime for opening file streams. See fopen function definition for the meaning of the mode string.

openSocket

(address:port [, timeout ] ) →  stream | null

Static method. Opens the socket stream which address and port is stored in the address-port string and returns an instance of Stream object. Opens socket stream in read-write mode. Address can be either domain name or IP address. Format of address:port string is "domain:NNN" or "NNN.NNN.NNN.NNN:NNN" where N is a decimal digit. timeout is a number of seconds to wait on any operations on this socket. If execution of operation on this socket will take more than this limit then exception will be thrown by runtime system.

Example: following code will print out http server response of terrainformatica.com server:

var sock = Stream.openSocket( "www.terrainformatica.com:80" , 5 /*seconds timeout*/ );
if( !sock ) return;
 
sock.println("GET http://www.terrainformatica.com/main.whtm HTTP/1.0");
sock.println("User-Agent: TIScript [en]");
sock.println("");
 
while( true )
{
var s = sock.readln();
if(s == undefined) break;
stdout.println(s);
}
openString
([initialSize:int | initialValue:string]) : stream

Static method. Opens in-memory string output stream with initialSize (integer) of its buffer. Use string streams when you plan to update some string frequently or compose string from many components. String streams are also known as StringBuffer/StringBuilder in Java or .NET. To get current content of the string stream use its method toString.

toString
( ) →  stream

Returns content of string buffer if this is a string stream or name/address of the stream if it was open as file or socket stream.

close

( )true | false

Closes this stream - file, socket, string stream buffer.

print
( string ) true | false

Outputs string into the stream.

print is an equivalent of: stream << string; operation.

println
( string ) true | false

Outputs string appended by \r\n into the stream.  

printf
( format, [value1[, value2[, ...[, valueN]]]])true | false

Prints formatted text by the rules of printf C/C++ function.

Additional format types:

  • %v and %V - these format types accept any value as an argument and produce source code representation of the value suitable for later parsing by eval() method. Thus if value is an array of values it will be printed as "[element1, element2, element3... elementN]" and object (instance of Object class) will be printed as "{key1:value1, key2:value2,..., keyN:valueN}". %v produces one line output and %V tries to produce human readable output with line feeds and tabulations.
    Use it if you need to serialize objects in AJAX/JSON fashion.

  • %S - this format type converts its argument into string and outputs it with HTML escapement. So characters like '<' will be converted to "&lt;" sequences in the output.

putc
( char-code ) true | false

Outputs character into the stream. Character defined by its integer code.

putc is an equivalent of: stream << charcode; operation.

getc
( ) integer | undefined

Reads one character from the stream. Returns its code as integer or undefined if stream was closed or got EOF.

readln
( ) string | undefined

Reads sequence of characters from stream until '\n' character. Returns string read without trailing '\r' and '\n'.