From 6adf95c32910b71af750240e8a215948b2a9ac2c Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Thu, 1 Feb 2018 17:18:54 +0100 Subject: [PATCH] Make JNIKiwixReader throw an exception if something goes wrong at creation. If the `nativeHandle` is null, the JNIKiwixReader is invalid and we must not use it. Throwing an exception for the caller code to handle this properly. And previously, user code has no way to detect something went wrong :/ --- src/android/meson.build | 1 + .../org/kiwix/kiwixlib/JNIKiwixException.java | 27 +++++++++++++++++++ .../org/kiwix/kiwixlib/JNIKiwixReader.java | 6 ++++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/android/org/kiwix/kiwixlib/JNIKiwixException.java diff --git a/src/android/meson.build b/src/android/meson.build index 39ebe198a..e79bf839f 100644 --- a/src/android/meson.build +++ b/src/android/meson.build @@ -8,6 +8,7 @@ kiwix_jni = custom_target('jni', 'org/kiwix/kiwixlib/JNIKiwixInt.java', 'org/kiwix/kiwixlib/JNIKiwixString.java', 'org/kiwix/kiwixlib/JNIKiwixBool.java', + 'org/kiwix/kiwixlib/JNIKiwixException.java', 'org/kiwix/kiwixlib/Pair.java'], output: ['org_kiwix_kiwixlib_JNIKiwix.h', 'org_kiwix_kiwixlib_JNIKiwixReader.h', diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixException.java b/src/android/org/kiwix/kiwixlib/JNIKiwixException.java new file mode 100644 index 000000000..dd0e2143c --- /dev/null +++ b/src/android/org/kiwix/kiwixlib/JNIKiwixException.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2017 Matthieu Gautier + * + * 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.kiwixlib; + +public class JNIKiwixException extends Exception +{ + public JNIKiwixException(String message) { + super(message); + } +} diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java b/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java index e204f9aba..e405ef0f4 100644 --- a/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java +++ b/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java @@ -20,6 +20,7 @@ package org.kiwix.kiwixlib; +import org.kiwix.kiwixlib.JNIKiwixException; import org.kiwix.kiwixlib.JNIKiwixString; import org.kiwix.kiwixlib.JNIKiwixInt; import org.kiwix.kiwixlib.JNIKiwixSearcher; @@ -109,9 +110,12 @@ public class JNIKiwixReader return searcher; } - public JNIKiwixReader(String filename) + public JNIKiwixReader(String filename) throws JNIKiwixException { nativeHandle = getNativeReader(filename); + if (nativeHandle == 0) { + throw new JNIKiwixException("Cannot open zimfile "+filename); + } } public JNIKiwixReader() {