1 package net.jpountz.util;
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
22 /** FOR INTERNAL USE ONLY */
27 // Even on Windows, the default compiler from cpptasks (gcc) uses .so as a shared lib extension
28 WINDOWS("win32", "dll"), LINUX("linux", "so"), MAC("darwin", "dylib"), SOLARIS("solaris", "so");
29 public final String name, libExtension;
31 private OS(String name, String libExtension) {
33 this.libExtension = libExtension;
37 private static String arch() {
38 return System.getProperty("os.arch");
41 private static OS os() {
42 String osName = System.getProperty("os.name");
43 if (osName.contains("Linux")) {
45 } else if (osName.contains("Mac")) {
47 } else if (osName.contains("Windows")) {
49 } else if (osName.contains("Solaris") || osName.contains("SunOS")) {
52 throw new UnsupportedOperationException("Unsupported operating system: "
57 private static String resourceName() {
59 String packagePrefix = Native.class.getPackage().getName().replace('.', '/');
61 return "/" + packagePrefix + "/" + os.name + "/" + arch() + "/liblz4-java." + os.libExtension;
64 private static boolean loaded = false;
66 public static synchronized boolean isLoaded() {
70 public static synchronized void load() {
74 String resourceName = resourceName();
75 InputStream is = Native.class.getResourceAsStream(resourceName);
77 throw new UnsupportedOperationException("Unsupported OS/arch, cannot find " + resourceName + ". Please try building from source.");
81 tempLib = File.createTempFile("liblz4-java", "." + os().libExtension);
83 FileOutputStream out = new FileOutputStream(tempLib);
85 byte[] buf = new byte[4096];
87 int read = is.read(buf);
91 out.write(buf, 0, read);
96 } catch (IOException e) {
99 System.load(tempLib.getAbsolutePath());
106 } catch (IOException e) {
109 if (tempLib != null && tempLib.exists()) {
113 // try to delete on exit, does it work on Windows?
114 tempLib.deleteOnExit();
118 } catch (IOException e) {
119 throw new ExceptionInInitializerError("Cannot unpack liblz4-java");