1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.fastlz.impl;
14 import java.io.Closeable;
16 import java.io.FileNotFoundException;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
22 import org.simantics.fastlz.ARCHType;
23 import org.simantics.fastlz.OSType;
26 * @author Tuukka Lehtonen
31 * Extracts the specified source file in the specified bundle into the
32 * specified local directory.
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
42 public static File copyResource(URL url, File targetFile) throws IOException, FileNotFoundException {
43 FileOutputStream os = null;
44 InputStream is = null;
46 if (targetFile.exists())
49 is = url.openStream();
51 byte [] buffer = new byte [16384];
52 os = new FileOutputStream (targetFile);
53 while ((read = is.read (buffer)) != -1) {
54 os.write(buffer, 0, read);
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);
75 public static void uncheckedClose(Closeable closeable) {
77 if (closeable != null)
79 } catch (IOException e) {
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();
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");
113 throw new UnsatisfiedLinkError("Unsupported architecture for Apple OS: " + osArch);
126 throw new UnsatisfiedLinkError("Unsupported architecture for Linux OS: " + osArch);
139 throw new UnsatisfiedLinkError("Unsupported architecture for Sun OS: " + osArch);
152 throw new UnsatisfiedLinkError("Unsupported architecture for Windows: " + osArch);
156 throw new UnsatisfiedLinkError("Unsupported operating system: " + os);
161 public static String resolveLibName() {
162 String lib = "fastlz";
163 lib = System.mapLibraryName(lib + formOsArchSuffix());