-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathLibFile.java
More file actions
81 lines (65 loc) · 2.13 KB
/
Copy pathLibFile.java
File metadata and controls
81 lines (65 loc) · 2.13 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
package datadog.nativeloader;
import java.io.File;
import java.nio.file.Path;
/**
* Represents a resolved library
*
* <ul>
* <li>library may be preloaded - with no backing file
* <li>regular file - that doesn't require clean-up
* <li>temporary file - copying from another source - that does require clean-up
* </ul>
*/
public final class LibFile implements AutoCloseable {
static final boolean NO_CLEAN_UP = false;
static final boolean CLEAN_UP = true;
static final LibFile preloaded(String libName) {
return new LibFile(libName, null, NO_CLEAN_UP);
}
static final LibFile fromFile(String libName, File file) {
return new LibFile(libName, file, NO_CLEAN_UP);
}
static final LibFile fromTempFile(String libName, File file) {
return new LibFile(libName, file, CLEAN_UP);
}
final String libName;
final File file;
final boolean needsCleanup;
LibFile(String libName, File file, boolean needsCleanup) {
this.libName = libName;
this.file = file;
this.needsCleanup = needsCleanup;
}
/** Indicates if this library was "preloaded" */
public boolean isPreloaded() {
return (this.file == null);
}
/** Loads the underlying library into the JVM */
public void load() throws LibraryLoadException {
if (this.isPreloaded()) return;
try {
Runtime.getRuntime().load(this.getAbsolutePath());
} catch (Throwable t) {
throw new LibraryLoadException(this.libName, t);
}
}
/** Provides a File to the library -- returns null for pre-loaded libraries */
public final File toFile() {
return this.file;
}
/** Provides a Path to the library -- return null for pre-loaded libraries */
public final Path toPath() {
return this.file == null ? null : this.file.toPath();
}
/** Provides the an absolute path to the library -- returns null for pre-loaded libraries */
public final String getAbsolutePath() {
return this.file == null ? null : this.file.getAbsolutePath();
}
/** Schedules clean-up of underlying file -- if the file is a temp file */
@Override
public void close() {
if (this.needsCleanup) {
NativeLoader.delete(this.file);
}
}
}