From 8c694079874d95b45bd006fe766e6ac4ef9b1e3b Mon Sep 17 00:00:00 2001 From: kelson42 Date: Wed, 3 Apr 2013 13:05:43 +0200 Subject: [PATCH] + small imp --- JNIKiwix.h | 4 ++-- JNIKiwix.java | 12 +++++------- kiwix.c | 20 +++++++++++++------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/JNIKiwix.h b/JNIKiwix.h index 1f41e1bc5..14885c77e 100644 --- a/JNIKiwix.h +++ b/JNIKiwix.h @@ -18,10 +18,10 @@ JNIEXPORT jboolean JNICALL Java_JNIKiwix_nativeLoadZIM /* * Class: JNIKiwix * Method: nativeGetContent - * Signature: (Ljava/lang/String;LJNIKiwixString;LJNIKiwixInt;LJNIKiwixBool;)[B + * Signature: (Ljava/lang/String;LJNIKiwixString;LJNIKiwixInt;)[B */ JNIEXPORT jbyteArray JNICALL Java_JNIKiwix_nativeGetContent - (JNIEnv *, jobject, jstring, jobject, jobject, jobject); + (JNIEnv *, jobject, jstring, jobject, jobject); #ifdef __cplusplus } diff --git a/JNIKiwix.java b/JNIKiwix.java index ed751ec5c..0307eb97e 100644 --- a/JNIKiwix.java +++ b/JNIKiwix.java @@ -1,8 +1,7 @@ public class JNIKiwix { public native boolean nativeLoadZIM(String path); - public native byte[] nativeGetContent(String url, JNIKiwixString mimeType, - JNIKiwixInt size, JNIKiwixBool isOk); - + public native byte[] nativeGetContent(String url, JNIKiwixString mimeType, JNIKiwixInt size); + static { System.loadLibrary("kiwix"); } @@ -15,11 +14,10 @@ public class JNIKiwix { JNIKiwixString mimeTypeObj = new JNIKiwixString(); JNIKiwixInt sizeObj = new JNIKiwixInt(); - JNIKiwixBool isOkObj = new JNIKiwixBool(); - self.nativeGetContent("/A/Wikipedia.html", mimeTypeObj, sizeObj, isOkObj); + byte[] data = self.nativeGetContent("/A/Wikipedia.html", mimeTypeObj, sizeObj); System.out.println(mimeTypeObj.value); System.out.println(sizeObj.value); - System.out.println(isOkObj.value); + System.out.println(data[0]); } catch (Exception e) { System.out.println("Message " + e.getMessage()); @@ -40,4 +38,4 @@ class JNIKiwixInt { class JNIKiwixBool { boolean value; -} \ No newline at end of file +} diff --git a/kiwix.c b/kiwix.c index 7b68ac4ef..d460647d5 100644 --- a/kiwix.c +++ b/kiwix.c @@ -1,6 +1,9 @@ #include #include "JNIKiwix.h" +#include +#include + #include #include @@ -42,10 +45,9 @@ void setStringObjValue(const std::string &value, const jobject obj, JNIEnv *env) } void setIntObjValue(const int value, const jobject obj, JNIEnv *env) { - int tmp = value; jclass objClass = env->GetObjectClass(obj); jfieldID objFid = env->GetFieldID(objClass, "value", "I"); - env->SetIntField(obj, objFid, c2jni(tmp)); + env->SetIntField(obj, objFid, value); } void setBoolObjValue(const bool value, const jobject obj, JNIEnv *env) { @@ -60,12 +62,16 @@ JNIEXPORT jboolean JNICALL Java_JNIKiwix_nativeLoadZIM(JNIEnv *env, jobject obj, } JNIEXPORT jbyteArray JNICALL Java_JNIKiwix_nativeGetContent(JNIEnv *env, jobject obj, jstring url, - jobject mimeTypeObj, jobject sizeObj, - jobject isOkObj) { + jobject mimeTypeObj, jobject sizeObj) { setStringObjValue("42", mimeTypeObj, env); setIntObjValue(42, sizeObj, env); - setBoolObjValue(false, isOkObj, env); - // Java program dies otherwise (please keep this "useless" line) - std::cout << ""; + std::string cData = "This is great!"; + jbyteArray data = env->NewByteArray(6); + jbyte *dataPointer = env->GetByteArrayElements(data, 0); + memcpy(dataPointer, cData.c_str(), c2jni(cData.size())); + env->ReleaseByteArrayElements(data, dataPointer, 0); + + return data; } +