mirror of https://github.com/kiwix/libkiwix.git
Merge pull request #98 from kiwix/jni_byte_range
Add a API to get only a part of a article content.
This commit is contained in:
commit
2357af8f58
|
@ -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<const jbyte*>(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,
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue