如何在jni中返回具有byte []成员的自定义类?

时间:2019-05-27 10:49:30

标签: java android java-native-interface

[更新]下面的代码现在可用,谢谢大家

我在jni / C代码中得到了一些uint8_t数组,我想用这些uint8_t数组用字节数组成员填充自定义对象,然后将自定义对象返回java端,有人会帮助我吗?谢谢

我在网上搜索,但是很困惑,我仍然无法将数据从C传输到Java端,有人帮我吗?谢谢

package com.example.prj;

/* the result class */
public class ZGKeyPair {

    public byte[] publicKey;
    public byte[] privateKey;

    public byte[] getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(byte[] publicKey) {
        this.publicKey = publicKey;
    }

    public byte[] getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(byte[] privateKey) {
        this.privateKey = privateKey;
    }

    public ZGKeyPair(){
        publicKey  = new byte[64];
        privateKey = new byte[32];
    }    
}


/* the interface class*/
public class ECC {
    static {
        System.loadLibrary("ecc");
    }
    public native ZGKeyPair generateKeyPair();
}


extern "C" JNIEXPORT jobject JNICALL Java_com_example_prj_ECC_generateKeyPair(JNIEnv *env, jclass){

        uint8_t privateKey[32] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 
                                  6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
                                  1, 2};
        uint8_t publicKey[64] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                 1, 2, 3, 4};
       jclass cls = env->FindClass("com/example/prj/ZGKeyPair");
        if(cls == 0 || cls == NULL){

            return NULL;
        }

        jmethodID constructorId = env->GetMethodID(cls, "<init>", "()V");
        if(constructorId == 0 || constructorId == NULL){

            return NULL;
        }

        jobject returnObj = env->NewObject(cls, constructorId);
        if(returnObj == 0 || returnObj == NULL){

            return NULL;
        }

        jfieldID  bytePubId = env->GetFieldID(cls, "publicKey", "[B");
        if(bytePubId == 0 || bytePubId == NULL){

            return NULL;
        }

        jbyteArray pub_bytes = (jbyteArray)(env->GetObjectField(returnObj, bytePubId));
        if(pub_bytes == 0 || pub_bytes == NULL){

            return NULL;
        }
        jbyte* b = env->GetByteArrayElements(pub_bytes, NULL);
        memcpy(b, publicKey, 64);
        env->ReleaseByteArrayElements(pub_bytes, b, 0);


        jfieldID  bytePriId = env->GetFieldID(cls, "privateKey", "[B");
        if(bytePriId == 0 || bytePriId == NULL){

            return NULL;
        }

        jbyteArray pri_bytes = (jbyteArray)(env->GetObjectField(returnObj, bytePriId));
        if(pri_bytes == 0 || pri_bytes == NULL){

            return NULL;
        }
        jbyte* bb = env->GetByteArrayElements(pri_bytes, NULL);
         memcpy(bb, privateKey, 32);
         env->ReleaseByteArrayElements(pri_bytes, bb, 0);

        return returnObj;

}

0 个答案:

没有答案