mirror of https://github.com/kiwix/libkiwix.git
JNIKiwixReader can open an embedded ZIM archive
This commit is contained in:
parent
4d23e44de7
commit
9cdf7a44c0
|
@ -57,6 +57,7 @@ class Reader
|
||||||
*/
|
*/
|
||||||
explicit Reader(const string zimFilePath);
|
explicit Reader(const string zimFilePath);
|
||||||
explicit Reader(int fd);
|
explicit Reader(int fd);
|
||||||
|
Reader(int fd, zim::offset_type offset, zim::size_type size);
|
||||||
~Reader() = default;
|
~Reader() = default;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -90,7 +90,14 @@ Reader::Reader(int fd)
|
||||||
: zimArchive(new zim::Archive(fd)),
|
: zimArchive(new zim::Archive(fd)),
|
||||||
zimFilePath("")
|
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: */
|
/* initialize random seed: */
|
||||||
srand(time(nullptr));
|
srand(time(nullptr));
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<jlong>(new Handle<kiwix::Reader>(reader));
|
||||||
|
} catch (std::exception& e) {
|
||||||
|
LOG("Error opening ZIM file");
|
||||||
|
LOG(e.what());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL
|
JNIEXPORT void JNICALL
|
||||||
Java_org_kiwix_kiwixlib_JNIKiwixReader_dispose(JNIEnv* env, jobject obj)
|
Java_org_kiwix_kiwixlib_JNIKiwixReader_dispose(JNIEnv* env, jobject obj)
|
||||||
{
|
{
|
||||||
|
|
|
@ -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() {
|
public JNIKiwixReader() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -168,5 +177,6 @@ public class JNIKiwixReader
|
||||||
|
|
||||||
private native long getNativeReader(String filename);
|
private native long getNativeReader(String filename);
|
||||||
private native long getNativeReaderByFD(FileDescriptor fd);
|
private native long getNativeReaderByFD(FileDescriptor fd);
|
||||||
|
private native long getNativeReaderEmbedded(FileDescriptor fd, long offset, long size);
|
||||||
private long nativeHandle;
|
private long nativeHandle;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
@Test
|
||||||
public void testLibrary()
|
public void testLibrary()
|
||||||
throws IOException
|
throws IOException
|
||||||
|
|
Loading…
Reference in New Issue