1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.fastlz.impl;
\r
14 import java.io.Closeable;
\r
15 import java.io.File;
\r
16 import java.io.FileNotFoundException;
\r
17 import java.io.FileOutputStream;
\r
18 import java.io.IOException;
\r
19 import java.io.InputStream;
\r
20 import java.net.URL;
\r
22 import org.simantics.fastlz.ARCHType;
\r
23 import org.simantics.fastlz.OSType;
\r
26 * @author Tuukka Lehtonen
\r
31 * Extracts the specified source file in the specified bundle into the
\r
32 * specified local directory.
\r
34 * @param url the source URL to stream the resource from
\r
35 * @param targetFile the target file to write the resource to
\r
36 * @param deleteOnExit <code>true</code> to use {@link File#deleteOnExit()}
\r
37 * on the resulting file. Note that this does not guarantee that the
\r
38 * file is deleted when the JVM exits
\r
39 * @return the resulting file
\r
40 * @throws FileNotFoundException
\r
42 public static File copyResource(URL url, File targetFile) throws IOException, FileNotFoundException {
\r
43 FileOutputStream os = null;
\r
44 InputStream is = null;
\r
46 if (targetFile.exists())
\r
47 targetFile.delete();
\r
49 is = url.openStream();
\r
51 byte [] buffer = new byte [16384];
\r
52 os = new FileOutputStream (targetFile);
\r
53 while ((read = is.read (buffer)) != -1) {
\r
54 os.write(buffer, 0, read);
\r
66 public static File extractLib(URL libURL, String libName) throws FileNotFoundException, IOException {
\r
67 String tmpDirStr = System.getProperty("java.io.tmpdir");
\r
68 if (tmpDirStr == null)
\r
69 throw new NullPointerException("java.io.tmpdir property is null");
\r
70 File tmpDir = new File(tmpDirStr);
\r
71 File libFile = new File(tmpDir, libName);
\r
72 return copyResource(libURL, libFile);
\r
75 public static void uncheckedClose(Closeable closeable) {
\r
77 if (closeable != null)
\r
79 } catch (IOException e) {
\r
84 public static String formOsArchSuffix() {
\r
85 String osName = System.getProperty("os.name");
\r
86 String osArch = System.getProperty("os.arch");
\r
87 OSType os = OSType.calculate();
\r
88 ARCHType arch = ARCHType.calculate();
\r
90 if (os == OSType.UNKNOWN)
\r
91 throw new UnsatisfiedLinkError("unknown OS '" + osName + "' cannot load native fastlz library");
\r
92 if (arch == ARCHType.UNKNOWN)
\r
93 throw new UnsatisfiedLinkError("unknown architecture '" + osArch + "' cannot load native fastlz library");
\r
113 throw new UnsatisfiedLinkError("Unsupported architecture for Apple OS: " + osArch);
\r
126 throw new UnsatisfiedLinkError("Unsupported architecture for Linux OS: " + osArch);
\r
139 throw new UnsatisfiedLinkError("Unsupported architecture for Sun OS: " + osArch);
\r
152 throw new UnsatisfiedLinkError("Unsupported architecture for Windows: " + osArch);
\r
156 throw new UnsatisfiedLinkError("Unsupported operating system: " + os);
\r
161 public static String resolveLibName() {
\r
162 String lib = "fastlz";
\r
163 lib = System.mapLibraryName(lib + formOsArchSuffix());
\r