Moscrif API Docs for: 2012q3
Show:

Stream Class

Library: core

Stream class has no public constructor to creates new instance use Stream.openFile, Stream.openSocket or Stream.openString functions.

Methods

$

(
  • str
)
Boolean

Stringizer method, outputs content in brackets appended by \n into the stream.

Parameters:

Returns:

$n

(
  • str
)
Boolean

Stringizer method, outputs content in brackets appended by \n into the stream.

Parameters:

Returns:

close

() Boolean

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

Returns:

getc

() Integer

Reads one character from the stream.

Returns:

Integer: Char code as integer or undefined if stream was closed or got EOF.

openFile

(
  • fileName
  • [mode]
)
Stream

Opens the file which name is stored in the file-name string and returns an instance of Stream object.

Parameters:

  • fileName String

    Name of the file to open.

  • [mode] String optional

    String specifying the behavior of function:

    • "r" - Open a file for reading. The file must exist.
    • "w" - Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
    • "a" - Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
    • "r+" - Open a file for update both reading and writing. The file must exist.
    • "w+" - Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
    • "a+" - Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

    It is also possible to specity encoding for text files: * "8" - Causes the file to be encoded with UTF-8. * "u" - Causes the file to be encoded with UCS2.

Returns:

Stream: New instance of Stream class, which allows to manipulate with opened file.

openSocket

(
  • [addressPort]
  • [timeout=10]
  • numberOfAttempts=1
)
Stream

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.

Parameters:

  • [addressPort] String optional

    Address and port to open. Address can be either domain name or IP address. Format of addressPort string is "domain:NNN" or "NNN.NNN.NNN.NNN:NNN" where N is a decimal digit.

  • [timeout=10] Integer optional

    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.

  • numberOfAttempts=1 Integer

    Number of attempts to connect, 1 by default.

Returns:

Stream: New instance of Stream class or null if funcition failed.

Example:

// following code will print out http server response of moscrif.com server:
var sock = Stream.openSocket("moscrif.com:80" , 5 );
if(!sock)
    return;
sock.println("GET https://moscrif.com/default.aspx HTTP/1.0");
sock.println("User-Agent: Moscrif");
sock.println("");

while( true )
{
    var s = sock.readln();
    if(s == undefined) break;
    console<<s<<"\n";
}

openString

(
  • initialSize|initialValue
)
Stream

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.

Parameters:

  • initialSize|initialValue String

    Initial size of stream, or initial value of stream.

Returns:

Stream: New instance of Stream class or null if funcition failed

print

(
  • str
)
Boolean

Outputs string into the stream. print is an equivalent of: stream << str; operation.

Parameters:

Returns:

printf

(
  • format
  • [value]
)
Boolean

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.

Parameters:

  • format String

    String specifying format of text.

  • [value] Object optional multiple

    Values to be inserted to text.

Returns:

printf

(
  • format
)
Array

Scans the stream for values according to the rules of scanf C/C++ function with wildcard (like %[a-z] ) extensions. See definition of scanf in MSDN. Additional format types:

  • %N - scans for either float or integer number.

Parameters:

  • format String

    specifying format of text

Returns:

Array: Array of successfully parsed values.

println

(
  • str
)
Boolean

Outputs string appended by \n into the stream. println is an equivalent of: stream<<str<<"\n"; operation.

Parameters:

Returns:

putc

(
  • charCode
)
Boolean

Outputs character into the stream. Character defined by its integer code. putc is an equivalent of: stream << charcode; operation.

Parameters:

  • charCode Integer

    Code of char to input

Returns:

Boolean: True if function succeed, otherwise false.

readln

() String

Reads sequence of characters from stream until '\n' character.

Returns:

String: String read without trailing '\r' and '\n'.

toString

() String

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.

Returns:

Properties

encoding

Integer

Gets or sets encoding used by the stream. For now it supports either "none" (raw byte stream) and "utf-8" encodings.

length

Boolean

Length of the input stream or not.

name

String

Name of the stream - either file name or url where the data came from. Can be an empty string, e.g. for in memory streams.