G
Guest
How can I access an encapsulated Java int array from a
native method, given the Object that declared it? ie, can
anyone replace the 3rd & 4th lines in the first native method
below with actual code that does the job?
Also, assuming that this problem is solvable, is it possible
to keep an array locked-down in the JVM beyond the scope
of the first native method that was called? Obviously, my
goal is to write a third native method that can be called by
the Java side whenever it wants to repopulate an array with
data available to the native code but only go through the over-
head of translating the address space one time. (Yes,
synchronization may be tricky; but for this app, I don't really
care how volitile the data appears to be on the Java side as
long as it gets updated *fast* and without cloning/copying.)
TIA,
// file Main.java
class MyObject
{
int myArray[] = new int[500000];
}
class Main
{
public void native doSomething (Object o);
public void native releaseArray ();
public static void main (String args[])
{
MyObject mo = new MyObject ();
mo.myArray[4] = 4321;
doSomething (mo);
// yada yada ...
releaseArray ();
}
}
// file myjni.cc
include "Main.h"
jbyte *data = NULL;
jarray array;
JNIEXPORT void JNICALL
Java_Main_doSomething (JNIEnv *env, jobject obj, jobject anObject)
{
jclass clazz = env -> GetObjectClass (anObject);
jfieldID fid = env -> GetFieldID (clazz, "myArray", "[I");
// HULP NEEDED HERE!
jniMagic hulp = env -> CallSomeJNImethodIhaventHeardAbout (fid);
array = env -> MoreHulpNeededHere (hulp);
int isCopy;
data = env -> GetPrimitiveArrayCritical (array, &isCopy);
if (data[4] > 4000)
printf ("yeah, verily!\n");
else
printf ("boo, hiss!\n");
}
JNIEXPORT void JNICALL
Java_Main_releaseArray (JNIEnv *env, jobject obj)
{ // is this legal and/or wise?
if (data != NULL)
{
env -> ReleasePrimitiveArrayCritical (array, data, JNI_ABORT);
data = NULL;
}
}
native method, given the Object that declared it? ie, can
anyone replace the 3rd & 4th lines in the first native method
below with actual code that does the job?
Also, assuming that this problem is solvable, is it possible
to keep an array locked-down in the JVM beyond the scope
of the first native method that was called? Obviously, my
goal is to write a third native method that can be called by
the Java side whenever it wants to repopulate an array with
data available to the native code but only go through the over-
head of translating the address space one time. (Yes,
synchronization may be tricky; but for this app, I don't really
care how volitile the data appears to be on the Java side as
long as it gets updated *fast* and without cloning/copying.)
TIA,
// file Main.java
class MyObject
{
int myArray[] = new int[500000];
}
class Main
{
public void native doSomething (Object o);
public void native releaseArray ();
public static void main (String args[])
{
MyObject mo = new MyObject ();
mo.myArray[4] = 4321;
doSomething (mo);
// yada yada ...
releaseArray ();
}
}
// file myjni.cc
include "Main.h"
jbyte *data = NULL;
jarray array;
JNIEXPORT void JNICALL
Java_Main_doSomething (JNIEnv *env, jobject obj, jobject anObject)
{
jclass clazz = env -> GetObjectClass (anObject);
jfieldID fid = env -> GetFieldID (clazz, "myArray", "[I");
// HULP NEEDED HERE!
jniMagic hulp = env -> CallSomeJNImethodIhaventHeardAbout (fid);
array = env -> MoreHulpNeededHere (hulp);
int isCopy;
data = env -> GetPrimitiveArrayCritical (array, &isCopy);
if (data[4] > 4000)
printf ("yeah, verily!\n");
else
printf ("boo, hiss!\n");
}
JNIEXPORT void JNICALL
Java_Main_releaseArray (JNIEnv *env, jobject obj)
{ // is this legal and/or wise?
if (data != NULL)
{
env -> ReleasePrimitiveArrayCritical (array, data, JNI_ABORT);
data = NULL;
}
}