Input Stream

Input streams provide convenient and uniform asyncronous methods to read any data sources in may ways. Input streams implement read buffering and do read data in blocks where possible.

Note that all read methods share and advance current stream position. It means for example that allBytes() will read not the whole file but the rest of it from the current position.

Construction

Usually, application software does not construct streams but use u8 sources for it. Use constructor only to connect new source.

let input = new InputStream(handle, buferLength = defaultBufferSize);
  • handle: handle-like object providing async read(maxSize) returning proxy resolving to Unit8Array with loaded data.

Binary operations

Read portion

let array = await inputStream.read(maxSize);

Reads up to maxSize bytes. Can return less if the stream closes before the data is received.

Read single byte

returns typed array or undefined on end of stream (EOS) reached.

let value = await inputStream.nextByte()

returns next byte or undefined on EOS.

Read stream to the end

let contents = await inputStream.allBytes();

Read stream from the current position to the end and return it as an Unit8Array, or undefined on EOS.

Binary iterator

Iterate remainig bytes one by one.

for await (let value of inputStream.bytes) { /* value is a byte */ }

String operations

All string operations currently use utf8 encoding. More encoding could be added later on the request.

Read remainig stream as string

let text = await inputStream.allAsString();

returns contents decoded from utf8 or undefined on EOS.

Get next line

Read stream bytes until the EOS or line ending character and decode it as utf8 string. Line ending is not included in the result.

let line = await inputStream.nextLine();

returns the line string or undefined on EOS.

Lines iterator

for await (let line of inputStream.lines) { /* line is a string */ }