Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting wrong array size from GetByteArrayElements

843829Apr 21 2004 — edited May 6 2004
Hi

I'm having trouble with GetByteArrayElements() function. I'm passing a byte[] array from JAVA and filling it up in C++ before passing the array back to JAVA (as an out parameter). In Java, I've allocated a byte array of size 1024 to be passed as a parameter to the JNI method. In c++, I'm using the GetByteArrayElements() method to get a reference to the byte array. However, when I tried to fill up the array, it complains of an out-of-memory error. Is there something I'm doing wrong?

My code is something like this::

Java::

public native int jniGetBuffer(byte[] array, int offset, int length);
public static void main(String[] args){
.....
int result = jniGetBuffer(new byte[1024], 0, 1);
....
}

C++::
JNIEXPORT jint
JNICALL Java_jniGetBuffer(JNIEnv *pEnv, jobject, jbyteArray jBuffer, jint jOffset, jint jLength){

int bufferLen = jLength;
jbyte *buffer = pEnv->GetByteArrayElements(jBuffer, NULL);
fillBuffer(bufferLen, (long*) buffer); // fill buffer with 'bufferLen' number of bytes
return bufferLen;
}

Is there something I'm doing wrongly?

Thank yoU!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 3 2004
Added on Apr 21 2004
2 comments
763 views