Jni fix
This commit is contained in:
Matthieu Gautier 2019-08-12 14:55:30 +02:00 committed by GitHub
commit 99dcdd849a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 15 deletions

View File

@ -64,7 +64,7 @@ Java_org_kiwix_kiwixlib_JNIKiwixLibrary_addBook(JNIEnv* env, jobject obj, jstrin
book.update(reader); book.update(reader);
ret = LIBRARY->addBook(book); ret = LIBRARY->addBook(book);
} catch (std::exception& e) { } catch (std::exception& e) {
__android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM main page"); __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to add the book");
__android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what());
ret = false; ret = false;
} }

View File

@ -225,7 +225,7 @@ JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getMimeType(
} }
JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent( JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent(
JNIEnv* env, jobject obj, jstring url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj) JNIEnv* env, jobject obj, jobject url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj)
{ {
/* Default values */ /* Default values */
setStringObjValue("", titleObj, env); setStringObjValue("", titleObj, env);
@ -234,21 +234,24 @@ JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent(
jbyteArray data = env->NewByteArray(0); jbyteArray data = env->NewByteArray(0);
/* Retrieve the content */ /* Retrieve the content */
std::string cUrl = jni2c(url, env); std::string cUrl = getStringObjValue(url, env);
unsigned int cSize = 0; unsigned int cSize = 0;
try { try {
auto entry = READER->getEntryFromEncodedPath(cUrl); auto entry = READER->getEntryFromEncodedPath(cUrl);
bool isRedirect = entry.isRedirect();
entry = entry.getFinalEntry(); entry = entry.getFinalEntry();
cSize = entry.getSize(); cSize = entry.getSize();
setIntObjValue(cSize, sizeObj, env); setIntObjValue(cSize, sizeObj, env);
setStringObjValue(entry.getMimetype(), mimeTypeObj, env);
setStringObjValue(entry.getTitle(), titleObj, env);
if (isRedirect) {
setStringObjValue(entry.getPath(), url, env);
} else {
data = env->NewByteArray(cSize); data = env->NewByteArray(cSize);
env->SetByteArrayRegion( env->SetByteArrayRegion(
data, 0, cSize, reinterpret_cast<const jbyte*>(entry.getBlob().data())); data, 0, cSize, reinterpret_cast<const jbyte*>(entry.getBlob().data()));
}
setStringObjValue(entry.getMimetype(), mimeTypeObj, env);
setStringObjValue(entry.getTitle(), titleObj, env);
} catch (std::exception& e) { } catch (std::exception& e) {
__android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get content for url: %s", cUrl.c_str()); __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get content for url: %s", cUrl.c_str());
__android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what());

View File

@ -26,7 +26,7 @@ public class JNIKiwixLibrary
{ {
public native boolean addBook(String path) throws JNIKiwixException; public native boolean addBook(String path) throws JNIKiwixException;
public JNIKiwixLibrary(String filename) public JNIKiwixLibrary()
{ {
nativeHandle = getNativeLibrary(); nativeHandle = getNativeLibrary();
} }

View File

@ -38,7 +38,21 @@ public class JNIKiwixReader
public native String getMimeType(String url); public native String getMimeType(String url);
public native byte[] getContent(String url, /**
* Get the content of a article.
*
* Return a byte array of the content of the article.
* Set the title, mimeType to the title and mimeType of the article.
* Set the size to the size of the returned array.
*
* If the entry doesn't exist :
* - return a empty byte array
* - set all arguments (except url) to empty/0.
* If the entry exist but is a redirection :
* - return an empty byte array
* - set all arguments (including url) to information of the targeted article.
*/
public native byte[] getContent(JNIKiwixString url,
JNIKiwixString title, JNIKiwixString title,
JNIKiwixString mimeType, JNIKiwixString mimeType,
JNIKiwixInt size); JNIKiwixInt size);

View File

@ -114,6 +114,13 @@ inline std::string jni2c(const jstring& val, JNIEnv* env)
inline int jni2c(const jint val) { return (int)val; } inline int jni2c(const jint val) { return (int)val; }
/* Method to deal with variable passed by reference */ /* Method to deal with variable passed by reference */
inline std::string getStringObjValue(const jobject obj, JNIEnv* env)
{
jclass objClass = env->GetObjectClass(obj);
jfieldID objFid = env->GetFieldID(objClass, "value", "Ljava/lang/String;");
jstring jstr = (jstring)env->GetObjectField(obj, objFid);
return jni2c(jstr, env);
}
inline void setStringObjValue(const std::string& value, inline void setStringObjValue(const std::string& value,
const jobject obj, const jobject obj,
JNIEnv* env) JNIEnv* env)

View File

@ -29,7 +29,9 @@
namespace kiwix namespace kiwix
{ {
/* Constructor */ /* Constructor */
Book::Book() : m_readOnly(false) Book::Book() :
m_pathValid(false),
m_readOnly(false)
{ {
} }
/* Destructor */ /* Destructor */
@ -46,6 +48,7 @@ bool Book::update(const kiwix::Book& other)
if (m_path.empty()) { if (m_path.empty()) {
m_path = other.m_path; m_path = other.m_path;
m_pathValid = other.m_pathValid;
} }
if (m_url.empty()) { if (m_url.empty()) {
@ -83,6 +86,7 @@ void Book::update(const kiwix::Reader& reader)
m_articleCount = reader.getArticleCount(); m_articleCount = reader.getArticleCount();
m_mediaCount = reader.getMediaCount(); m_mediaCount = reader.getMediaCount();
m_size = static_cast<uint64_t>(reader.getFileSize()) << 10; m_size = static_cast<uint64_t>(reader.getFileSize()) << 10;
m_pathValid = true;
reader.getFavicon(m_favicon, m_faviconMimeType); reader.getFavicon(m_favicon, m_faviconMimeType);
} }

View File

@ -121,22 +121,24 @@ std::string computeAbsolutePath(const std::string& path, const std::string& rela
#else #else
char* cRelativePath = strdup(relativePath.c_str()); char* cRelativePath = strdup(relativePath.c_str());
#endif #endif
char* token = strtok(cRelativePath, "/"); char* saveptr = nullptr;
char* token = strtok_r(cRelativePath, "/", &saveptr);
while (token != NULL) { while (token != NULL) {
if (std::string(token) == "..") { if (std::string(token) == "..") {
absolutePath = removeLastPathElement(absolutePath, true, false); absolutePath = removeLastPathElement(absolutePath, true, false);
token = strtok(NULL, "/"); token = strtok_r(NULL, "/", &saveptr);
} else if (strcmp(token, ".") && strcmp(token, "")) { } else if (strcmp(token, ".") && strcmp(token, "")) {
absolutePath += std::string(token); absolutePath += std::string(token);
token = strtok(NULL, "/"); token = strtok_r(NULL, "/", &saveptr);
if (token != NULL) { if (token != NULL) {
absolutePath += SEPARATOR; absolutePath += SEPARATOR;
} }
} else { } else {
token = strtok(NULL, "/"); token = strtok_r(NULL, "/", &saveptr);
} }
} }
free(cRelativePath);
return absolutePath; return absolutePath;
} }