@@ -717,34 +717,41 @@ JNIEXPORT jboolean JNICALL Java_org_coolreader_crengine_Engine_checkFontLanguage
717717JNIEXPORT jobjectArray JNICALL Java_org_coolreader_crengine_Engine_listFilesInternal
718718 (JNIEnv *env, jclass, jobject jdir)
719719{
720- jclass fileClass = env->FindClass (" java/io/File" );
721- if (NULL == fileClass)
720+ if (NULL == jdir)
722721 return NULL ;
723- jmethodID mFileGetAbsolutePath = env->GetMethodID (fileClass, " getAbsolutePath " , " ()Ljava/lang/String; " );
724- if (NULL == mFileGetAbsolutePath )
722+ jclass pjcFile = env->FindClass ( " java/io/File " );
723+ if (NULL == pjcFile )
725724 return NULL ;
726- jmethodID fileCtor = env->GetMethodID (fileClass , " <init> " , " (Ljava/lang/String;)V " );
727- if (NULL == fileCtor )
725+ jmethodID pjmFile_GetAbsolutePath = env->GetMethodID (pjcFile , " getAbsolutePath " , " () Ljava/lang/String;" );
726+ if (NULL == pjmFile_GetAbsolutePath )
728727 return NULL ;
729- jstring jpathname = (jstring)env->CallObjectMethod (jdir, mFileGetAbsolutePath );
728+ jmethodID pjmFile_Ctor = env->GetMethodID (pjcFile, " <init>" , " (Ljava/lang/String;)V" );
729+ if (NULL == pjmFile_Ctor)
730+ return NULL ;
731+ jstring jpathname = (jstring)env->CallObjectMethod (jdir, pjmFile_GetAbsolutePath);
730732 jboolean iscopy;
731733 const char * s = env->GetStringUTFChars (jpathname, &iscopy);
732734 lString16 path = (CRJNIEnv::sdk_int >= ANDROID_SDK_M) ? Utf8ToUnicode (s) : Wtf8ToUnicode (s);
733- LVContainerRef dir = LVOpenDirectory (path );
735+ env-> ReleaseStringUTFChars (jpathname, s );
734736 jobjectArray jarray = NULL ;
737+ LVContainerRef dir = LVOpenDirectory (path);
735738 if ( !dir.isNull () ) {
736739 jstring emptyString = env->NewStringUTF (" " );
737- jobject emptyFile = env->NewObject (fileClass, fileCtor, emptyString);
738- jarray = env->NewObjectArray (dir->GetObjectCount (), fileClass, emptyFile);
739- for (int i = 0 ; i < dir->GetObjectCount (); i++) {
740- const LVContainerItemInfo *item = dir->GetObjectInfo (i);
741- lString16 fileName = path + " /" + item->GetName ();
742- jstring jfilename = env->NewStringUTF ((CRJNIEnv::sdk_int >= ANDROID_SDK_M) ? UnicodeToUtf8 (fileName).c_str () : UnicodeToWtf8 (fileName).c_str ());
743- jobject jfile = env->NewObject (fileClass, fileCtor, jfilename);
744- env->SetObjectArrayElement (jarray, i, jfile);
740+ jobject emptyFile = env->NewObject (pjcFile, pjmFile_Ctor, emptyString);
741+ jarray = env->NewObjectArray (dir->GetObjectCount (), pjcFile, emptyFile);
742+ if (NULL != jarray) {
743+ for (int i = 0 ; i < dir->GetObjectCount (); i++) {
744+ const LVContainerItemInfo *item = dir->GetObjectInfo (i);
745+ lString16 fileName = path + " /" + item->GetName ();
746+ jstring jfilename = env->NewStringUTF (
747+ (CRJNIEnv::sdk_int >= ANDROID_SDK_M) ? UnicodeToUtf8 (fileName).c_str ()
748+ : UnicodeToWtf8 (fileName).c_str ());
749+ jobject jfile = env->NewObject (pjcFile, pjmFile_Ctor, jfilename);
750+ if (NULL != jfile)
751+ env->SetObjectArrayElement (jarray, i, jfile);
752+ }
745753 }
746754 }
747- env->ReleaseStringUTFChars (jpathname, s);
748755 return jarray;
749756}
750757
0 commit comments