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