Skip to content

Commit

Permalink
Allow zim::Archive to be created with a set of File descriptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored and kelson42 committed Apr 20, 2024
1 parent 9c52584 commit c729665
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
65 changes: 65 additions & 0 deletions lib/src/main/cpp/libzim/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions lib/src/main/java/org/kiwix/libzim/Archive.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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
Expand All @@ -44,6 +45,18 @@ public Archive(FileDescriptor fd, long offset, long size)
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();
Expand Down Expand Up @@ -94,6 +107,8 @@ public Archive(FileDescriptor fd, long offset, long size)
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(); }
Expand Down
29 changes: 29 additions & 0 deletions lib/src/main/java/org/kiwix/libzim/FdInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2017 Matthieu Gautier <[email protected]>
*
* 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;
}

0 comments on commit c729665

Please sign in to comment.