Repository navigation
Expand file tree
/
Copy pathdClassLoader.java
More file actions
122 lines (98 loc) · 2.53 KB
/
Copy pathdClassLoader.java
File metadata and controls
122 lines (98 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package dclass;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
public class dClassLoader
{
public static boolean load(String lib)
{
if(sLoadLib(lib))
return true;
String os=detectOS();
if(!os.isEmpty() && loadLibResource("/lib/",lib,os))
return true;
System.loadLibrary(lib);
return false;
}
private static boolean sLoadLib(String lib)
{
try
{
System.loadLibrary(lib);
}
catch (UnsatisfiedLinkError ex)
{
return false;
}
return true;
}
private static boolean sLoad(String lib)
{
try
{
System.load(lib);
}
catch (UnsatisfiedLinkError ex)
{
return false;
}
return true;
}
private static boolean loadLibResource(String path,String lib,String os)
{
InputStream is=null;
OutputStream fos=null;
File f=null;
//TODO fix this try catch
try
{
is=dClassLoader.class.getResourceAsStream(path+lib+os);
if(is==null)
return false;
f=File.createTempFile(lib,null);
f.deleteOnExit();
if(!f.exists())
return false;
fos=new FileOutputStream(f);
byte[] buffer=new byte[10000];
int read;
while((read=is.read(buffer))!=-1)
{
fos.write(buffer,0,read);
}
}
catch(Exception ex)
{
return false;
}
finally
{
try
{
is.close();
fos.close();
}
catch(Exception ex) {}
}
return sLoad(f.getAbsolutePath());
}
private static String detectOS()
{
String os=System.getProperty("os.name").toLowerCase();
String arch=System.getProperty("os.arch").toLowerCase();
if(arch.equals("amd64"))
arch="x86_64";
else if(arch.equals("x86") || arch.equals("i686") || arch.equals("i386"))
arch="x86_32";
else if(arch.startsWith("armv7"))
arch="armv7";
if(os.contains("windows"))
return "_win_"+arch+".dll";
else if(os.contains("linux"))
return "_linux_"+arch+".so";
else if(os.contains("mac os"))
return "_osx_"+arch+".dylib";
return "";
}
}