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