Getting wrong array size from GetByteArrayElements
843829Apr 21 2004 — edited May 6 2004Hi
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!