Skip to content

Core\Response\Stream::length

$stream->length(): int

Returns how many bytes your program writes to the stream. It works on a stream that Core\Response::startStream started with a $length.

length() is the size of the whole body, or the size of the part the browser asked for with a Range header. It is 0 when nothing is sent. That happens when the browser already has this version of the body, or when it asked for a part the body does not have.

Your writes must add up to exactly this number. A write past it throws a LogicError and sends nothing. If the program ends before it has written this many bytes, the connection is closed. The browser then knows that the body is not complete.

A stream without a $length throws a LogicError here.

The examples show a part written in small pieces, a body with a version name, and a write past the length.

Return value

Returns int. An int. It is 0 when nothing is sent: the browser already has this version, or it asked for a part the body does not have. Your writes must add up to exactly this number. If the program ends before that, the connection is closed, so the browser knows the body is not complete.

Errors

  • LogicErrorThe stream was started without a $length.

Examples

Write the part in pieces

<?nvs
// Shows how to write the part in pieces whose sizes add up to `length()`.
bytes $text = "One, two, three, four, five.\n" as bytes;
Core\Response\Stream $body = Core\Response::startStream('text/plain; charset=utf-8', Core\Bytes::length($text) as int);
// Each piece is 8 bytes or less. The loop stops when exactly `length()` bytes are written.
int $written = 0;
while ($written < $body->length()) {
int $size = $body->length() - $written;
if ($size > 8) {
$size = 8;
}
$body->write(Core\Bytes::slice($text, $body->offset() + $written, $size));
$written = $written + $size;
}
Output
One, two, three, four, five.

A body with a version name

<?nvs
// Shows how to give the body a version name, so that a browser can resume a download safely.
bytes $report = "month,total\nJanuary,1200\nFebruary,950\n" as bytes;
// The third argument is sent as the `ETag` header. Give a new name when the bytes change.
// A browser that resumes a download with an old name gets the whole body again.
Core\Response\Stream $body = Core\Response::startStream('text/csv', Core\Bytes::length($report) as int, 'report-2');
// This request asks for no part, so `length()` is the size of the whole report.
$body->write(Core\Bytes::slice($report, $body->offset(), $body->length()));
Output
month,total
January,1200
February,950

A write past the length

<?nvs
// Shows that a write past `length()` throws a `LogicError` and sends nothing.
Core\Response\Stream $body = Core\Response::startStream('text/plain; charset=utf-8', 6);
$body->write("Hello\n");
try {
// The 6 bytes are already written, so this part does not fit.
$body->write("More text\n");
} catch (LogicError $error) {
// Nothing more is sent. The body is only "Hello".
}
Output
Hello