fiore@3: /* fiore@3: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool fiore@3: fiore@3: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@3: fiore@3: This program is free software: you can redistribute it and/or modify fiore@3: it under the terms of the GNU General Public License as published by fiore@3: the Free Software Foundation, either version 3 of the License, or fiore@3: (at your option) any later version. fiore@3: fiore@3: This program is distributed in the hope that it will be useful, fiore@3: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@3: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@3: GNU General Public License for more details. fiore@3: fiore@3: You should have received a copy of the GNU General Public License fiore@3: along with this program. If not, see . fiore@3: */ fiore@3: fiore@3: package uk.ac.qmul.eecs.ccmi.utils; fiore@3: fiore@3: import java.io.File; fiore@3: import java.io.FileOutputStream; fiore@3: import java.io.IOException; fiore@3: import java.io.InputStream; fiore@3: import java.net.URL; fiore@3: fiore@3: /** fiore@3: * This class is used to extract a native library (e.g. .dll file in windows) from within a jar fiore@3: * to the local file system, in order to allow the virtual machine to load it. fiore@3: * fiore@3: */ fiore@3: public class ResourceFileWriter { fiore@3: fiore@3: /** fiore@3: * Creates an instance of the the class linked to a native library file. fiore@3: * @param resource the URL of the native library file. The URL can be obtained by fiore@3: * @see Class#getResource(String), therefore can be called from a class within a jar file fiore@3: * which needs to access a static library. fiore@3: */ fiore@3: public ResourceFileWriter(URL resource){ fiore@3: this.resource = resource; fiore@3: } fiore@3: fiore@3: public void serResource(URL resource){ fiore@3: this.resource = resource;; fiore@3: path = null; fiore@3: } fiore@3: fiore@3: /** fiore@3: * Writes the file in a directory returned by {@link PreferencesService#get(String, String)}} if defined, or fiore@3: * the System default temporary directory otherwise. fiore@3: * The path to the file can be retrieved by @see {@link #getFilePath()} and then passed as argument fiore@3: * to {@link System#load(String)} fiore@3: * fiore@3: * @param prefix a prefix the temporary native library file will have in the temporary directory fiore@3: */ fiore@3: public void writeToDisk(String dir,String fileName){ fiore@3: if (resource == null) fiore@3: return; fiore@3: InputStream in = null; fiore@3: FileOutputStream out = null; fiore@3: File file = new File(dir,fileName); fiore@3: if(file.exists()){ //if file already exists. no job needs to be done. fiore@3: path = file.getAbsolutePath(); fiore@3: return; fiore@3: } fiore@3: try{ fiore@3: in = resource.openStream(); fiore@3: out = new FileOutputStream(file); fiore@3: int byteRead; fiore@3: byte[] b = new byte[1024]; fiore@3: while((byteRead = in.read(b)) > 0){ fiore@3: out.write(b, 0, byteRead); fiore@3: } fiore@3: path = file.getAbsolutePath(); fiore@3: }catch(IOException ioe){ fiore@3: path = null; fiore@3: }finally{ fiore@3: if(in != null) fiore@3: try{in.close();}catch(IOException ioe){} fiore@3: if(out != null) fiore@3: try{out.close();}catch(IOException ioe){} fiore@3: } fiore@3: fiore@3: } fiore@3: fiore@3: /** fiore@3: * Returns the absolute path of the last written file. If the writing wasn't successfully fiore@3: * or no writing took place yet, then {@code null} is returned. fiore@3: * @return the path of the last written file or {@code null} fiore@3: */ fiore@3: public String getFilePath(){ fiore@3: return path; fiore@3: } fiore@3: fiore@3: private URL resource; fiore@3: private String path; fiore@3: }