Stream Class
Stream class has no public constructor to creates new instance use Stream.openFile, Stream.openSocket or Stream.openString functions.
Item Index
Methods
Methods
$
str
Stringizer method, outputs content in brackets appended by \n into the stream.
Parameters:
-
str
StringString to input.
Returns:
$n
str
Stringizer method, outputs content in brackets appended by \n into the stream.
Parameters:
-
str
StringString to input.
Returns:
getc
() Integer
Reads one character from the stream.
Returns:
openFile
fileName
[mode]
Opens the file which name is stored in the file-name string and returns an instance of Stream object.
Parameters:
-
fileName
StringName of the file to open.
-
[mode]
String optionalString 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:
openSocket
[addressPort]
[timeout=10]
numberOfAttempts=1
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 optionalAddress 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 optionalTimeout 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
IntegerNumber of attempts to connect, 1 by default.
Returns:
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
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
StringInitial size of stream, or initial value of stream.
Returns:
print
str
Outputs string into the stream. print is an equivalent of: stream << str; operation.
Parameters:
-
str
StringString to input.
Returns:
printf
format
[value]
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 < sequences in the output.
Parameters:
-
format
StringString specifying format of text.
-
[value]
Object optional multipleValues to be inserted to text.
Returns:
printf
format
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
Stringspecifying format of text
Returns:
println
str
Outputs string appended by \n into the stream. println is an equivalent of: stream<<str<<"\n"; operation.
Parameters:
-
str
StringString to input.
Returns:
putc
charCode
Outputs character into the stream. Character defined by its integer code. putc is an equivalent of: stream << charcode; operation.
Parameters:
-
charCode
IntegerCode of char to input