提交 | 用户 | 时间
|
149dd0
|
1 |
package com.iailab.netsdk.lib; |
H |
2 |
|
|
3 |
import com.iailab.netsdk.lib.DynamicParseUtil; |
|
4 |
|
|
5 |
import java.io.*; |
|
6 |
|
|
7 |
/** |
|
8 |
* @author 47081 |
|
9 |
* @version 1.0 |
|
10 |
* @description 动态库加载 |
|
11 |
* @date 2020/11/14 |
|
12 |
*/ |
|
13 |
public class LibraryLoad { |
|
14 |
private static final String ARCH_WINDOWS = "win"; |
|
15 |
private static final String ARCH_LINUX = "linux"; |
|
16 |
private static final String ARCH_MAC = "mac"; |
|
17 |
private static final int PREFIX_64 = 64; |
|
18 |
private static final int PREFIX_32 = 32; |
|
19 |
private static final String PREFIX_ARM = "ARM"; |
|
20 |
private static final String EXTERNAL_WIN = ".dll"; |
|
21 |
private static final String EXTERNAL_LINUX = ".so"; |
|
22 |
private static final String EXTERNAL_MAC = ".dylib"; |
|
23 |
private static DynamicParseUtil dynamicParseUtil; |
|
24 |
/** 当前读取的目录 */ |
|
25 |
private static String currentFold; |
|
26 |
/** 动态库需要写入的目录 */ |
|
27 |
private static String EXTRACT_PATH = System.getProperty("java.io.tmpdir"); |
|
28 |
|
|
29 |
private static boolean written = false; |
|
30 |
|
|
31 |
/** |
|
32 |
* 设置动态库写入的路径,适用于需要自定义加载路径的用户 |
|
33 |
* |
|
34 |
* @param path 动态库写入的文件夹,从该文件夹下加载sdk的动态库 |
|
35 |
*/ |
|
36 |
public static void setExtractPath(String path) { |
|
37 |
EXTRACT_PATH = path; |
|
38 |
} |
|
39 |
|
|
40 |
public static String getExtractPath() { |
|
41 |
return EXTRACT_PATH; |
|
42 |
|
|
43 |
} |
|
44 |
/** 动态库路径 */ |
|
45 |
private static String INNER_PATH; |
|
46 |
|
|
47 |
// private static final String EXTERNAL_MAC = ".so"; |
|
48 |
|
|
49 |
private static String extractNetSDKLib(String libName) { |
|
50 |
return extractLibrary(libName); |
|
51 |
} |
|
52 |
|
|
53 |
public static String getLoadLibrary(String libraryName) { |
|
54 |
currentFold = getLibraryFold(); |
|
55 |
if (dynamicParseUtil == null) { |
|
56 |
try { |
|
57 |
dynamicParseUtil = |
|
58 |
new DynamicParseUtil( |
|
59 |
LibraryLoad.class.getClassLoader().getResourceAsStream("dynamic-lib-load.xml")); |
|
60 |
if (!written) { |
|
61 |
for (String libName : dynamicParseUtil.getLibsSystem(currentFold)) { |
|
62 |
extractLibrary(libName); |
|
63 |
} |
|
64 |
written = true; |
|
65 |
} |
|
66 |
} catch (Exception e) { |
|
67 |
e.printStackTrace(); |
|
68 |
} |
|
69 |
} |
|
70 |
String fullName = getLibraryName(libraryName); |
|
71 |
String path = EXTRACT_PATH; |
|
72 |
if (!(EXTRACT_PATH.endsWith("/") || EXTRACT_PATH.endsWith("\\"))) { |
|
73 |
path = EXTRACT_PATH + "/"; |
|
74 |
} |
|
75 |
System.out.println("load library: " + path + fullName); |
|
76 |
return path + fullName; |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* 将jar包里的动态库写入到系统缓存目录,使用绝对路径加载动态库 |
|
81 |
* |
|
82 |
* @param libName |
|
83 |
* @return |
|
84 |
*/ |
|
85 |
private static String extractLibrary(String libName) { |
|
86 |
return extractLibrary("", libName); |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* 相对路径文件夹 |
|
91 |
* |
|
92 |
* @param relativePath 相对路径 |
|
93 |
* @param libName 动态库路径 |
|
94 |
* @return |
|
95 |
*/ |
|
96 |
private static String extractLibrary(String relativePath, String libName) { |
|
97 |
if (libName.trim().equals("")) { |
|
98 |
return ""; |
|
99 |
} |
|
100 |
String libFullName = getLibraryName(libName); |
|
101 |
String dir = getLibraryFold(); |
|
102 |
if (!(relativePath.endsWith("/") || relativePath.endsWith("\\"))) { |
|
103 |
relativePath = relativePath + "/"; |
|
104 |
} |
|
105 |
String fileName = relativePath + dir + "/" + libFullName; |
|
106 |
InputStream in = LibraryLoad.class.getResourceAsStream(fileName); |
|
107 |
BufferedInputStream reader; |
|
108 |
FileOutputStream writer; |
|
109 |
File extractedLibFile = null; |
|
110 |
try { |
|
111 |
if (in == null) { |
|
112 |
in = new FileInputStream(fileName); |
|
113 |
} |
|
114 |
String nativeTempDir = EXTRACT_PATH; |
|
115 |
if (!(nativeTempDir.endsWith("/") || nativeTempDir.endsWith("\\"))) { |
|
116 |
nativeTempDir = nativeTempDir + "/"; |
|
117 |
} |
|
118 |
extractedLibFile = new File(nativeTempDir + libFullName); |
|
119 |
reader = new BufferedInputStream(in); |
|
120 |
writer = new FileOutputStream(extractedLibFile); |
|
121 |
byte[] buffer = new byte[1024]; |
|
122 |
while (true) { |
|
123 |
int len = reader.read(buffer); |
|
124 |
if (len == 0 || len == -1) break; |
|
125 |
writer.write(buffer, 0, len); |
|
126 |
} |
|
127 |
reader.close(); |
|
128 |
writer.close(); |
|
129 |
in.close(); |
|
130 |
} catch (Exception e) { |
|
131 |
//System.out.println("dynamic file[ "+ fileName+ " ] not found in project.please ensure you need this library."); |
|
132 |
} |
|
133 |
return extractedLibFile != null ? extractedLibFile.getAbsolutePath() : ""; |
|
134 |
} |
|
135 |
|
|
136 |
/** |
|
137 |
* 获取动态库完整名称 |
|
138 |
* |
|
139 |
* @param libName |
|
140 |
* @return |
|
141 |
*/ |
|
142 |
private static String getLibraryName(String libName) { |
|
143 |
String dir = currentFold; |
|
144 |
String libPrefix = ""; |
|
145 |
String libExtension = EXTERNAL_WIN; |
|
146 |
|
|
147 |
if (!dir.contains("win")) { |
|
148 |
libPrefix = "lib"; |
|
149 |
if (dir.contains("linux")) { |
|
150 |
libExtension = EXTERNAL_LINUX; |
|
151 |
} else { |
|
152 |
// libExtension=".dylib"; |
|
153 |
libExtension = EXTERNAL_MAC; |
|
154 |
} |
|
155 |
} |
|
156 |
libName = dynamicParseUtil.compareLibName(currentFold, libName); |
|
157 |
// 动态库以lib开头,则不添加lib前缀 |
|
158 |
// 以lib开头的库则不添加lib前缀 |
|
159 |
return (libName.startsWith("lib") ? "" : libPrefix) + libName + libExtension; |
|
160 |
} |
|
161 |
|
|
162 |
// 获取系统对应的动态库文件夹 |
|
163 |
private static String getLibraryFold() { |
|
164 |
String osType; |
|
165 |
String osName = System.getProperty("os.name"); |
|
166 |
if (osName.toLowerCase().startsWith("linux")) { |
|
167 |
osType = ARCH_LINUX; |
|
168 |
} else if (osName.toLowerCase().startsWith("mac") |
|
169 |
|| osName.toLowerCase().startsWith("darwin")) { |
|
170 |
osType = ARCH_MAC; |
|
171 |
} else if (osName.toLowerCase().startsWith("windows")) { |
|
172 |
osType = ARCH_WINDOWS; |
|
173 |
} else { |
|
174 |
osType = ""; |
|
175 |
} |
|
176 |
String arch = System.getProperty("os.arch"); |
|
177 |
arch = arch.toLowerCase().trim(); |
|
178 |
if ("i386".equals(arch) || "i686".equals(arch)||"x86".equals(arch)) { |
|
179 |
arch = PREFIX_32 + ""; |
|
180 |
} else if ("x86_64".equals(arch) || "amd64".equals(arch)) { |
|
181 |
arch = PREFIX_64 + ""; |
|
182 |
} else if (arch.startsWith("arm")) { |
|
183 |
arch = PREFIX_ARM + ""; |
|
184 |
} |
|
185 |
return osType + arch; |
|
186 |
} |
|
187 |
} |