0

我尝试编写方法,例如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);

如何从参数将 uint_8 数组写入 jbyteArray?

4

1 回答 1

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 字节总是有符号的

于 2013-06-11T09:13:12.573 回答