From ebe5fc182c64c084c986f8ffb3fabd5e18f7cde7 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Sat, 19 Apr 2014 01:18:38 +0100 Subject: [PATCH] do not throw exceptions from native code: this does not work on some devices --- jni/NativeFormats/JavaNativeFormatPlugin.cpp | 7 +---- jni/NativeFormats/util/AndroidUtil.cpp | 28 ------------------- jni/NativeFormats/util/AndroidUtil.h | 6 ---- .../fbreader/formats/NativeFormatPlugin.java | 18 ++++++++---- .../model/CachedCharStorageException.java | 2 +- 5 files changed, 14 insertions(+), 47 deletions(-) diff --git a/jni/NativeFormats/JavaNativeFormatPlugin.cpp b/jni/NativeFormats/JavaNativeFormatPlugin.cpp index 4df4eb7d9..cd30a2345 100644 --- a/jni/NativeFormats/JavaNativeFormatPlugin.cpp +++ b/jni/NativeFormats/JavaNativeFormatPlugin.cpp @@ -32,11 +32,7 @@ static shared_ptr findCppPlugin(jobject base) { const std::string fileType = AndroidUtil::Method_NativeFormatPlugin_supportedFileType->callForCppString(base); - shared_ptr plugin = PluginCollection::Instance().pluginByType(fileType); - if (plugin.isNull()) { - AndroidUtil::throwRuntimeException("Native FormatPlugin instance is NULL for type " + fileType); - } - return plugin; + return PluginCollection::Instance().pluginByType(fileType); } static void fillUids(JNIEnv* env, jobject javaBook, Book &book) { @@ -287,7 +283,6 @@ JNIEXPORT jint JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin return 2; } if (!model->flush()) { - AndroidUtil::throwCachedCharStorageException("Cannot write file from native code"); return 3; } diff --git a/jni/NativeFormats/util/AndroidUtil.cpp b/jni/NativeFormats/util/AndroidUtil.cpp index 1f71c70ee..fe66837db 100644 --- a/jni/NativeFormats/util/AndroidUtil.cpp +++ b/jni/NativeFormats/util/AndroidUtil.cpp @@ -128,8 +128,6 @@ shared_ptr AndroidUtil::Method_NativeBookModel_addImage; shared_ptr AndroidUtil::Method_NativeBookModel_registerFontFamilyList; shared_ptr AndroidUtil::Method_NativeBookModel_registerFontEntry; -//shared_ptr AndroidUtil::StaticMethod_BookReadingException_throwForFile; - JNIEnv *AndroidUtil::getEnv() { JNIEnv *env; ourJavaVM->GetEnv((void **)&env, JNI_VERSION_1_2); @@ -212,11 +210,6 @@ bool AndroidUtil::init(JavaVM* jvm) { Method_NativeBookModel_registerFontFamilyList = new VoidMethod(Class_NativeBookModel, "registerFontFamilyList", "([Ljava/lang/String;)"); Method_NativeBookModel_registerFontEntry = new VoidMethod(Class_NativeBookModel, "registerFontEntry", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/fonts/FileInfo;Lorg/geometerplus/zlibrary/core/fonts/FileInfo;Lorg/geometerplus/zlibrary/core/fonts/FileInfo;Lorg/geometerplus/zlibrary/core/fonts/FileInfo;)"); -/* - Class_BookReadingException = new JavaClass(env, "org/geometerplus/fbreader/bookmodel/BookReadingException"); - StaticMethod_BookReadingException_throwForFile = new StaticVoidMethod(Class_BookReadingException, "throwForFile", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/filesystem/ZLFile;)V") ); -*/ - return true; } @@ -330,24 +323,3 @@ jbyteArray AndroidUtil::createJavaByteArray(JNIEnv *env, const std::vectorSetByteArrayRegion(array, 0, size, &data.front()); return array; } - -void AndroidUtil::throwRuntimeException(const std::string &message) { - getEnv()->ThrowNew(Class_java_lang_RuntimeException.j(), message.c_str()); -} - -void AndroidUtil::throwCachedCharStorageException(const std::string &message) { - getEnv()->ThrowNew(Class_CachedCharStorageException.j(), message.c_str()); -} - -/* -void AndroidUtil::throwBookReadingException(const std::string &resourceId, const ZLFile &file) { - JNIEnv *env = getEnv(); - env->CallStaticVoidMethod( - StaticMethod_BookReadingException_throwForFile, - AndroidUtil::createJavaString(env, resourceId), - AndroidUtil::createJavaFile(env, file.path()) - ); - // TODO: possible memory leak - // TODO: clear ZLFile object reference -} -*/ diff --git a/jni/NativeFormats/util/AndroidUtil.h b/jni/NativeFormats/util/AndroidUtil.h index 737905a6d..ff7cb50cb 100644 --- a/jni/NativeFormats/util/AndroidUtil.h +++ b/jni/NativeFormats/util/AndroidUtil.h @@ -166,8 +166,6 @@ public: static shared_ptr Method_NativeBookModel_registerFontFamilyList; static shared_ptr Method_NativeBookModel_registerFontEntry; - //static shared_ptr StaticMethod_BookReadingException_throwForFile; - public: static bool init(JavaVM* jvm); static JNIEnv *getEnv(); @@ -182,10 +180,6 @@ public: static jintArray createJavaIntArray(JNIEnv *env, const std::vector &data); static jbyteArray createJavaByteArray(JNIEnv *env, const std::vector &data); - - static void throwRuntimeException(const std::string &message); - static void throwCachedCharStorageException(const std::string &message); - //static void throwBookReadingException(const std::string &resourceId, const ZLFile &file); }; inline jstring JString::j() { return myJ; } diff --git a/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java b/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java index 375d543ba..4a39111f0 100644 --- a/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java +++ b/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java @@ -26,6 +26,7 @@ import org.geometerplus.zlibrary.core.encodings.EncodingCollection; import org.geometerplus.zlibrary.core.encodings.JavaEncodingCollection; import org.geometerplus.zlibrary.core.filesystem.ZLFile; import org.geometerplus.zlibrary.core.image.*; +import org.geometerplus.zlibrary.text.model.CachedCharStorageException; import org.geometerplus.fbreader.book.Book; import org.geometerplus.fbreader.book.BookUtil; @@ -93,12 +94,17 @@ public class NativeFormatPlugin extends FormatPlugin { @Override synchronized public void readModel(BookModel model) throws BookReadingException { final int code = readModelNative(model); - if (code != 0) { - throw new BookReadingException( - "nativeCodeFailure", - model.Book.File, - new String[] { String.valueOf(code), model.Book.File.getPath() } - ); + switch (code) { + case 0: + return; + case 3: + throw new CachedCharStorageException("Cannot write file from native code"); + default: + throw new BookReadingException( + "nativeCodeFailure", + model.Book.File, + new String[] { String.valueOf(code), model.Book.File.getPath() } + ); } } diff --git a/src/org/geometerplus/zlibrary/text/model/CachedCharStorageException.java b/src/org/geometerplus/zlibrary/text/model/CachedCharStorageException.java index 42cd43fb6..bc4f8b094 100644 --- a/src/org/geometerplus/zlibrary/text/model/CachedCharStorageException.java +++ b/src/org/geometerplus/zlibrary/text/model/CachedCharStorageException.java @@ -19,7 +19,7 @@ package org.geometerplus.zlibrary.text.model; -final class CachedCharStorageException extends RuntimeException { +public final class CachedCharStorageException extends RuntimeException { private static final long serialVersionUID = -6373408730045821053L; public CachedCharStorageException(String message) {