fiore@0
|
1 /*
|
fiore@0
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
fiore@0
|
3
|
fiore@0
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
fiore@0
|
5
|
fiore@0
|
6 This program is free software: you can redistribute it and/or modify
|
fiore@0
|
7 it under the terms of the GNU General Public License as published by
|
fiore@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
fiore@0
|
9 (at your option) any later version.
|
fiore@0
|
10
|
fiore@0
|
11 This program is distributed in the hope that it will be useful,
|
fiore@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
fiore@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
fiore@0
|
14 GNU General Public License for more details.
|
fiore@0
|
15
|
fiore@0
|
16 You should have received a copy of the GNU General Public License
|
fiore@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
fiore@0
|
18 */
|
fiore@0
|
19
|
fiore@0
|
20 package uk.ac.qmul.eecs.ccmi.utils;
|
fiore@0
|
21
|
fiore@0
|
22 import java.io.File;
|
fiore@0
|
23 import java.io.FileOutputStream;
|
fiore@0
|
24 import java.io.IOException;
|
fiore@0
|
25 import java.io.InputStream;
|
fiore@0
|
26 import java.net.URL;
|
fiore@0
|
27
|
fiore@0
|
28 /**
|
fiore@0
|
29 * This class is used to extract a native library (e.g. .dll file in windows) from within a jar
|
fiore@0
|
30 * to the local file system, in order to allow the virtual machine to load it.
|
fiore@0
|
31 *
|
fiore@0
|
32 */
|
fiore@0
|
33 public class NativeLibFileWriter {
|
fiore@0
|
34
|
fiore@0
|
35 /**
|
fiore@0
|
36 * Creates an instance of the the class linked to a native library file.
|
fiore@0
|
37 * @param resource the URL of the native library file. The URL can be obtained by
|
fiore@0
|
38 * @see Class#getResource(String), therefore can be called from a class within a jar file
|
fiore@0
|
39 * which needs to access a static library.
|
fiore@0
|
40 */
|
fiore@0
|
41 public NativeLibFileWriter(URL resource){
|
fiore@0
|
42 this.resource = resource;
|
fiore@0
|
43 }
|
fiore@0
|
44
|
fiore@0
|
45 /**
|
fiore@0
|
46 * Writes the file in a directory returned by {@link PreferencesService#get(String, String)}} if defined, or
|
fiore@0
|
47 * the System default temporary directory otherwise.
|
fiore@0
|
48 * The path to the file can be retrieved by @see {@link #getFilePath()} and then passed as argument
|
fiore@0
|
49 * to {@link System#load(String)}
|
fiore@0
|
50 *
|
fiore@0
|
51 * @param prefix a prefix the temporary native library file will have in the temporary directory
|
fiore@0
|
52 */
|
fiore@0
|
53 public void writeToDisk(String fileName){
|
fiore@0
|
54 InputStream in = null;
|
fiore@0
|
55 FileOutputStream out = null;
|
fiore@0
|
56 File lib = new File(PreferencesService.getInstance().get("dir.libs", System.getProperty("java.io.tmpdir")),fileName);
|
fiore@0
|
57 if(lib.exists()){ //if dll already exists. no job needs to be done.
|
fiore@0
|
58 path = lib.getAbsolutePath();
|
fiore@0
|
59 return;
|
fiore@0
|
60 }
|
fiore@0
|
61 try{
|
fiore@0
|
62 in = resource.openStream();
|
fiore@0
|
63 out = new FileOutputStream(lib);
|
fiore@0
|
64 int byteRead;
|
fiore@0
|
65 byte[] b = new byte[1024];
|
fiore@0
|
66 while((byteRead = in.read(b)) > 0){
|
fiore@0
|
67 out.write(b, 0, byteRead);
|
fiore@0
|
68 }
|
fiore@0
|
69 path = lib.getAbsolutePath();
|
fiore@0
|
70 }catch(IOException ioe){
|
fiore@0
|
71 path = null;
|
fiore@0
|
72 }finally{
|
fiore@0
|
73 if(in != null)
|
fiore@0
|
74 try{in.close();}catch(IOException ioe){}
|
fiore@0
|
75 if(out != null)
|
fiore@0
|
76 try{out.close();}catch(IOException ioe){}
|
fiore@0
|
77 }
|
fiore@0
|
78
|
fiore@0
|
79 }
|
fiore@0
|
80
|
fiore@0
|
81 public String getFilePath(){
|
fiore@0
|
82 return path;
|
fiore@0
|
83 }
|
fiore@0
|
84
|
fiore@0
|
85 private URL resource;
|
fiore@0
|
86 private String path;
|
fiore@0
|
87 }
|