Skip to content

infinity.stream

Provides functionality for reading and writing data from and to streams.

Module: infinity.stream

Classes:
Interfaces:

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;
  }

  console.debug(memStream.copyFrom(fileStream) + ' bytes copied');
  //52430 bytes copied

  memStream.position = 0;
  console.debug(memStream.readRawString(infinity.encoding.utf8));
  //<!DOCTYPE html>
  //...

} finally {
  fileStream.free();
  fileStream = null;

  memStream.free();
  memStream = null;
}

Interfaces

byteArray

Extends: Array<number>

An array of byte values (0-255), used by readBytes() and writeBytes().


Classes

stream

The class for operating with streams in INFINITY.JS. A stream created without parameters is a memory stream, a stream created with a filename is a file stream. All read and write methods operate at the current position and advance it.

Example:

infinity.loadModule('infinity.stream');
let myStream = new infinity.stream('data.bin', infinity.fileMode.create);
myStream.free();

Properties


position

Type: number

Gets or sets the position (in bytes) 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, to reduce memory re-allocation and increase performance. Setting a smaller size truncates the stream.


fileName

Type: string

Gets the filename provided during stream object initialization. Throws an error for memory streams.


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. Throws an error if the file cannot be opened or created.

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, optional

    Specifies whether the file should be deleted automatically when the stream is freed (with free() or by the garbage collector). Default: false.

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 memory stream object instance. Doesn't accept parameters.

Signature:

constructor()

Example:

infinity.loadModule('infinity.stream');
let myStream = new infinity.stream();

copyFrom()

Copies the specified number of bytes from the current position of the provided stream to the current stream. If count is omitted or 0, the complete source stream is copied from its beginning. Returns the number of bytes copied. Throws an error if the source stream contains less than count bytes after its current position.

Signature:

copyFrom( stream: infinity.stream, count?: number ): number

Parameters:

  • stream: infinity.stream

    The stream object to copy from.

  • count: number, optional

    The number of bytes to copy. Default: 0 (complete stream).

Return type: number

Example:

myStream.copyFrom(fileStream);

free()

Frees the memory previously occupied by the INFINITY.JS stream object instance and closes the file of a file stream. If buffered data of a file stream cannot be written (e.g. a file opened read-only), the file is closed anyway and an error is thrown. The object must not be used afterwards: all other methods and properties then throw an error. Calling free() again does nothing.

Signature:

free(): void

Example:

myStream.free();

read()

Reads and returns data from the current stream, if it was previously written to it with write() (a 4-byte length followed by UTF-8 encoded JSON, which is parsed). Returns undefined if the stored length is 0.

Signature:

read(): any

Return type: any

Example:

myStream.read();

readBoolean()

Reads and returns a boolean value (1 byte) from the current stream, if it was previously written to it with writeBoolean().

Signature:

readBoolean(): boolean

Return type: boolean

Example:

if( myStream.readBoolean() ){
  //..
}

readBytes()

Reads the specified number of bytes from the current stream and returns them as an array of numbers.

Signature:

readBytes( count?: number ): infinity.stream.byteArray

Parameters:

  • count: number, optional

    The number of bytes to read. Default: all bytes from the current position to the end of the stream. At most the bytes up to the end of the stream are returned.

Return type: infinity.stream.byteArray

Example:

myStream.position = 0;
let header = myStream.readBytes(4);

readNumber()

Reads and returns a number (8-byte double) 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 string from the current stream, if it was previously written to it with writeString() (a 4-byte length in bytes followed by the encoded string).

Signature:

readString( encoding?: infinity.encoding ): string

Parameters:

  • encoding: infinity.encoding, optional

    The encoding of the string. Must match the encoding used for writing. Default: infinity.encoding.utf8.

Return type: string

Example:

let string = myStream.readString();

readRawString()

Reads the specified number of bytes from the current position of the stream and returns them decoded as a string (without length prefix, e.g. text written with writeRawString() or received via HTTP). A byte order mark (BOM) at the beginning of the data is skipped.

Signature:

readRawString( encoding?: infinity.encoding, count?: number ): string

Parameters:

  • encoding: infinity.encoding, optional

    Specifies the character encoding to be used for interpreting the bytes read from the stream. If omitted, the encoding is detected from a BOM, otherwise UTF-8 is used if the data is valid UTF-8, else Windows-1252.

  • count: number, optional

    The number of bytes to read from the stream. Default: all bytes from the current position to the end of the stream. At most the bytes up to the end of the stream are read.

Return type: string

Example:

let string = myStream.readRawString(infinity.encoding.utf8, 100);

write()

Serializes the provided data as JSON and writes it to the current stream (a 4-byte length in bytes followed by the UTF-8 encoded JSON; before version 3.11 data with non-ASCII characters was written incompletely and could not be read). 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 boolean value (1 byte) to the current stream. Reading will then be possible with readBoolean().

Signature:

writeBoolean( value: boolean ): void

Parameters:

  • value: boolean

    The boolean value to be written.

Example:

myStream.writeBoolean(true);

writeBytes()

Writes the provided bytes to the current stream. Reading will then be possible with readBytes().

Signature:

writeBytes( value: infinity.stream.byteArray ): void

Parameters:

Example:

myStream.writeBytes([0x25, 0x50, 0x44, 0x46]);

writeNumber()

Writes the provided number (8-byte double) 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 string (without length prefix) at the current position of the stream (before version 3.11 the string was always appended at the end of the 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, optional

    Specifies the character encoding to be used when writing the string to the stream. Default: infinity.encoding.utf8.

  • writeBom: boolean, optional

    Writes a Byte Order Mark (BOM) of the encoding before the string, if the string is written at the beginning of the stream (position 0). Default: false.

Example:

myStream.writeRawString('Hello, world!', infinity.encoding.utf8);

writeString()

Writes the provided string to the current stream (a 4-byte length in bytes followed by the encoded string). Reading will then be possible with readString().

Signature:

writeString( value: string, encoding?: infinity.encoding ): void

Parameters:

Example:

myStream.writeString('abc');