From 4e5d9f036032f5c439ac1f78ab9438a6ec9a88cd Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 22 Nov 2017 19:06:54 +0000 Subject: [PATCH] Add a API to get only a part of a article content. Add the jni method `getContentPart` to get only a part of the artcicle content. The method can be used to get a part of the content or to know the size of the full content. --- src/android/kiwixreader.cpp | 40 +++++++++++++++++++ .../org/kiwix/kiwixlib/JNIKiwixReader.java | 15 +++++++ 2 files changed, 55 insertions(+) diff --git a/src/android/kiwixreader.cpp b/src/android/kiwixreader.cpp index e50e100ca..ea25e3373 100644 --- a/src/android/kiwixreader.cpp +++ b/src/android/kiwixreader.cpp @@ -237,6 +237,46 @@ JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent( return data; } +JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContentPart( + JNIEnv* env, jobject obj, jstring url, jint offset, jint len, jobject sizeObj) +{ + jbyteArray data = env->NewByteArray(0); + setIntObjValue(0, sizeObj, env); + + /* Default values */ + /* Retrieve the content */ + std::string cUrl = jni2c(url, env); + int cOffset = jni2c(offset); + int cLen = jni2c(len); + try { + zim::Article article; + READER->getArticleObjectByDecodedUrl(kiwix::urlDecode(cUrl), article); + if (! article.good()) { + return data; + } + int loopCounter = 0; + while (article.isRedirect() && loopCounter++ < 42) { + article = article.getRedirectArticle(); + } + if (loopCounter == 42) { + return data; + } + if (cLen == 0) { + setIntObjValue(article.getArticleSize(), sizeObj, env); + } else if (cOffset+cLen > article.getArticleSize()) { + auto blob = article.getData(cOffset, cLen); + data = env->NewByteArray(cLen); + env->SetByteArrayRegion( + data, 0, cLen, reinterpret_cast(blob.data())); + setIntObjValue(cLen, sizeObj, env); + } + } catch (...) { + std::cerr << "Unable to get partial content for url " << cUrl + << "(" << cOffset << ":" << cLen << ")" << std::endl; + } + return data; +} + JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_searchSuggestions(JNIEnv* env, jobject obj, diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java b/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java index db416d4bc..ffae39d0c 100644 --- a/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java +++ b/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java @@ -41,6 +41,21 @@ public class JNIKiwixReader JNIKiwixString mimeType, JNIKiwixInt size); + /** + * getContentPart. + * + * Get only a part of the content of the article. + * Return a byte array of `len` size starting from offset `offset`. + * Set `size` to the number of bytes read + * (`len` if everything is ok, 0 in case of error). + * If `len` == 0, no bytes are read but `size` is set to the total size of the + * article. + */ + public native byte[] getContentPart(String url, + int offest, + int len, + JNIKiwixInt size); + public native boolean searchSuggestions(String prefix, int count); public native boolean getNextSuggestion(JNIKiwixString title);