使用params传递uint_8数组

时间:2013-06-11 06:59:17

标签: android android-ndk

我尝试编写像InputStream:read(byte[] buffer,int offset,int length)这样的方法:

/*
 * Class:     com_readium_ResourceStream
 * Method:    readNative
 * Signature: ([BII)I
 */
JNIEXPORT jint JNICALL Java_com_readium_ResourceStream_readNative
  (JNIEnv *, jobject, jbyteArray, jint, jint);

如何从params写入uint_8数组到jbyteArray?

1 个答案:

答案 0 :(得分:2)

这是我的答案。它必须编译为c ++编译单元。否则你必须将env作为第一个参数传递(即env-> GetArrayLength(...);变为c GetArrayLength(env,...)。

/*
 * Class:     com_readium_ResourceStream
 * Method:    readNative
 * Signature: ([BII)I
 */
JNIEXPORT jint JNICALL Java_com_readium_ResourceStream_readNative
  (JNIEnv *env, jobject obj, jbyteArray buffer, jint offset, jint len)
{
  jint readed;
  // Read data and set readed

  jboolean isCopy;
  jsize arrayLen = env->GetArrayLength(buffer);
  jbyte* array = env->GetByteArrayElements(env, buffer, &isCopy);

  // Use array here

  env->ReleaseByteArrayElements(buffer, array, 0);
  return readed;
}

记住,java字节总是签名