Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

How can I do to share the array elements between "C" and "Java"

Thread view: 
Ejeep - 06 Nov 2007 02:35 GMT
Hi,all

I want to implement the function about this: c and java between JNI
can share the same data area. For example, when change the value of
the array in java, i can get the same value in c.

I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray array,
jboolean isCopy)

But i find that the "isCopy" is JNI_TRUE which means that i get the
pointer which point to a copy,so i can't get the change
in Java later.

How can I do?Can i set "isCopy" with JNI_FALSE??

Thank u.
Gordon Beaton - 06 Nov 2007 06:59 GMT
> I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray
> array, jboolean isCopy)
[quoted text clipped - 4 lines]
>
> How can I do?Can i set "isCopy" with JNI_FALSE??

You can't make any meaningful change to "isCopy", it's purpose is just
to inform the caller.

However when you release the copy (which you must do), the changes
will propagate to the Java array: see ReleaseXxxArrayElements().

Or you could use GetPrimitiveArrayCritical() (and corresponding
Release...), which claim to make it "more likely" that you get a
direct pointer, but then there are additional restrictions on what you
can do while you are holding the array. YMMV.

/gordon

--
Gordon Beaton - 06 Nov 2007 07:04 GMT
> You can't make any meaningful change to "isCopy", it's purpose is just
> to inform the caller.

Agh! "Its purpose", of course. It's early here.

/gordon

--
Lew - 06 Nov 2007 14:54 GMT
>> You can't make any meaningful change to "isCopy", it's purpose is just
>> to inform the caller.
>
> Agh! "Its purpose", of course. It's early here.

You're my hero!

Signature

Lew

Ejeep - 06 Nov 2007 14:11 GMT
> Or you could use GetPrimitiveArrayCritical() (and corresponding
> Release...), which claim to make it "more likely" that you get a
[quoted text clipped - 4 lines]
>
> --

Thank u for you reply.
I use GetPrimitiveArrayCritical() directly in an example code ,and it
is useful.
But in my project ,it appears the error:"fixing up unaligned userspace
access"

How can i fix this???
Gordon Beaton - 06 Nov 2007 14:34 GMT
> I use GetPrimitiveArrayCritical() directly in an example code ,and
> it is useful. But in my project ,it appears the error:"fixing up
> unaligned userspace access"
>
> How can i fix this???

This is not a Java or even a JNI issue. You appear to invoking a
system call from your native code, and are passing a pointer that is
not sufficiently aligned for the specific datatype it refers to. An
example of this is attempting to refer to an "int" at an odd address,
when the architecture requires every "int" to be aligned at an even
address.

/gordon

--
Ejeep - 07 Nov 2007 02:45 GMT
> > I use GetPrimitiveArrayCritical() directly in an example code ,and
> > it is useful. But in my project ,it appears the error:"fixing up
[quoted text clipped - 12 lines]
>
> --

Thank you.

It's hard for me to check the "aligned" question.
The variable I use is the same type "int".
Have any methods to check?

----
ejeep
Gordon Beaton - 07 Nov 2007 06:50 GMT
> It's hard for me to check the "aligned" question.
> The variable I use is the same type "int".
> Have any methods to check?

No, but "proper" use of pointers won't cause this problem. It can only
occur when you cast pointers from one type to another, and they have
different alignment requirements (e.g. from void* or char* to int*).

Again, this isn't a Java or JNI issue.

/gordon

--
Ejeep - 07 Nov 2007 10:45 GMT
> > It's hard for me to check the "aligned" question.
> > The variable I use is the same type "int".
[quoted text clipped - 9 lines]
>
> --

I see.
it isn't a Java or JNI issue
Just don't find the solution.

Thank u very much
Piotr Kobzda - 06 Nov 2007 13:15 GMT
> I want to implement the function about this: c and java between JNI
> can share the same data area. For example, when change the value of
> the array in java, i can get the same value in c.
>
> I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray array,
> jboolean isCopy)

You may also try to use a /direct buffers/ to achieve your goal.

See:
<http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions.html#nio_s
upport
>

piotr
Ejeep - 06 Nov 2007 14:15 GMT
> You may also try to use a /direct buffers/ to achieve your goal.
>
> See:
> <http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions...>
>
> piotr

Thank you for you reply.

I will try it.
By the way,the sun's spec doesn't contain an example.
Sign!~~~
Ejeep - 07 Nov 2007 02:34 GMT
> > I want to implement the function about this: c and java between JNI
> > can share the same data area. For example, when change the value of
[quoted text clipped - 9 lines]
>
> piotr

Hi,Piotr
   I try to use JNI function: GetDirectBufferAddress(),
GetDirectBufferCapacity()
and I get the return value with "NULL" and "-1".

   test code:
   ------------------------------
JNIEXPORT void JNICALL Java_test_nativeInit
 (JNIEnv *env, jclass obj, jintArray arr)
{

   jlong jl;
   char* ch;

   ch = (char*)(*env)->GetDirectBufferAddress(env,arr);
   jl = (*env)->GetDirectBufferCapacity(env,arr);

   return;
}
Piotr Kobzda - 07 Nov 2007 11:38 GMT
>     I try to use JNI function: GetDirectBufferAddress(),
> GetDirectBufferCapacity()
> and I get the return value with "NULL" and "-1".

Most likely your JVM do not support that.  What is the JVM's version,
vendor, and the OS you're trying to use that with?

piotr


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.