infinity.stream¶
Provides functionality for reading and writing data from and to streams.
Module: infinity.stream
Example:
infinity.loadModule('infinity.stream');
infinity.loadModule('infinity.http');
let memStream = new infinity.stream();
let fileStream = new infinity.stream(infinity.current.root + '../stream.dat', infinity.fileMode.create | infinity.fileMode.shareDenyWrite, true);
try {
let http = new infinity.http.client();
try {
http.timeout = 1000;
http.getStream('https://localhost', fileStream);
} finally {
http.free();
http = null;
}
fileStream.position = 0;
console.debug(memStream.copyFrom(fileStream, fileStream.size) + ' bytes copied');
//52430 bytes copied
console.debug(memStream.readString(infinity.encoding.ansi, true, fileStream.size));
//<!DOCTYPE html>
//...
} finally {
fileStream.free();
fileStream = null;
memStream.free();
memStream = null;
}
stream¶
The class for operating with streams in INFINITY.JS.
Properties:
Methods:
Example:
infinity.loadModule('infinity.stream');
let myStream = new infinity.stream(fileName, fileMode);
Properties
position¶
Type: number
Gets or sets the position within the current stream.
size¶
Type: number
Gets or sets the size of the stream in bytes. The stream will allocate memory as needed, when written to. You can set the size (in bytes) beforehand, however, so reduce memory re-allocation and increase performance.
fileName¶
Type: string
Gets the filename provided during stream object initialization.
Methods
constructor()¶
Lets you create an INFINITY.JS stream object instance with input/output to a file with the possibility of specifying whether the file should be deleted automatically after object destruction.
Signature:
constructor( fileName: string, fileMode: infinity.fileMode, autoDelete?: boolean )
Parameters:
-
fileName:
string
Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which should be used.
-
fileMode:
infinity.fileMode
The mode for opening or creating the file according to infinity.fileMode.
-
autoDelete:
boolean
, optionalSpecifies whether the file should be deleted automatically after object destruction.
Example:
infinity.loadModule('infinity.stream');
let fileStream = new infinity.stream(infinity.current.root + '../stream.dat', infinity.fileMode.create | infinity.fileMode.shareDenyWrite);
constructor()¶
Lets you create an INFINITY.JS stream object instance. Doesn't accept parameters.
Signature:
constructor()
Example:
infinity.loadModule('infinity.stream');
let myStream = new infinity.stream();
copyFrom()¶
Copies the specified amount of the contents of the provided stream to the current stream. Returns the amount of bytes copied.
Signature:
copyFrom( stream: infinity.stream, count?: number ): number
Parameters:
-
stream:
infinity.stream
The stream object to copy from.
-
count:
number
, optionalThe number of bytes to copy.
Return type: number
Example:
myStream.copyFrom(fileStream, fileStream.size)
free()¶
Frees the memory previously occupied by the INFINITY.JS stream object instance.
Signature:
free(): void
Example:
myStream.free();
read()¶
Reads and returns data from the current stream, if it was previously written to it with write().
Signature:
read(): any
Return type: any
Example:
myStream.read();
readBoolean()¶
Reads and returns a boolean value from current the stream, if it was previously written to it with writeBoolean().
Signature:
readBoolean(): boolean
Return type: boolean
Example:
if( myStream.readBoolean() ){
//..
}
readNumber()¶
Reads and returns a number from the current stream, if it was previously written to it with writeNumber().
Signature:
readNumber(): number
Return type: number
Example:
let number = myStream.readNumber();
readString()¶
Reads and returns a number from the current stream, if it was previously written to it with writeNumber().
Signature:
readString( encoding?: infinity.encoding ): string
Parameters:
- encoding:
infinity.encoding
, optionalThe encoding of the string.
Return type: string
Example:
let string = myStream.readString();
readRawString()¶
Reads a specified amount of data from a stream and returns it as a raw string.
Signature:
readRawString( encoding?: infinity.encoding, count?: number ): string
Parameters:
- encoding:
infinity.encoding
, optionalSpecifies the character encoding to be used for interpreting the bytes read from the stream.
- count: number, optional
The number of bytes to read from the stream
Return type: string
Example:
let string = myStream.readRawString(infinity.encoding.UTF8, 100);
write()¶
Writes the provided data to the current stream. Reading will then be possible with read().
Signature:
write( value: any ): void;
Parameters:
- value:
any
The data to be written.
Example:
myStream.write({data1: 'abc', 'code': 321, 'flag': true});
writeBoolean()¶
Writes the provided a boolean value to the current stream. Reading will then be possible with readNumber().
Signature:
writeBoolean( value: boolean ): void
Parameters:
- value:
boolean
The boolean value to be written.
Example:
myStream.writeBoolean(true);
writeNumber()¶
Writes the provided a number to the current stream. Reading will then be possible with readNumber().
Signature:
writeNumber( value: number ): void
Parameters:
- value:
number
The number to be written.
Example:
myStream.writeNumber(321);
writeRawString()¶
Writes the provided a string to the current stream. Reading will then be possible with readRawString().
Signature:
writeRawString( value: string, encoding?: infinity.encoding, writeBom?: boolean ): void
Parameters:
-
value:
string
The string to be written to the stream.
-
encoding:
infinity.encoding
, optionalSpecifies the character encoding to be used when writing the string to the stream.
-
writeBom:
boolean
, optionalDetermines whether to write a Byte Order Mark (BOM) at the beginning of the stream.
Example:
myStream.writeRawString('Hello, world!', infinity.encoding.UTF8, true);
writeString()¶
Writes the provided a string to the current stream. Reading will then be possible with readString().
Signature:
writeString( value: string, encoding?: infinity.encoding, append?: boolean ): void
Parameters:
-
value:
string
The string to be written.
-
encoding:
infinity.encoding
, optionalAn infinity.encoding enum for the string.
-
append:
boolean
, optionalWhether to append the string to previously written data or to overwrite it.
Example:
myStream.writeString('abc');