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