mirror of
https://github.com/kiwix/java-libkiwix.git
synced 2025-09-09 07:08:49 -04:00
Allow zim::Archive
to be created with a set of File descriptor.
This commit is contained in:
parent
9c525840ab
commit
8d684cee8e
@ -354,7 +354,8 @@ String getLibzimFiles() {
|
||||
"${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearcher.java " +
|
||||
"${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearch.java " +
|
||||
"${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java " +
|
||||
"${projectDir}/src/main/java/org/kiwix/libzim/EntryNotFoundException.java"
|
||||
"${projectDir}/src/main/java/org/kiwix/libzim/EntryNotFoundException.java " +
|
||||
"${projectDir}/src/main/java/org/kiwix/libzim/FdInput.java"
|
||||
}
|
||||
|
||||
task buildLinuxBinding(type: Exec) {
|
||||
|
@ -62,6 +62,22 @@ int jni2fd(const jobject& fdObj, JNIEnv* env)
|
||||
return env->GetIntField(fdObj, field_fd);
|
||||
}
|
||||
|
||||
zim::FdInput jni2fdInput(const jobject& fdInputObj, JNIEnv* env)
|
||||
{
|
||||
jclass class_fdesc = env->FindClass("org/kiwix/FdInput");
|
||||
jfieldID field_id = env->GetFieldID(class_fdesc, "fd", "java/io/FileDescriptor");
|
||||
jobject fdObj = env->GetField(fdInputObj, field_id);
|
||||
int fd = jni2fd(fdObj, env);
|
||||
|
||||
field_id = env->GetFieldID(class_fdesc, "offset", "J");
|
||||
long offset = env->GetLongField(fdObj, field_id);
|
||||
|
||||
field_id = env->GetFieldID(class_fdesc, "size", "J");
|
||||
long size = env->GetLongField(fdObj, field_id);
|
||||
|
||||
return zim::FdInput(fd, offset, size);
|
||||
}
|
||||
|
||||
} // unnamed namespace
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD(
|
||||
@ -104,6 +120,55 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded(
|
||||
#endif
|
||||
} CATCH_EXCEPTION()
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedFd(
|
||||
JNIEnv* env, jobject thisObj, jobject fdObj) try
|
||||
{
|
||||
#ifndef _WIN32
|
||||
auto fdInput = jni2fdInput(fdObj, env);
|
||||
|
||||
LOG("Attempting to create reader with fd: %d", fdInput);
|
||||
try {
|
||||
auto archive = std::make_shared<zim::Archive>(fdInput);
|
||||
SET_PTR(archive);
|
||||
} catch (std::exception& e) {
|
||||
LOG("Error opening ZIM file");
|
||||
LOG("%s", e.what());
|
||||
}
|
||||
#else
|
||||
jclass exception = env->FindClass("java/lang/UnsupportedOperationException");
|
||||
env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows");
|
||||
#endif
|
||||
} CATCH_EXCEPTION()
|
||||
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedFds(
|
||||
JNIEnv* env, jobject thisObj, jobjectArray fdsObj) try
|
||||
{
|
||||
#ifndef _WIN32
|
||||
|
||||
jsize length = env->GetArrayLength(fdsObj);
|
||||
std::vector<zim::FdInput> v(length);
|
||||
|
||||
int i;
|
||||
for(i = 0; i<length; i++) {
|
||||
jobject fdObj = env->GetObjectArrayElement(fdsObj, i);
|
||||
auto fdInput = jni2fdInput(fdObj, env);
|
||||
v.push_pack(fdInput);
|
||||
}
|
||||
|
||||
try {
|
||||
auto archive = std::make_shared<zim::Archive>(v);
|
||||
SET_PTR(archive);
|
||||
} catch (std::exception& e) {
|
||||
LOG("Error opening ZIM file");
|
||||
LOG("%s", e.what());
|
||||
}
|
||||
#else
|
||||
jclass exception = env->FindClass("java/lang/UnsupportedOperationException");
|
||||
env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows");
|
||||
#endif
|
||||
} CATCH_EXCEPTION()
|
||||
|
||||
DISPOSE
|
||||
|
||||
GETTER(jstring, getFilename)
|
||||
|
@ -23,6 +23,7 @@ import org.kiwix.libzim.ZimFileFormatException;
|
||||
import org.kiwix.libzim.Entry;
|
||||
import org.kiwix.libzim.Item;
|
||||
import org.kiwix.libzim.EntryIterator;
|
||||
import org.kiwix.libzim.FdInput;
|
||||
import java.io.FileDescriptor;
|
||||
|
||||
public class Archive
|
||||
@ -44,6 +45,18 @@ public class Archive
|
||||
setNativeArchiveEmbedded(fd, offset, size);
|
||||
}
|
||||
|
||||
public Archive(FdInput fd)
|
||||
throws ZimFileFormatException
|
||||
{
|
||||
setNativeArchiveEmbeddedFd(fd);
|
||||
}
|
||||
|
||||
public Archive(FdInput[] fds)
|
||||
throws ZimFileFormatException
|
||||
{
|
||||
setNativeArchiveEmbeddedFds(fds);
|
||||
}
|
||||
|
||||
public native String getFilename();
|
||||
public native long getFilesize();
|
||||
public native int getAllEntryCount();
|
||||
@ -94,6 +107,8 @@ public class Archive
|
||||
private native void setNativeArchive(String filename);
|
||||
private native void setNativeArchiveByFD(FileDescriptor fd);
|
||||
private native void setNativeArchiveEmbedded(FileDescriptor fd, long offset, long size);
|
||||
private native void setNativeArchiveEmbeddedFd(FdInput fd);
|
||||
private native void setNativeArchiveEmbeddedFds(FdInput[] fds);
|
||||
|
||||
@Override
|
||||
protected void finalize() { dispose(); }
|
||||
|
29
lib/src/main/java/org/kiwix/libzim/FdInput.java
Normal file
29
lib/src/main/java/org/kiwix/libzim/FdInput.java
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package org.kiwix.libzim;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
|
||||
public class FdInput
|
||||
{
|
||||
public FileDescriptor fd;
|
||||
public long offset;
|
||||
public long size;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user