Core\IO\File::readBytes
Reads a part of an open file as bytes, exactly as they are stored.
$file->readBytes($max) works on a Core\IO\File that Core\IO::open returned. It reads up to $max bytes from the current position, and moves the position past them. The next call starts where this one stopped. When the file has no more bytes, the result is empty bytes.
Use it for a file that is not text, such as an image, an archive or a file in a binary format. The bytes do not have to be valid UTF-8, and nothing is decoded or changed. To read text, use read. It returns a string and never cuts a character in two.
A closed handle throws a RuntimeError. A handle that can only write throws an IOError.
The examples below check the start of an image file, read a file in fixed-size parts, and read bytes that are not UTF-8.
Parameters
$maxuintThe most bytes to read. Fewer are returned when the file ends first.
Return value
Returns bytes. The bytes read. Empty bytes means the end of the file.
Errors
RuntimeErrorThe handle has already been closed.IOErrorThe read itself failed, or the handle was not opened for reading.
Examples
Check the header of an image
<?nvs// Shows how to read the first bytes of a file to check what kind of file it is.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photo.png');Core\IO::write($path, Core\Encoding::fromHex('89504e470d0a1a0a0000000d49484452'));
// Every PNG image starts with these 8 bytes.bytes $pngSignature = Core\Encoding::fromHex('89504e470d0a1a0a');
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);// `readBytes(8)` reads the first 8 bytes. This prints "89504e470d0a1a0a".bytes $header = $file->readBytes(8);echo Core\Encoding::toHex($header), "\n";
// The header is the same as the signature, so this prints "PNG image: yes".echo 'PNG image: ', $header == $pngSignature ? 'yes' : 'no', "\n";
// The next call starts at byte 8. This prints "0000000d".echo Core\Encoding::toHex($file->readBytes(4)), "\n";$file->close();89504e470d0a1a0aPNG image: yes0000000dRead a file in fixed size parts
<?nvs// Shows how to read a whole file in parts of the same size, until it ends.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'backup.bin');Core\IO::write($path, Core\Bytes::repeat(Core\Encoding::fromHex('00ff'), 5000));
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);uint $parts = 0;uint $total = 0;
// Each call returns up to 4096 bytes. Empty `bytes` means the file has ended.bytes $part = $file->readBytes(4096);while (Core\Bytes::length($part) > 0) { $parts = $parts + 1; $total = $total + Core\Bytes::length($part); $part = $file->readBytes(4096);}$file->close();
// The file has 10000 bytes. That is two full parts and one part of 1808 bytes.echo 'parts: ', $parts, "\n";echo 'bytes: ', $total, "\n";parts: 3bytes: 10000Bytes that are not text
<?nvs// Shows that `readBytes` returns bytes that are not valid UTF-8 exactly as they are in the file.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'export-utf16.csv');// This file is UTF-16 text. It starts with the bytes ff fe, which are not valid UTF-8.Core\IO::write($path, Core\Encoding::fromHex('fffe49004400'));
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);// The first 2 bytes come back unchanged. This prints "fffe".bytes $start = $file->readBytes(2);echo Core\Encoding::toHex($start), "\n";
// A UTF-16 file often starts with ff fe, so this prints "UTF-16: yes".echo 'UTF-16: ', $start == Core\Encoding::fromHex('fffe') ? 'yes' : 'no', "\n";
// The rest of the file is "ID" in UTF-16. This prints "49004400".echo Core\Encoding::toHex($file->readBytes(100)), "\n";$file->close();fffeUTF-16: yes49004400