public function writeBytes(string $buf, int $num = null): int
    {
        $bufSize = Binary::safeStrlen($buf);
        if (!\is_int($num) || $num > $bufSize) {
            $num = $bufSize;
        }
        // @codeCoverageIgnoreStart
        if ($num < 0) {
            throw new CannotPerformOperation('num < 0');
        }
        // @codeCoverageIgnoreEnd
        $remaining = $num;
        do {
            // @codeCoverageIgnoreStart
            if ($remaining <= 0) {
                break;
            }
            // @codeCoverageIgnoreEnd
            $written = \fwrite($this->fp, $buf, $remaining);
            if ($written === false) {
                // @codeCoverageIgnoreStart
                throw new FileAccessDenied(
                    'Could not write to the file'
                );
                // @codeCoverageIgnoreEnd
            }
            $buf = Binary::safeSubstr($buf, $written, null);
            $this->pos += $written;
            $this->stat = \fstat($this->fp);
            $remaining -= $written;
        } while ($remaining > 0);
        return $num;
    }