从Java存储中检索未知长度的字节数组 [英] Retrieving byte array of unknown length from Java store

查看:153
本文介绍了从Java存储中检索未知长度的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发布了一个有关它的问题,但当时我没有该帐户。我收到了回复,但我仍然感到困惑,我不能继续这个帖子。

I already posted a question regarding it, but at that time I haven't have the account. I got a reply but I was still confused and I cannot continue on that thread.

我再次发布问题以及前一个会话的链接。

I am re posting the question again along with a link to previous conversation.

将char数组从java返回到字符串 - JNI

我在Java中存储的数据是序列化的。我使用下面的代码进行java函数调用。

The data I am storing in Java is serialized. I make a java function call using following piece of code.

以下代码假定C的char与Java的字节兼容,因为Java的char是2字节而C的char是1个字节。 jbyte也是一个带符号的char *

The following code assumes that char of C is compatible with byte of Java, because char of Java is of 2 bytes whereas char of C is of 1 byte. The jbyte is also a signed char*

    //value will be the serialized data
void store(char* key, char* value, int val_len)

{

    //consider the jclass and methodid are already initialized

    jstring j_key = (*env)->NewStringUTF(env, key);
    jbyteArray j_value = (*env)->NewByteArray(env, val_len);

    (*env)->SetByteArrayRegion(env, j_value, 0, val_len, (jbyte *)value);

    //The store method on java side will save the value (as is) in memory
    (*env)->CallStaticVoidMethod(j_class, store_method, key, value);

    (*env)->ReleaseByteArrayElements(env, j_value, (jbyte *)value, JNI_ABORT);
    (*env)->ReleaseStringUTFChars(env, j_key, key);

}

一旦我保存了数据,我使用另一个函数从商店检索数据。那时我不知道我要检索的数据大小。我的API在C中,而商店是在Java中。我将使用我的C函数与Java交互。并且还可以有多个线程同时从Java存储中检索数据。

Once I have saved the data, I use another function to retrieve data from store. At that time i do not know the size of data I am going to retrieve. My API is in C and store is in Java. I will use my C functions to interact with Java. And also there can be multiple threads retrieving data from Java store at same time.

我正在从C调用Java并且我的控件应该在检索数据后返回到C程序。我对代码如何工作有点困惑。我将如何获取指向数组的指针(从java检索),然后使用GetByteArrayElements检索它。记住我不知道我要在手边检索的数据大小,因此不能使用NewByteArray函数创建一个字节数组,然后用java代码中的数据填充它。

I am making calls from C to Java and my control should return to C program after retrieving data. I am a little confuse on how the code will work. How will I get pointer to array (retrieved from java) and then retrieve it using GetByteArrayElements. Remember I dont know the size of data I am going to retrieve before hand and therefore cannot create a byte array using NewByteArray function and later fill it with data in java code.

推荐答案

好的,我把它弄清楚了。我会把它放在这里,所以其他人也可以利用它。

Ok, I figuered it out. I'll put it down here so others can also take advantage of it.

考虑以下java方法返回一个字节数组(只是一个虚拟代码,没有检查等)

Consider the following java method that returns a byte array (just a dummy code, no checks etc)

public static byte[] GetData(){
    return myStore.getData();
}

在C方面,您可以检索字节[]如下

and on C side, you can retrieve the byte[] as following

    void get_data()
{       
    int len = 0;
    char* value = NULL;
    /*Consider j_class, and j_methodid are already initialized*/
    jbyteArray j_value = (*env)->CallStaticObjectMethod(env, j_class, j_methodid);

    if(j_value != NULL)
    {
        len = (*env)->GetArrayLength(env, j_value);
        value = (*env)->GetByteArrayElements(env, j_value, NULL);
    }

    /*later on release the resource*/
    (*env)->ReleaseByteArrayElements(env, j_value, value, 0);
}

我已检查过它并且有效。我现在要检查二维阵列。我认为只有你得到jobjectArray,并且这个数组的每个元素都是jbyteArray。

I have checked it and it works. I am going to check it for 2-D array now. I think it'd be the same as this only you'd be getting jobjectArray and every element of this array is a jbyteArray.

这篇关于从Java存储中检索未知长度的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆