BOOL setTags()

in src/JNI/com/amazonaws/kinesis/video/producer/jni/Parameters.cpp [233:317]


BOOL setTags(JNIEnv *env, jobjectArray tagArray, PTag* ppTags, PUINT32 pTagCount)
{
    STATUS retStatus = STATUS_SUCCESS;
    const char *retChars;
    jclass tagClass = NULL;
    jobject tagObj = NULL;
    jstring retString = NULL;
    jmethodID nameMethodId = NULL;
    jmethodID valueMethodId = NULL;
    PTag pTags = NULL;
    BOOL retValue = TRUE;
    UINT32 tagCount = 0, i = 0;
    PCHAR pCurPtr = NULL;

    // Early exit in case of an optional array of tags
    CHK(tagArray != NULL, STATUS_SUCCESS);

    // Get the length of the array
    tagCount = (UINT32) env->GetArrayLength(tagArray);
    CHK_JVM_EXCEPTION(env);

    // Allocate enough memory.
    // NOTE: We need to add two NULL terminators for tag name and tag value
    CHK(NULL != (pTags = (PTag) MEMCALLOC(tagCount, SIZEOF(Tag) + (MAX_TAG_NAME_LEN +  MAX_TAG_VALUE_LEN + 2) * SIZEOF(CHAR))), STATUS_NOT_ENOUGH_MEMORY);

    // Iterate over and set the values. NOTE: the actual storage for the strings will follow the array
    pCurPtr = (PCHAR) (pTags + tagCount);
    for (;i < tagCount; i++) {
        CHK(NULL != (tagObj = env->GetObjectArrayElement(tagArray, (jsize) i)), STATUS_INVALID_ARG);
        CHK_JVM_EXCEPTION(env);

        // Get the Tag class object and the method IDs first time
        if (tagClass == NULL) {
            CHK(NULL != (tagClass = env->GetObjectClass(tagObj)), STATUS_INVALID_OPERATION);
            CHK_JVM_EXCEPTION(env);

            CHK(NULL != (nameMethodId = env->GetMethodID(tagClass, "getName", "()Ljava/lang/String;")), STATUS_INVALID_OPERATION);
            CHK_JVM_EXCEPTION(env);

            CHK(NULL != (valueMethodId = env->GetMethodID(tagClass, "getValue", "()Ljava/lang/String;")), STATUS_INVALID_OPERATION);
            CHK_JVM_EXCEPTION(env);
        }

        // Get the name of the tag
        CHK(NULL != (retString = (jstring) env->CallObjectMethod(tagObj, nameMethodId)), STATUS_INVALID_ARG);
        CHK_JVM_EXCEPTION(env);

        // Extract the chars and copy
        retChars = env->GetStringUTFChars(retString, NULL);
        STRNCPY(pCurPtr, retChars, MAX_TAG_NAME_LEN + 1);
        pCurPtr[MAX_TAG_NAME_LEN] = '\0';
        env->ReleaseStringUTFChars(retString, retChars);

        // Set the tag pointer and increment the current pointer
        pTags[i].name = pCurPtr;
        pCurPtr += MAX_TAG_NAME_LEN;

        // Get the value of the tag
        CHK(NULL != (retString = (jstring) env->CallObjectMethod(tagObj, valueMethodId)), STATUS_INVALID_ARG);
        CHK_JVM_EXCEPTION(env);

        // Extract the chars and copy
        retChars = env->GetStringUTFChars(retString, NULL);
        STRNCPY(pCurPtr, retChars, MAX_TAG_VALUE_LEN + 1);
        pCurPtr[MAX_TAG_VALUE_LEN] = '\0';
        env->ReleaseStringUTFChars(retString, retChars);

        // Set the tag pointer and increment the current pointer
        pTags[i].value = pCurPtr;
        pCurPtr += MAX_TAG_VALUE_LEN;
    }

    // Set the tag count and the tags
    *pTagCount = tagCount;
    *ppTags = pTags;

CleanUp:

    if (STATUS_FAILED(retStatus)) {
        retValue = FALSE;
        releaseTags(pTags);
    }

    return retValue;
}