ZSpirytus
ZSpirytus

Reputation: 359

Convert jbytearray to char* with wrong code in JNI

I try to convert jbytearray to char* at Jni Layer. However, its result has some wrong character like this:

07-23 10:22:43.701 26585-26646/com.zspirytus.androidlua D/ScriptPkgDataFetcher: getPkgData: 0028 at Kotlin
07-23 10:22:43.701 26585-26646/com.zspirytus.androidlua D/LuaEngine: getPkgData: 0028�ض
07-23 10:22:44.221 26585-26646/com.zspirytus.androidlua D/ScriptPkgDataFetcher: getPkgData: 0028 at Kotlin
07-23 10:22:44.221 26585-26646/com.zspirytus.androidlua D/LuaEngine: getPkgData: 0028�ض
07-23 10:22:44.721 26585-26646/com.zspirytus.androidlua D/ScriptPkgDataFetcher: getPkgData: 0028 at Kotlin
07-23 10:22:44.721 26585-26646/com.zspirytus.androidlua D/LuaEngine: getPkgData: 0028�ض

At Kotlin Layer, the result in String type is "0028"; at Jni Layer, the converted result is "0028�ض". The following is what I do in my code:

At Kotlin, the function return a bytearray:

fun getContentByEntryName(entryName: String): ByteArray {
    // data is always a String with value "0028"
    val data = ZipFileUtils.getFileContentFromZipFile(ZipFile(scriptPkg), entryName)
    Log.d("ScriptPkgDataFetcher", "getPkgDatagetPkgData: $data at Kotlin")
    return data.toByteArray()
}

At Jni, I convert jbytearray to char* like this:

jbyteArray jba = (jbyteArray) env->CallStaticObjectMethod(clazz, methodId, dataPath);
int len = env->GetArrayLength (jba);
char* buff = new char[len];
env->GetByteArrayRegion (jba, 0, len, reinterpret_cast<jbyte*>(buff));
Log_d(LOG_TAG, "getPkgData: %s", buff);

Seem that it is not work correct. I also try this code, but it still not work correct...

jbyteArray jba = (jbyteArray) env->CallStaticObjectMethod(clazz, methodId, dataPath);
const char *cStr = (char *) (env)->GetByteArrayElements(jba, NULL);

Is any mistake existing in my code? Help me to correct it, Please. Thanks!

Upvotes: 0

Views: 1436

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

If you're going to use new char[] to create string data, and then use that data in functions that require null-termination, you have to explicitly add the null terminator.

In addition, you may not have allocated enough space for the terminating null, thus you need to allocate len + 1 bytes.

You can do this:

char* buff = new char[len + 1]();

which fills the buffer with null characters or:

char* buff = new char[len + 1];
buff[len] = '\0';

However I highly suggest you use a std::string or std::vector<char> instead of doing things this way.

Since JNI is very fragile, if an exception is thrown, you may get memory leaks using raw pointers this way.

Here is a small routine you can use to convert from a Java byte array to a vector:

#include <vector>
//...
jbyteArray jba = (jbyteArray) env->CallStaticObjectMethod(clazz, methodId, dataPath);
int len = env->GetArrayLength (jba);
std::vector<char> buff(len + 1, 0);
env->GetByteArrayRegion (jba, 0, len, reinterpret_cast<jbyte*>(buff.data()));
Log_d(LOG_TAG, "getPkgData: %s", buff.data());

This will not suffer from memory leaks if an exception is thrown, since std::vector will automatically deallocate the memory when the vector goes out of scope for any reason.

Upvotes: 2

Related Questions