0

I tryed to writer method like 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);

How can I write uint_8 array to jbyteArray from params?

1 Answer 1

2

This is my answer. It must be compiled as a c++ compilation unit. Otherwise you have to pass env as the first param (ie env->GetArrayLength(...); become in 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;
}

Remeber, java bytes are always signed.

Sign up to request clarification or add additional context in comments.

2 Comments

GetByteArrayElements gets a pointer to, or copy of, the byte[]. You can then modify it directly. The ReleaseByteArrayElements call unlocks or copies back the modified array.
Thanks fadden and sorry Alex, I forgot to mention it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.