diff options
Diffstat (limited to 'flow/src')
-rw-r--r-- | flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala b/flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala index 9f26ef0..2526293 100644 --- a/flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala +++ b/flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala @@ -1,37 +1,58 @@ package com.github.jodersky.flow.internal import java.io.File - -import scalax.file.Path -import scalax.io.Resource +import java.io.FileOutputStream +import java.io.InputStream +import java.io.OutputStream /** Handles loading of the current platform's native library for flow. */ object NativeLoader { - private def os = System.getProperty("os.name").toLowerCase.filter(_ != ' ') + private final val BufferSize = 4096 - private def arch = System.getProperty("os.arch").toLowerCase + private def os = System.getProperty("os.name").toLowerCase.replaceAll("\\s", "") - private def extractNative(nativeLibrary: String): Option[File] = { - val fqlib = System.mapLibraryName(nativeLibrary) //fully qualified library name - - val in = NativeLoader.getClass().getResourceAsStream(s"/native/${os}-${arch}/${fqlib}") - if (in == null) return None + private def arch = System.getProperty("os.arch").toLowerCase - val temp = Path.createTempFile(nativeLibrary) - Resource.fromInputStream(in).copyDataTo(temp) - temp.fileOption + /** Extract a resource from this class loader to a temporary file. */ + private def extract(path: String, prefix: String): Option[File] = { + var in: Option[InputStream] = None + var out: Option[OutputStream] = None + + try { + in = Option(NativeLoader.getClass.getResourceAsStream(path)) + if (in.isEmpty) return None + + val file = File.createTempFile(prefix, "") + out = Some(new FileOutputStream(file)) + + val buffer = new Array[Byte](BufferSize) + var length = -1; + do { + length = in.get.read(buffer) + if (length != -1) out.get.write(buffer, 0, length) + } while (length != -1) + + Some(file) + } finally { + in.foreach(_.close) + out.foreach(_.close) + } } - private def loadFromJar(nativeLibrary: String) = extractNative(nativeLibrary) match { - case Some(file) => System.load(file.getAbsolutePath) - case None => throw new UnsatisfiedLinkError("Cannot extract flow's native library, the native library may not exist for your specific architecture/OS combination.") + private def loadFromJar(library: String) = { + val fqlib = System.mapLibraryName(library) //fully qualified library name + extract(s"/native/${os}-${arch}/${fqlib}", fqlib) match { + case Some(file) => System.load(file.getAbsolutePath) + case None => throw new UnsatisfiedLinkError("Cannot extract flow's native library, " + + "the native library may not exist for your specific architecture/OS combination.") + } } - - def load(nativeLibrary: String) = try { - System.loadLibrary(nativeLibrary) + + def load(library: String) = try { + System.loadLibrary(library) } catch { - case ex: UnsatisfiedLinkError => loadFromJar(nativeLibrary) + case ex: UnsatisfiedLinkError => loadFromJar(library) } }
\ No newline at end of file |