diff --git a/src/wrapper/java/kiwixreader.cpp b/src/wrapper/java/kiwixreader.cpp index e55d54f..e62eb30 100644 --- a/src/wrapper/java/kiwixreader.cpp +++ b/src/wrapper/java/kiwixreader.cpp @@ -45,6 +45,35 @@ JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReader( } } +namespace +{ + +int jni2fd(const jobject& fdObj, JNIEnv* env) +{ + jclass class_fdesc = env->FindClass("java/io/FileDescriptor"); + jfieldID field_fd = env->GetFieldID(class_fdesc, "fd", "I"); + return env->GetIntField(fdObj, field_fd); +} + +} // unnamed namespace + +JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReaderByFD( + JNIEnv* env, jobject obj, jobject fdObj) +{ + int fd = jni2fd(fdObj, env); + + LOG("Attempting to create reader with fd: %d", fd); + Lock l; + try { + kiwix::Reader* reader = new kiwix::Reader(fd); + return reinterpret_cast(new Handle(reader)); + } catch (std::exception& e) { + LOG("Error opening ZIM file"); + LOG(e.what()); + return 0; + } +} + JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_dispose(JNIEnv* env, jobject obj) { diff --git a/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixReader.java b/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixReader.java index f94c3dc..fe5e39e 100644 --- a/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixReader.java +++ b/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixReader.java @@ -25,6 +25,7 @@ import org.kiwix.kiwixlib.JNIKiwixString; import org.kiwix.kiwixlib.JNIKiwixInt; import org.kiwix.kiwixlib.JNIKiwixSearcher; import org.kiwix.kiwixlib.Pair; +import java.io.FileDescriptor; public class JNIKiwixReader { @@ -151,11 +152,21 @@ public class JNIKiwixReader throw new JNIKiwixException("Cannot open zimfile "+filename); } } + + public JNIKiwixReader(FileDescriptor fd) throws JNIKiwixException + { + nativeHandle = getNativeReaderByFD(fd); + if (nativeHandle == 0) { + throw new JNIKiwixException("Cannot open zimfile by fd "+fd.toString()); + } + } + public JNIKiwixReader() { } public native void dispose(); private native long getNativeReader(String filename); + private native long getNativeReaderByFD(FileDescriptor fd); private long nativeHandle; } diff --git a/src/wrapper/java/org/kiwix/testing/test.java b/src/wrapper/java/org/kiwix/testing/test.java index cc0879f..c0eb7f0 100644 --- a/src/wrapper/java/org/kiwix/testing/test.java +++ b/src/wrapper/java/org/kiwix/testing/test.java @@ -41,6 +41,24 @@ throws JNIKiwixException, IOException } +@Test +public void testReaderByFd() +throws JNIKiwixException, IOException +{ + FileInputStream fis = new FileInputStream("small.zim"); + JNIKiwixReader reader = new JNIKiwixReader(fis.getFD()); + assertEquals("Test ZIM file", reader.getTitle()); + assertEquals(45, reader.getFileSize()); // The file size is in KiB + assertEquals("A/main.html", reader.getMainPage()); + String s = getFileContent("small_zimfile_data/main.html"); + byte[] c = reader.getContent(new JNIKiwixString("A/main.html"), + new JNIKiwixString(), + new JNIKiwixString(), + new JNIKiwixInt()); + assertEquals(s, new String(c)); + +} + @Test public void testLibrary() throws IOException