JNI Novice question

Discussion in 'Java' started by Aaron Fude, May 25, 2004.

  1. Aaron Fude

    Aaron Fude Guest

    Hi,

    Is it possible to be in control of allocating your memory for arrays? In
    other words, rather than

    jintArray tri = inEnv->NewIntArray(10);
    jint *thearray = (jint *) (inEnv->GetIntArrayElements(tri, 0));

    to do

    jint *thearray = new jint[10];
    jintArray tri = inEnv->NewIntArray(Use theArray);

    Thanks!

    Aaron Fude
     
    Aaron Fude, May 25, 2004
    #1
    1. Advertising

  2. On Tue, 25 May 2004 17:58:10 -0400, Aaron Fude wrote:
    > Is it possible to be in control of allocating your memory for
    > arrays?


    [...]

    > jint *thearray = new jint[10];
    > jintArray tri = inEnv->NewIntArray(Use theArray);


    Not exactly like that, but have a look at NewDirectByteBuffer(),
    described under "JNI enhancements in 1.4". It lets you create a
    ByteBuffer from memory you've allocated using some other mechanism.
    Other JNI functions let you retrieve the address and size of the
    original buffer:

    jobject allocateByteBuffer(JNIEnv *env, jclass this, jint size)
    {
    void *p;

    if ((p = malloc(size))) {
    return (*env)->NewDirectByteBuffer(env,p,size);
    }
    else return NULL;
    }

    void freeByteBuffer(JNIEnv *env, jclass this, jobject buf)
    {
    char *p = (*env)->GetDirectBufferAddress(env,buf);
    free(p);
    }

    In Java, you can treat the ByteBuffer as different buffer type, for
    example:

    ByteBuffer bb = allocateByteBuffer(100); // allocate ByteBuffer
    IntBuffer ib = bb.asIntBuffer(); // get IntBuffer view

    In theory you should be able to do the following to get an int array
    backed by the ByteBuffer you've just allocated, but unfortunately the
    array() method is optional and does not seem to be implemented for
    ByteBuffers created this way:

    if (ib.hasArray()) {
    int[] arr = ib.array();
    }

    Also have a look at java.nio.MappedByteBuffer, which appears to let
    you do something similar with sections of a mapped file.

    You didn't mention what specific problem you're trying to solve, so it
    might be worth pointing out that when you do this:

    > jint *thearray = (jint *) (inEnv->GetIntArrayElements(tri, 0));


    ....that you can make modifications to thearray just as you would any
    other array of (j)int, and any changes will propagate back to the
    original int[] when you later call ReleaseIntArrayElements() (i.e. you
    don't need to use SetIntArrayRegion() to write to the int[]).

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, May 26, 2004
    #2
    1. Advertising

  3. Aaron Fude

    Aaron Fude Guest

    Thank you. Very informative.

    "Gordon Beaton" <> wrote in message
    news:40b4de87$...
    > On Tue, 25 May 2004 17:58:10 -0400, Aaron Fude wrote:
    > > Is it possible to be in control of allocating your memory for
    > > arrays?

    >
    > [...]
    >
    > > jint *thearray = new jint[10];
    > > jintArray tri = inEnv->NewIntArray(Use theArray);

    >
    > Not exactly like that, but have a look at NewDirectByteBuffer(),
    > described under "JNI enhancements in 1.4". It lets you create a
    > ByteBuffer from memory you've allocated using some other mechanism.
    > Other JNI functions let you retrieve the address and size of the
    > original buffer:
    >
    > jobject allocateByteBuffer(JNIEnv *env, jclass this, jint size)
    > {
    > void *p;
    >
    > if ((p = malloc(size))) {
    > return (*env)->NewDirectByteBuffer(env,p,size);
    > }
    > else return NULL;
    > }
    >
    > void freeByteBuffer(JNIEnv *env, jclass this, jobject buf)
    > {
    > char *p = (*env)->GetDirectBufferAddress(env,buf);
    > free(p);
    > }
    >
    > In Java, you can treat the ByteBuffer as different buffer type, for
    > example:
    >
    > ByteBuffer bb = allocateByteBuffer(100); // allocate ByteBuffer
    > IntBuffer ib = bb.asIntBuffer(); // get IntBuffer view
    >
    > In theory you should be able to do the following to get an int array
    > backed by the ByteBuffer you've just allocated, but unfortunately the
    > array() method is optional and does not seem to be implemented for
    > ByteBuffers created this way:
    >
    > if (ib.hasArray()) {
    > int[] arr = ib.array();
    > }
    >
    > Also have a look at java.nio.MappedByteBuffer, which appears to let
    > you do something similar with sections of a mapped file.
    >
    > You didn't mention what specific problem you're trying to solve, so it
    > might be worth pointing out that when you do this:
    >
    > > jint *thearray = (jint *) (inEnv->GetIntArrayElements(tri, 0));

    >
    > ...that you can make modifications to thearray just as you would any
    > other array of (j)int, and any changes will propagate back to the
    > original int[] when you later call ReleaseIntArrayElements() (i.e. you
    > don't need to use SetIntArrayRegion() to write to the int[]).
    >
    > /gordon
    >
    > --
    > [ do not email me copies of your followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Aaron Fude, May 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    885
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    947
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,784
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    642
    vasanth
    Jan 25, 2005
  5. bgabrhelik
    Replies:
    0
    Views:
    895
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page