static jintArray index_set_to_indices_array(JNIEnv* env, const IndexSet& index_set)
{
    if (index_set.empty()) {
        return env->NewIntArray(0);
    }

    std::vector<jint> indices_vector;
    for (auto index : index_set.as_indexes()) {
        indices_vector.push_back(index);
    }
    if (indices_vector.size() > io_realm_internal_OsCollectionChangeSet_MAX_ARRAY_LENGTH) {
        std::ostringstream error_msg;
        error_msg << "There are too many indices in this change set. They cannot fit into an array."
                  << " indices_vector's size: " << indices_vector.size()
                  << " Java array's max size: " << io_realm_internal_OsCollectionChangeSet_MAX_ARRAY_LENGTH << ".";
        ThrowException(env, IllegalState, error_msg.str());
        return nullptr;
    }
    jintArray jint_array = env->NewIntArray(static_cast<jsize>(indices_vector.size()));
    env->SetIntArrayRegion(jint_array, 0, indices_vector.size(), indices_vector.data());
    return jint_array;
}