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