+ small imp

This commit is contained in:
kelson42 2013-04-03 13:05:43 +02:00
parent b88909e0ae
commit 8c69407987
3 changed files with 20 additions and 16 deletions

View File

@ -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
}

View File

@ -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;
}
}

20
kiwix.c
View File

@ -1,6 +1,9 @@
#include <jni.h>
#include "JNIKiwix.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
@ -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;
}