未知长度的检索字节数组从Java软件商店 [英] Retrieving byte array of unknow length from Java store
问题描述
我已经发布关于它的问题,但是那个时候我还没有该帐户。我得到了一个答复,但我仍然困惑,我无法继续在该线程。
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.
我再一个链接到previous谈话沿着再次发布问题。
I am re posting the question again along with a link to previous conversation.
<一个href=\"http://stackoverflow.com/questions/1227582/returning-char-array-from-java-to-c-jni/1231585#1231585\">Returning从Java字符数组字符串 - JNI
我在Java中存储的数据序列化。我用下面这段code的做一个Java函数调用。
The data I am storing in Java is serialized. I make a java function call using following piece of code.
以下code假定的C char是与Java字节兼容,因为Java的字符是2个字节,而的C char是1字节。该jbyte也是一个符号字符*
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程序。我对code是如何工作有点迷惑。我如何获得指针数组(从Java检索),然后用GetByteArrayElements检索。还记得我不知道数据我会用手之前检索的大小,因此使用NewByteArray功能不能创建一个字节数组,后来在Java code数据填充它。
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.
推荐答案
好吧,我figuered出来。我把它放下这里,所以别人也可以利用它。
Ok, I figuered it out. I'll put it down here so others can also take advantage of it.
考虑到返回一个字节数组(只是一个虚拟的code,没有检查等)以下的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面,你可以检索的byte []如下
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);
}
我已经检查了它和它的作品。我现在要检查它的2-D数组。我觉得这是一样的这只是你会越来越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屋!