SetByteArrayRegion Behavior

I was wondering if someone could help me understand this better...

I have a large amount of memory generated in a C++ application that I want to make available to my JVM. I'm currently using SetByteArrayRegion() to get the data into my Java object, however I believe that this is making a copy of the data, rather than referencing the shared memory from the C++ application.

Is there a way to simply pass a shared pointer in to the JVM and use it as a byte[]? When I've tried that, I end up with access violations from the JVM.

Thanks.

[562 byte] By [jheddingsa] at [2007-10-3 5:04:25]
# 1
Added some dukes... Would like some help on this one.
jheddingsa at 2007-7-14 23:10:20 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

You should use the [url http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/functions.html#nio_support]JNI NIO support[/url] (JDK 1.4+).

This very simple example should give you an idea how to use it.// MemAccess.java:

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

public class MemAccess {

static {

System.loadLibrary("MemAccess");

}

public static void main(String[] args) {

new MemAccess().readGeneratedMem();

}

void readGeneratedMem() {

// get a reference to the object that holds a pointer to the c++ buffer

ByteBuffer buffer = getGeneratedMem();

buffer.order(ByteOrder.nativeOrder());

int nbr = 16;

// walk through the c++ buffer to display the values

for (int i = 0; i < nbr; i++)

System.out.println(buffer.getInt());

}

native ByteBuffer getGeneratedMem();

}

// MemAccess.h:

#ifndef _Included_MemAccess

#define _Included_MemAccess

#include <jni.h>

#ifdef __cplusplus

extern "C" {

#endif

JNIEXPORT jobject JNICALL Java_MemAccess_getGeneratedMem(JNIEnv * env, jobject obj);

#ifdef __cplusplus

}

#endif

#endif /* _Included_MemAccess */

// MemAccess.cpp:

#include "MemAccess.h"

JNIEXPORT jobject JNICALL Java_MemAccess_getGeneratedMem(JNIEnv * env, jobject obj) {

int nbr = 16;

int * buffer = new int[nbr]; // c++ buffer

for (int i = 0; i < nbr; i++)

buffer[i] = i * 2;

// return a reference of a (Java) object that holds a pointer to the c++ buffer

return env->NewDirectByteBuffer(buffer, nbr * sizeof(int));

}

Regards

jfbrierea at 2007-7-14 23:10:20 > top of Java-index,Java HotSpot Virtual Machine,Specifications...