mirror of https://github.com/kiwix/libkiwix.git
JNIKiwixReader ctor taking a file descriptor
... and a corresponding unit test
This commit is contained in:
parent
98d69ef59b
commit
4d23e44de7
|
@ -55,7 +55,8 @@ class Reader
|
|||
* unsplitted path as if the file were not splitted
|
||||
* (.zim extesion).
|
||||
*/
|
||||
Reader(const string zimFilePath);
|
||||
explicit Reader(const string zimFilePath);
|
||||
explicit Reader(int fd);
|
||||
~Reader() = default;
|
||||
|
||||
/**
|
||||
|
|
|
@ -86,6 +86,15 @@ Reader::Reader(const string zimFilePath)
|
|||
srand(time(nullptr));
|
||||
}
|
||||
|
||||
Reader::Reader(int fd)
|
||||
: zimArchive(new zim::Archive(fd)),
|
||||
zimFilePath("")
|
||||
{
|
||||
|
||||
/* initialize random seed: */
|
||||
srand(time(nullptr));
|
||||
}
|
||||
|
||||
zim::Archive* Reader::getZimArchive() const
|
||||
{
|
||||
return zimArchive.get();
|
||||
|
|
|
@ -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<jlong>(new Handle<kiwix::Reader>(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)
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue