diff --git a/include/reader.h b/include/reader.h index 263617c9d..cbeaaad2a 100644 --- a/include/reader.h +++ b/include/reader.h @@ -57,6 +57,7 @@ class Reader */ explicit Reader(const string zimFilePath); explicit Reader(int fd); + Reader(int fd, zim::offset_type offset, zim::size_type size); ~Reader() = default; /** diff --git a/src/reader.cpp b/src/reader.cpp index 51bdaf28d..0278b5033 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -90,7 +90,14 @@ Reader::Reader(int fd) : zimArchive(new zim::Archive(fd)), zimFilePath("") { + /* initialize random seed: */ + srand(time(nullptr)); +} +Reader::Reader(int fd, zim::offset_type offset, zim::size_type size) + : zimArchive(new zim::Archive(fd, offset, size)), + zimFilePath("") +{ /* initialize random seed: */ srand(time(nullptr)); } diff --git a/src/wrapper/java/kiwixreader.cpp b/src/wrapper/java/kiwixreader.cpp index e62eb3084..c3f06ecc5 100644 --- a/src/wrapper/java/kiwixreader.cpp +++ b/src/wrapper/java/kiwixreader.cpp @@ -74,6 +74,23 @@ JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReaderBy } } +JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReaderEmbedded( + JNIEnv* env, jobject obj, jobject fdObj, jlong offset, jlong size) +{ + int fd = jni2fd(fdObj, env); + + LOG("Attempting to create reader with fd: %d", fd); + Lock l; + try { + kiwix::Reader* reader = new kiwix::Reader(fd, offset, size); + 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 fe5e39e6c..94b7b39ff 100644 --- a/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixReader.java +++ b/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixReader.java @@ -161,6 +161,15 @@ public class JNIKiwixReader } } + public JNIKiwixReader(FileDescriptor fd, long offset, long size) + throws JNIKiwixException + { + nativeHandle = getNativeReaderEmbedded(fd, offset, size); + if (nativeHandle == 0) { + throw new JNIKiwixException(String.format("Cannot open embedded zimfile (fd=%s, offset=%d, size=%d)", fd, offset, size)); + } + } + public JNIKiwixReader() { } @@ -168,5 +177,6 @@ public class JNIKiwixReader private native long getNativeReader(String filename); private native long getNativeReaderByFD(FileDescriptor fd); + private native long getNativeReaderEmbedded(FileDescriptor fd, long offset, long size); private long nativeHandle; } diff --git a/src/wrapper/java/org/kiwix/testing/test.java b/src/wrapper/java/org/kiwix/testing/test.java index c0eb7f044..594a5e0bf 100644 --- a/src/wrapper/java/org/kiwix/testing/test.java +++ b/src/wrapper/java/org/kiwix/testing/test.java @@ -59,6 +59,25 @@ throws JNIKiwixException, IOException } +@Test +public void testReaderWithAnEmbeddedArchive() +throws JNIKiwixException, IOException +{ + File plainArchive = new File("small.zim"); + FileInputStream fis = new FileInputStream("small.zim.embedded"); + JNIKiwixReader reader = new JNIKiwixReader(fis.getFD(), 8, plainArchive.length()); + 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