view 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
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 ResourceFileWriter {
	
	/**
	 * 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 ResourceFileWriter(URL resource){
		this.resource = resource;
	}
	
	public void serResource(URL resource){
		this.resource = resource;;
		path = null;
	}

	/**
	 * 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 dir,String fileName){
		if (resource == null) 
			return;
		InputStream in = null;
		FileOutputStream out = null;
		File file = new File(dir,fileName);
		if(file.exists()){ //if file already exists. no job needs to be done.
			path = file.getAbsolutePath();
			return;
		}
		try{
			in = resource.openStream();
			out = new FileOutputStream(file);
			int byteRead;
			byte[] b = new byte[1024];
			while((byteRead = in.read(b)) > 0){
				out.write(b, 0, byteRead);
			}
			path = file.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){}	
		}
		
	}
	
	/**
	 * Returns the absolute path of the last written file. If the writing wasn't successfully 
	 * or no writing took place yet, then {@code null} is returned. 
	 * @return the path of the last written file or {@code null}
	 */
	public String getFilePath(){
		return path;
	}
	
	private URL resource;
	private String path;
}