Methods Summary |
---|
static java.io.File | computeOutputFile(java.io.File outputDirectory, java.lang.String mappedName)Compute an unique name for us to copy the library to.
The file returned will not be in use by any other process.
for (int i = 0; i < 1024; i++) {
File check = new File(outputDirectory, mappedName + "." + i);
if (check.createNewFile()) {
return check;
}
}
throw new RuntimeException("More that 1024 processes are running that are using or have failed to clean: " + outputDirectory + "/" + mappedName);
|
private static void | copyStream(java.io.InputStream is, java.io.File outputFile)
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(outputFile));
int c;
while ((c = is.read()) != -1) {
os.write(c);
}
} finally {
safeClose(os);
}
|
public void | findAndLoad(java.lang.Class classInJar, java.lang.String library)
String mappedName = System.mapLibraryName(library);
InputStream is = findLibraryResource(classInJar, mappedName);
try {
NativeLibraryFinder.getLibraryDirectory().mkdirs();
File outputFile = computeOutputFile(NativeLibraryFinder.getLibraryDirectory(), mappedName);
outputFile.deleteOnExit(); // delete on exit
copyStream(is, outputFile);
System.load(outputFile.getAbsolutePath());
} catch (IOException e) {
UnsatisfiedLinkError le = new UnsatisfiedLinkError("Failed to extract native library");
le.initCause(e);
throw le;
} finally {
safeClose(is);
}
|
java.io.InputStream | findLibraryResource(java.lang.Class classInJar, java.lang.String mappedName)
for (int i = 0; i < NativeLibraryFinder.OS_PREFIXES.length; i++) {
String osPrefix = NativeLibraryFinder.OS_PREFIXES[i];
String resource = osPrefix + mappedName;
s_logger.log(Level.FINE, "trying {0}", resource);
InputStream is = classInJar.getResourceAsStream(resource);
if (is != null) {
return is;
}
}
throw new UnsatisfiedLinkError("Failed to find: " + mappedName);
|
private static void | safeClose(java.io.InputStream closable)
if (closable == null) {
return;
}
try {
closable.close();
} catch (IOException e) {
s_logger.log(Level.INFO, "Failed to close", e);
}
|
private static void | safeClose(java.io.OutputStream closable)
if (closable == null) {
return;
}
try {
closable.close();
} catch (IOException e) {
s_logger.log(Level.INFO, "Failed to close", e);
}
|