diff --git a/src/main/jni/src/mainJNILib.cpp b/src/main/jni/src/mainJNILib.cpp index a888e69f..0067e4b2 100644 --- a/src/main/jni/src/mainJNILib.cpp +++ b/src/main/jni/src/mainJNILib.cpp @@ -55,6 +55,9 @@ class DocumentFile { FPDF_DOCUMENT pdfDocument = NULL; size_t fileSize; + public: + jbyte *cDataCopy = NULL; + DocumentFile() { initLibraryIfNeed(); } ~DocumentFile(); }; @@ -62,7 +65,10 @@ DocumentFile::~DocumentFile(){ if(pdfDocument != NULL){ FPDF_CloseDocument(pdfDocument); } - + if(cDataCopy != NULL){ + free(cDataCopy); + cDataCopy = NULL; + } destroyLibraryIfNeed(); } @@ -238,13 +244,11 @@ JNI_FUNC(jlong, PdfiumCore, nativeOpenMemDocument)(JNI_ARGS, jbyteArray data, js cpassword = env->GetStringUTFChars(password, NULL); } - jbyte *cData = env->GetByteArrayElements(data, NULL); int size = (int) env->GetArrayLength(data); jbyte *cDataCopy = new jbyte[size]; - memcpy(cDataCopy, cData, size); + env->GetByteArrayRegion(data, 0, size, cDataCopy); FPDF_DOCUMENT document = FPDF_LoadMemDocument( reinterpret_cast(cDataCopy), size, cpassword); - env->ReleaseByteArrayElements(data, cData, JNI_ABORT); if(cpassword != NULL) { env->ReleaseStringUTFChars(password, cpassword); @@ -269,7 +273,7 @@ JNI_FUNC(jlong, PdfiumCore, nativeOpenMemDocument)(JNI_ARGS, jbyteArray data, js } docFile->pdfDocument = document; - + docFile->cDataCopy = cDataCopy; return reinterpret_cast(docFile); }