-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeLibrary.java
More file actions
111 lines (99 loc) · 4.11 KB
/
Copy pathNativeLibrary.java
File metadata and controls
111 lines (99 loc) · 4.11 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
package org.tensorflow;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
final class NativeLibrary {
private static final boolean DEBUG = (System.getProperty("org.tensorflow.NativeLibrary.DEBUG") != null);
private static final String LIBNAME = "tensorflow_jni";
NativeLibrary() {
}
public static void load() {
if (!isLoaded() && !tryLoadLibrary()) {
String makeResourceName = makeResourceName();
log("resourceName: " + makeResourceName);
InputStream resourceAsStream = NativeLibrary.class.getClassLoader().getResourceAsStream(makeResourceName);
if (resourceAsStream == null) {
throw new UnsatisfiedLinkError(String.format("Cannot find TensorFlow native library for OS: %s, architecture: %s. See https://github.com/tensorflow/tensorflow/tree/master/java/README.md for possible solutions (such as building the library from source).", new Object[]{os(), architecture()}));
}
try {
System.load(extractResource(resourceAsStream));
} catch (IOException e) {
throw new UnsatisfiedLinkError(String.format("Unable to extract native library into a temporary file (%s)", new Object[]{e.toString()}));
}
}
}
private static boolean tryLoadLibrary() {
try {
System.loadLibrary(LIBNAME);
return true;
} catch (UnsatisfiedLinkError e) {
log("tryLoadLibraryFailed: " + e.getMessage());
return false;
}
}
private static boolean isLoaded() {
try {
TensorFlow.version();
log("isLoaded: true");
return true;
} catch (UnsatisfiedLinkError e) {
return false;
}
}
private static String extractResource(InputStream inputStream) throws IOException {
String mapLibraryName = System.mapLibraryName(LIBNAME);
int indexOf = mapLibraryName.indexOf(".");
File createTempFile = File.createTempFile(indexOf < 0 ? mapLibraryName : mapLibraryName.substring(0, indexOf), indexOf < 0 ? null : mapLibraryName.substring(indexOf));
String absolutePath = createTempFile.getAbsolutePath();
createTempFile.deleteOnExit();
log("extracting native library to: " + absolutePath);
long copy = copy(inputStream, createTempFile);
log(String.format("copied %d bytes to %s", new Object[]{Long.valueOf(copy), absolutePath}));
return absolutePath;
}
private static String os() {
String toLowerCase = System.getProperty("os.name").toLowerCase();
if (toLowerCase.contains("linux")) {
return "linux";
}
if (toLowerCase.contains("os x") || toLowerCase.contains("darwin")) {
return "darwin";
}
if (toLowerCase.contains("windows")) {
return "windows";
}
return toLowerCase.replaceAll("\\s", "");
}
private static String architecture() {
String toLowerCase = System.getProperty("os.arch").toLowerCase();
return toLowerCase.equals("amd64") ? "x86_64" : toLowerCase;
}
private static void log(String str) {
if (DEBUG) {
System.err.println("org.tensorflow.NativeLibrary: " + str);
}
}
private static String makeResourceName() {
return "org/tensorflow/native/" + String.format("%s-%s/", new Object[]{os(), architecture()}) + System.mapLibraryName(LIBNAME);
}
private static long copy(InputStream inputStream, File file) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(file);
try {
byte[] bArr = new byte[1048576];
long j = 0;
while (true) {
int read = inputStream.read(bArr);
if (read < 0) {
break;
}
fileOutputStream.write(bArr, 0, read);
j += (long) read;
}
return j;
} finally {
fileOutputStream.close();
inputStream.close();
}
}
}