]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/src/org/simantics/fastlz/impl/OS.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.fastlz / src / org / simantics / fastlz / impl / OS.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.fastlz.impl;
13
14 import java.io.Closeable;
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URL;
21
22 import org.simantics.fastlz.ARCHType;
23 import org.simantics.fastlz.OSType;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public class OS {
29
30     /**
31      * Extracts the specified source file in the specified bundle into the
32      * specified local directory.
33      * 
34      * @param url the source URL to stream the resource from
35      * @param targetFile the target file to write the resource to
36      * @param deleteOnExit <code>true</code> to use {@link File#deleteOnExit()}
37      *        on the resulting file. Note that this does not guarantee that the
38      *        file is deleted when the JVM exits
39      * @return the resulting file
40      * @throws FileNotFoundException
41      */
42     public static File copyResource(URL url, File targetFile) throws IOException, FileNotFoundException {
43         FileOutputStream os = null;
44         InputStream is = null;
45         try {
46             if (targetFile.exists())
47                 targetFile.delete();
48
49             is = url.openStream();
50             int read;
51             byte [] buffer = new byte [16384];
52             os = new FileOutputStream (targetFile);
53             while ((read = is.read (buffer)) != -1) {
54                 os.write(buffer, 0, read);
55             }
56             os.close ();
57             is.close ();
58
59             return targetFile;
60         } finally {
61             uncheckedClose(os);
62             uncheckedClose(is);
63         }
64     }
65
66     public static File extractLib(URL libURL, String libName) throws FileNotFoundException, IOException {
67         String tmpDirStr = System.getProperty("java.io.tmpdir");
68         if (tmpDirStr == null)
69             throw new NullPointerException("java.io.tmpdir property is null");
70         File tmpDir = new File(tmpDirStr);
71         File libFile = new File(tmpDir, libName);
72         return copyResource(libURL, libFile);
73     }
74
75     public static void uncheckedClose(Closeable closeable) {
76         try {
77             if (closeable != null)
78                 closeable.close();
79         } catch (IOException e) {
80             //ignore
81         }
82     }
83
84     public static String formOsArchSuffix() {
85         String osName = System.getProperty("os.name");
86         String osArch = System.getProperty("os.arch");
87         OSType os = OSType.calculate();
88         ARCHType arch = ARCHType.calculate();
89
90         if (os == OSType.UNKNOWN)
91             throw new UnsatisfiedLinkError("unknown OS '" + osName + "' cannot load native fastlz library");
92         if (arch == ARCHType.UNKNOWN)
93             throw new UnsatisfiedLinkError("unknown architecture '" + osArch + "' cannot load native fastlz library");
94
95         String lib = "";
96         switch (os) {
97             case APPLE:
98                 lib += "-darwin";
99                 switch (arch) {
100                     case PPC:
101                         lib += "-ppc";
102                         break;
103                     case PPC_64:
104                         lib += "-ppc_64";
105                         break;
106                     case X86:
107                         lib += "-x86";
108                         break;
109                     case X86_64:
110                         lib += "-x86_64";
111                         break;
112                     default:
113                         throw new UnsatisfiedLinkError("Unsupported architecture for Apple OS: " + osArch);
114                 }
115                 break;
116             case LINUX:
117                 lib += "-linux";
118                 switch (arch) {
119                     case X86:
120                         lib += "-x86";
121                         break;
122                     case X86_64:
123                         lib += "-x86_64";
124                         break;
125                     default:
126                         throw new UnsatisfiedLinkError("Unsupported architecture for Linux OS: " + osArch);
127                 }
128                 break;
129             case SUN:
130                 lib += "-sun";
131                 switch (arch) {
132                     case SPARC:
133                         lib += "-sparc";
134                         break;
135                     case X86_64:
136                         lib += "-x86_64";
137                         break;
138                     default:
139                         throw new UnsatisfiedLinkError("Unsupported architecture for Sun OS: " + osArch);
140                 }
141                 break;
142             case WINDOWS:
143                 lib += "-windows";
144                 switch (arch) {
145                     case X86:
146                         lib += "-x86";
147                         break;
148                     case X86_64:
149                         lib += "-x86_64";
150                         break;
151                     default:
152                         throw new UnsatisfiedLinkError("Unsupported architecture for Windows: " + osArch);
153                 }
154                 break;
155             default:
156                 throw new UnsatisfiedLinkError("Unsupported operating system: " + os);
157         }
158         return lib;
159     }
160
161     public static String resolveLibName() {
162         String lib = "fastlz";
163         lib = System.mapLibraryName(lib + formOsArchSuffix());
164         return lib;
165     }
166
167 }