Skip to content

Commit 494c7de

Browse files
[NoForkingMode] Fix again...
Don't give two methods the same name. Extern "C" the jni method. Synchronize the calls to give some thread safety
1 parent f76333c commit 494c7de

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

pdf2htmlEX/src/main/cpp/pdf2htmlEX.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_set_1environment_1value(JN
8484
}
8585

8686
static bool forkBeforeConverting = true;
87+
88+
extern "C"
8789
JNIEXPORT void JNICALL
88-
Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_setNoForking(JNIEnv *, jobject) {
90+
Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_set_1no_1forking(JNIEnv *, jclass) {
8991
forkBeforeConverting = false;
9092
}
9193

@@ -164,4 +166,3 @@ Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_call_1pdf2htmlEX(JNIEnv *e
164166

165167
return retVal;
166168
}
167-

pdf2htmlEX/src/main/java/com/viliussutkus89/android/pdf2htmlex/pdf2htmlEX.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public final class pdf2htmlEX {
4343
private String p_ownerPassword = "";
4444
private String p_userPassword = "";
4545

46-
private boolean m_isNoForking = false;
46+
private final static Object m_forkingSynchro = new Object();
47+
private static boolean m_isNoForking = false;
4748
private static boolean m_noForkingConversionAlreadyDone = false;
4849

4950
public static class ConversionFailedException extends Exception {
@@ -113,20 +114,17 @@ public pdf2htmlEX setUserPassword(@NonNull String userPassword) {
113114
return this;
114115
}
115116

116-
public pdf2htmlEX setNoForking(boolean iDoUnderstandThatIWillHaveToRestartTheAppBeforeICanRunConversionForTheSecondTime) throws IllegalArgumentException {
117+
public void setNoForking(boolean iDoUnderstandThatIWillHaveToRestartTheAppBeforeICanRunConversionForTheSecondTime) throws IllegalArgumentException {
117118
if (!iDoUnderstandThatIWillHaveToRestartTheAppBeforeICanRunConversionForTheSecondTime) {
118119
throw new IllegalArgumentException();
119120
}
120-
this.m_isNoForking = true;
121-
setNoForking();
122-
return this;
121+
synchronized (m_forkingSynchro) {
122+
m_isNoForking = true;
123+
}
124+
set_no_forking();
123125
}
124126

125127
public File convert() throws IOException, ConversionFailedException {
126-
if (this.m_noForkingConversionAlreadyDone) {
127-
throw new ConversionFailedException("No forking mode allows only one conversion!");
128-
}
129-
130128
if (null == this.p_inputPDF) {
131129
throw new ConversionFailedException("No Input PDF given!");
132130
}
@@ -135,6 +133,12 @@ public File convert() throws IOException, ConversionFailedException {
135133
throw new ConversionFailedException("Input PDF does not exist!");
136134
}
137135

136+
synchronized (m_forkingSynchro) {
137+
if (m_noForkingConversionAlreadyDone) {
138+
throw new ConversionFailedException("No forking mode allows only one conversion!");
139+
}
140+
m_noForkingConversionAlreadyDone = m_isNoForking;
141+
}
138142
String inputFilenameNoPDFExt = this.p_inputPDF.getName();
139143
if (inputFilenameNoPDFExt.endsWith(".pdf")) {
140144
inputFilenameNoPDFExt = inputFilenameNoPDFExt.substring(0, inputFilenameNoPDFExt.length() - 4);
@@ -149,11 +153,7 @@ public File convert() throws IOException, ConversionFailedException {
149153
m_poppler_dataDir.getAbsolutePath(), m_pdf2htmlEX_tmpDir.getAbsolutePath(),
150154
this.p_inputPDF.getAbsolutePath(), outputHtml.getAbsolutePath(),
151155
this.p_ownerPassword, this.p_userPassword
152-
);
153-
154-
if (this.m_isNoForking) {
155-
this.m_noForkingConversionAlreadyDone = true;
156-
}
156+
);
157157

158158
if (0 != retVal) {
159159
outputHtml.delete();
@@ -168,5 +168,5 @@ public File convert() throws IOException, ConversionFailedException {
168168
// Because Java cannot setenv for the current process
169169
static native void set_environment_value(String key, String value);
170170

171-
private native void setNoForking();
171+
private native static void set_no_forking();
172172
}

0 commit comments

Comments
 (0)