diff options
Diffstat (limited to 'flow-main/src')
-rw-r--r-- | flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala b/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala index aebbe3f..23314f1 100644 --- a/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala +++ b/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala @@ -2,35 +2,37 @@ package com.github.jodersky.flow.internal import java.io.File import java.io.FileOutputStream +import scalax.file.Path +import scalax.io.Resource +import scala.util.Try /**Loads the current system's native library for flow. */ object NativeLoader { - def load = { + def extract(): Option[File] = { val os = System.getProperty("os.name").toLowerCase val arch = System.getProperty("os.arch").toLowerCase + val fqlib = System.mapLibraryName("flow") //fully qualified library name - val in = NativeLoader.getClass().getResourceAsStream("/native/" + os + "/" + arch + "/" + "libflow.so") - val temp = File.createTempFile("flow" + os + arch, ".so"); - temp.deleteOnExit() - val out = new FileOutputStream(temp); + val in = NativeLoader.getClass().getResourceAsStream(s"/native/${os}/${arch}/${fqlib}") + if (in == null) return None - try { - var read: Int = 0; ; - val buffer = new Array[Byte](4096); - do { - read = in.read(buffer) - if (read != -1) { - out.write(buffer, 0, read); - } - } while (read != -1) - } finally { - in.close() - out.close - } + val temp = Path.createTempFile() + Resource.fromInputStream(in).copyDataTo(temp) + temp.fileOption + } - System.load(temp.getAbsolutePath()) + def loadFromJar() = extract() match { + case Some(file) => System.load(file.getAbsolutePath) + case None => throw new UnsatisfiedLinkError("cannot extract native library, the native library may not exist for your specific system/architecture combination") + } + def load = { + try { + System.loadLibrary("flow") + } catch { + case ex: UnsatisfiedLinkError => loadFromJar() + } } }
\ No newline at end of file |