]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.fastlz/src/org/simantics/fastlz/impl/OS.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.fastlz / src / org / simantics / fastlz / impl / OS.java
diff --git a/bundles/org.simantics.fastlz/src/org/simantics/fastlz/impl/OS.java b/bundles/org.simantics.fastlz/src/org/simantics/fastlz/impl/OS.java
new file mode 100644 (file)
index 0000000..034096b
--- /dev/null
@@ -0,0 +1,167 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.fastlz.impl;\r
+\r
+import java.io.Closeable;\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.URL;\r
+\r
+import org.simantics.fastlz.ARCHType;\r
+import org.simantics.fastlz.OSType;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class OS {\r
+\r
+    /**\r
+     * Extracts the specified source file in the specified bundle into the\r
+     * specified local directory.\r
+     * \r
+     * @param url the source URL to stream the resource from\r
+     * @param targetFile the target file to write the resource to\r
+     * @param deleteOnExit <code>true</code> to use {@link File#deleteOnExit()}\r
+     *        on the resulting file. Note that this does not guarantee that the\r
+     *        file is deleted when the JVM exits\r
+     * @return the resulting file\r
+     * @throws FileNotFoundException\r
+     */\r
+    public static File copyResource(URL url, File targetFile) throws IOException, FileNotFoundException {\r
+        FileOutputStream os = null;\r
+        InputStream is = null;\r
+        try {\r
+            if (targetFile.exists())\r
+                targetFile.delete();\r
+\r
+            is = url.openStream();\r
+            int read;\r
+            byte [] buffer = new byte [16384];\r
+            os = new FileOutputStream (targetFile);\r
+            while ((read = is.read (buffer)) != -1) {\r
+                os.write(buffer, 0, read);\r
+            }\r
+            os.close ();\r
+            is.close ();\r
+\r
+            return targetFile;\r
+        } finally {\r
+            uncheckedClose(os);\r
+            uncheckedClose(is);\r
+        }\r
+    }\r
+\r
+    public static File extractLib(URL libURL, String libName) throws FileNotFoundException, IOException {\r
+        String tmpDirStr = System.getProperty("java.io.tmpdir");\r
+        if (tmpDirStr == null)\r
+            throw new NullPointerException("java.io.tmpdir property is null");\r
+        File tmpDir = new File(tmpDirStr);\r
+        File libFile = new File(tmpDir, libName);\r
+        return copyResource(libURL, libFile);\r
+    }\r
+\r
+    public static void uncheckedClose(Closeable closeable) {\r
+        try {\r
+            if (closeable != null)\r
+                closeable.close();\r
+        } catch (IOException e) {\r
+            //ignore\r
+        }\r
+    }\r
+\r
+    public static String formOsArchSuffix() {\r
+        String osName = System.getProperty("os.name");\r
+        String osArch = System.getProperty("os.arch");\r
+        OSType os = OSType.calculate();\r
+        ARCHType arch = ARCHType.calculate();\r
+\r
+        if (os == OSType.UNKNOWN)\r
+            throw new UnsatisfiedLinkError("unknown OS '" + osName + "' cannot load native fastlz library");\r
+        if (arch == ARCHType.UNKNOWN)\r
+            throw new UnsatisfiedLinkError("unknown architecture '" + osArch + "' cannot load native fastlz library");\r
+\r
+        String lib = "";\r
+        switch (os) {\r
+            case APPLE:\r
+                lib += "-darwin";\r
+                switch (arch) {\r
+                    case PPC:\r
+                        lib += "-ppc";\r
+                        break;\r
+                    case PPC_64:\r
+                        lib += "-ppc_64";\r
+                        break;\r
+                    case X86:\r
+                        lib += "-x86";\r
+                        break;\r
+                    case X86_64:\r
+                        lib += "-x86_64";\r
+                        break;\r
+                    default:\r
+                        throw new UnsatisfiedLinkError("Unsupported architecture for Apple OS: " + osArch);\r
+                }\r
+                break;\r
+            case LINUX:\r
+                lib += "-linux";\r
+                switch (arch) {\r
+                    case X86:\r
+                        lib += "-x86";\r
+                        break;\r
+                    case X86_64:\r
+                        lib += "-x86_64";\r
+                        break;\r
+                    default:\r
+                        throw new UnsatisfiedLinkError("Unsupported architecture for Linux OS: " + osArch);\r
+                }\r
+                break;\r
+            case SUN:\r
+                lib += "-sun";\r
+                switch (arch) {\r
+                    case SPARC:\r
+                        lib += "-sparc";\r
+                        break;\r
+                    case X86_64:\r
+                        lib += "-x86_64";\r
+                        break;\r
+                    default:\r
+                        throw new UnsatisfiedLinkError("Unsupported architecture for Sun OS: " + osArch);\r
+                }\r
+                break;\r
+            case WINDOWS:\r
+                lib += "-windows";\r
+                switch (arch) {\r
+                    case X86:\r
+                        lib += "-x86";\r
+                        break;\r
+                    case X86_64:\r
+                        lib += "-x86_64";\r
+                        break;\r
+                    default:\r
+                        throw new UnsatisfiedLinkError("Unsupported architecture for Windows: " + osArch);\r
+                }\r
+                break;\r
+            default:\r
+                throw new UnsatisfiedLinkError("Unsupported operating system: " + os);\r
+        }\r
+        return lib;\r
+    }\r
+\r
+    public static String resolveLibName() {\r
+        String lib = "fastlz";\r
+        lib = System.mapLibraryName(lib + formOsArchSuffix());\r
+        return lib;\r
+    }\r
+\r
+}\r