1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 09:49:19 +02:00

do not throw exceptions from native code: this does not work on some devices

This commit is contained in:
Nikolay Pultsin 2014-04-19 01:18:38 +01:00
parent 64de74460d
commit ebe5fc182c
5 changed files with 14 additions and 47 deletions

View file

@ -32,11 +32,7 @@
static shared_ptr<FormatPlugin> findCppPlugin(jobject base) {
const std::string fileType = AndroidUtil::Method_NativeFormatPlugin_supportedFileType->callForCppString(base);
shared_ptr<FormatPlugin> 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;
}

View file

@ -128,8 +128,6 @@ shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_addImage;
shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_registerFontFamilyList;
shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_registerFontEntry;
//shared_ptr<StaticObjectMethod> 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::vector<jbyte
env->SetByteArrayRegion(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
}
*/

View file

@ -166,8 +166,6 @@ public:
static shared_ptr<VoidMethod> Method_NativeBookModel_registerFontFamilyList;
static shared_ptr<VoidMethod> Method_NativeBookModel_registerFontEntry;
//static shared_ptr<StaticObjectMethod> 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<jint> &data);
static jbyteArray createJavaByteArray(JNIEnv *env, const std::vector<jbyte> &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; }

View file

@ -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() }
);
}
}

View file

@ -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) {