Skip to content

Commit 6e3220b

Browse files
ksh8281clover2123
authored andcommitted
Prevent multiple escargot-jni so copying from multi thread or multi process
Signed-off-by: Seonghyun Kim <[email protected]>
1 parent c1d3b8c commit 6e3220b

File tree

1 file changed

+25
-28
lines changed
  • build/android/escargot/src/main/java/com/samsung/lwe/escargot

1 file changed

+25
-28
lines changed

build/android/escargot/src/main/java/com/samsung/lwe/escargot/Escargot.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,33 @@ public class Escargot {
1212
try {
1313
System.loadLibrary("escargot-jni");
1414
} catch(UnsatisfiedLinkError e) {
15-
// try to load from jar
16-
InputStream soStream = Escargot.class.getResourceAsStream("/libescargot-jni.so");
17-
if (soStream != null) {
18-
try {
19-
if (Files.size(Paths.get("/tmp/libescargot-jni.so")) == soStream.available()) {
20-
System.load("/tmp/libescargot-jni.so");
21-
} else {
22-
throw new Exception();
23-
}
24-
} catch(Exception e2) {
25-
try {
26-
File f = new File("/tmp/libescargot-jni.so");
27-
f.setExecutable(true);
28-
FileOutputStream fos = new FileOutputStream(f);
29-
int read;
30-
byte [] bytes = new byte[1024];
31-
while ((read = soStream.read(bytes)) != -1) {
32-
fos.write(bytes, 0, read);
33-
}
34-
fos.flush();
35-
} catch (IOException e1) {
36-
e1.printStackTrace();
37-
}
38-
System.load("/tmp/libescargot-jni.so");
15+
tryToLoadLibraryFromJarResource(e);
16+
}
17+
}
18+
static native public void init();
19+
static private synchronized void tryToLoadLibraryFromJarResource(UnsatisfiedLinkError e)
20+
{
21+
InputStream soStream = Escargot.class.getResourceAsStream("/libescargot-jni.so");
22+
if (soStream != null) {
23+
try {
24+
File f = File.createTempFile("libescargot-jni-", ".so");
25+
f.deleteOnExit();
26+
f.setExecutable(true);
27+
FileOutputStream fos = new FileOutputStream(f);
28+
int read;
29+
byte[] bytes = new byte[1024];
30+
while ((read = soStream.read(bytes)) != -1) {
31+
fos.write(bytes, 0, read);
3932
}
40-
} else {
41-
throw e;
33+
fos.flush();
34+
soStream.close();
35+
fos.close();
36+
System.load(f.getAbsolutePath());
37+
} catch (IOException e1) {
38+
e1.printStackTrace();
4239
}
40+
} else {
41+
throw e;
4342
}
4443
}
45-
46-
static native public void init();
4744
}

0 commit comments

Comments
 (0)