I'm using JNI to pass a jobjectarray from java (which is in fact a byte[][]).
I'd to convert this into a usable "byte array" in the form of uint8_t* or vector (ideally the latter) so that it is compatible with all of my existing code which uses it in those forms.
Any advice on how I can best do this? Open to using jbytearray instead if that's cleaner...
I'm not sure if vector<uint8_t*>
would be a good idea here. A vector<vector<uint8_t>>
might be better.
jsize numRows = env->GetArrayLength(objArray);
vector<vector<uint8_t>> v(numRows);
Write a loop where you get the n:th row from the jobjectArray
:
jbyteArray bytes = (jbyteArray) env->GetObjectArrayElement(objArray, n);
Then get a pointer to the actual bytes in that row, and insert those bytes into your vector:
jsize numBytes = env->GetArrayLength(bytes);
uint8_t *data = (uint8_t*) env->GetByteArrayElements(bytes, NULL);
std::copy(data, data + numBytes, std::back_inserter(v[n]));
env->ReleaseByteArrayElements(bytes, JNI_ABORT);
Immediately deleting the local reference to bytes
afterwards may be a good idea, to avoid overflowing the local reference table if you've got a large number of rows:
env->DeleteLocalRef(bytes);
Although a more efficient approach than a byte[][]
might be to use a java.nio.ByteBuffer
obtained with allocateDirect
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.