Skip to content

Core\IO::stdinStream

Reads a program's standard input in pieces, as bytes, for a foreach loop.

Each loop step gives the next piece of the input, up to 64 KiB. The loop ends when the input ends. Only one piece is in memory at a time, so a very large input works too. The bytes are given as they are, so input that is not UTF-8 text is read without an error.

Each piece is tainted, because it comes from outside the program. The input can be read only once. A program started with no input gets no pieces. Core\Hash::of accepts the result directly, so one line computes a checksum of the whole input.

The examples below count the bytes of the input, compute its checksum, and read input from an older system that does not write UTF-8.

Return value

Returns Core\IO\StdinStream. A Core\IO\StdinStream. Each loop step gives the next piece of the input, up to 64 KiB. The loop ends when the input ends. It gives no piece when the input is already closed.

Errors

  • IOErrorThe read failed in the operating system. The process writing to the pipe stopped, or standard input was not open for reading.

Examples

Count the bytes of the input

<?nvs
// Shows how to read standard input in pieces and count its bytes.
// Start it with `nvs run 01-count-the-bytes-of-the-input.nvs < notes.txt`.
// Here the input is the file `01-count-the-bytes-of-the-input.in` beside this one.
uint $pieces = 0;
uint $total = 0;
// Each loop step gives the next piece of the input as `tainted bytes`.
foreach (Core\IO::stdinStream() as tainted bytes $piece) {
$pieces = $pieces + 1;
$total = $total + Core\Bytes::length($piece);
}
// The input is small, so it arrives in one piece. This prints 1 and 56.
// The text has 53 characters. `é`, `ö` and `ß` take two bytes each.
echo 'pieces: ', $pieces, "\n";
echo 'bytes: ', $total, "\n";
Output
pieces: 1
bytes: 56

A checksum of the input

<?nvs
// Shows a small checksum tool, like `sha256sum`, that reads its standard input.
// Start it with `nvs run 02-a-checksum-of-the-input.nvs < backup.tar`.
// Here the input is the file `02-a-checksum-of-the-input.in` beside this one.
// `Core\Hash::of` reads every piece of the stream and returns one digest.
// A large file works too, because only one piece is in memory at a time.
tainted bytes $digest = Core\Hash::of(Core\IO::stdinStream(), Core\Hash::Digest::Sha256);
// `Core\Encoding::toHex` turns the 32 bytes into 64 hex digits.
echo Core\Encoding::toHex($digest), " -\n";
Output
1dd88da5af51639257db4bec04733b3f759246f00dbf0a0b463a9610580684cb -

Input that is not utf 8

<?nvs
// Shows how to read input from an older system that does not write UTF-8.
// Here the input is the file `03-input-that-is-not-utf-8.in` beside this one.
// It is written in windows-1252.
// `Core\IO::stdin` throws an error for this input, because it is not valid UTF-8.
// `Core\IO::stdinStream` gives the pieces as bytes, so nothing is lost.
tainted string $text = '';
foreach (Core\IO::stdinStream() as tainted bytes $piece) {
// This prints "valid UTF-8: no".
bool $valid = Core\Encoding::isValidText($piece, Core\Charset::Utf8);
echo 'valid UTF-8: ', $valid ? 'yes' : 'no', "\n";
// `Core\Encoding::decodeText` reads the bytes as windows-1252.
// In windows-1252 every character is one byte, so each piece is decoded alone.
$text = $text . Core\Encoding::decodeText($piece, Core\Charset::Windows1252);
}
// The result is a normal string, so `Core\Str` methods work on it.
foreach (Core\Str::lines($text) as tainted string $line) {
echo $line, "\n";
}
Output
valid UTF-8: no
Müller;Größe;12
André;Café;3