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