use io\streams\Buffer;
use lang\Environment;
$buffer= new Buffer(Environment::tempDir(), 1024);
// This will be written to memory as it's shorter than the threshold
$buffer->write('Slice #1');
// With this write, the buffer exceeds the threshold and is thus saved to a file
$buffer->write(str_repeat('*', 2048));
// Drain the buffer
while ($buffer->available()) {
$chunk= $buffer->read();
}
// Close the buffer, removing the file created by the second write
$buffer->close();
Example:
Inspired by https://github.com/xp-forge/web/issues/118