SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
NativeUtils.java
1/*
2 * Class NativeUtils is published under the The MIT License:
3 *
4 * Copyright (c) 2012 Adam Heinrich <adam@adamh.cz>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package umontreal.ssj.util;
25
26import java.io.File;
27import java.io.FileNotFoundException;
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.OutputStream;
32
42public class NativeUtils {
43
47 private NativeUtils() {
48 }
49
68 public static void loadLibraryFromJar(String path) throws IOException {
69
70 if (!path.startsWith("/")) {
71 throw new IllegalArgumentException("The path has to be absolute (start with '/').");
72 }
73
74 // Obtain filename from path
75 String[] parts = path.split("/");
76 String filename = (parts.length > 1) ? parts[parts.length - 1] : null;
77
78 // Split filename to prefix and suffix (extension)
79 String prefix = "";
80 String suffix = null;
81 if (filename != null) {
82 parts = filename.split("\\.", 2);
83 prefix = parts[0];
84 suffix = (parts.length > 1) ? "." + parts[parts.length - 1] : null; // Thanks, davs! :-)
85 }
86
87 // Check if the filename is okay
88 if (filename == null || prefix.length() < 3) {
89 throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
90 }
91
92 // Prepare temporary file
93 File temp = File.createTempFile(prefix, suffix);
94 temp.deleteOnExit();
95
96 if (!temp.exists()) {
97 throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist.");
98 }
99
100 // Prepare buffer for data copying
101 byte[] buffer = new byte[1024];
102 int readBytes;
103
104 // Open and check input stream
105 InputStream is = NativeUtils.class.getResourceAsStream(path);
106 if (is == null) {
107 throw new FileNotFoundException("File " + path + " was not found inside JAR.");
108 }
109
110 // Open output stream and copy data between source file in JAR and the temporary
111 // file
112 OutputStream os = new FileOutputStream(temp);
113 try {
114 while ((readBytes = is.read(buffer)) != -1) {
115 os.write(buffer, 0, readBytes);
116 }
117 } finally {
118 // If read/write fails, close streams safely before throwing an exception
119 os.close();
120 is.close();
121 }
122
123 // Finally, load the library
124 System.load(temp.getAbsolutePath());
125 }
126}
static void loadLibraryFromJar(String path)
Loads library from current JAR archive.