Mercurial > hg > ccmieditor
view java/src/uk/ac/qmul/eecs/ccmi/utils/ResourceFileWriter.java @ 5:d66dd5880081
Added support for Falcon Haptic device and Tablet/Mouse as haptic device
author | Fiore Martin <fiore@eecs.qmul.ac.uk> |
---|---|
date | Tue, 10 Jul 2012 22:39:37 +0100 |
parents | 9e67171477bc |
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 store a resource (e.g. .dll file in windows)to the local file system. * * This class can be used to allow the virtual machine to load resources which come embedded * into a jar file. */ public class ResourceFileWriter { /** * Creates an instance of the the class linked to no resource. an resource file writer * created with this constructor is useless, unless {@code setResource} is called before * writing the resource on the disk. * * @see Class#getResource(String) */ public ResourceFileWriter(){ } /** * Creates an instance of the the class linked to a specific resource * * @param resource the resource URL. The URL can be obtained by * {@code getResource}, therefore can be called from a class within a jar file * which needs to access embedded resources. */ public ResourceFileWriter(URL resource){ this.resource = resource; } /** * Sets a new resource for this resource file writer. Successive calls to {@code writeToDisk} * will store the resouce passed as argument in the file system. * * @param resource the URL of the new resource this resource file writer is linked to */ public void setResource(URL resource){ this.resource = resource;; path = null; } /** * Writes the resource in the file passed as argument. * * The path to the file can be retrieved afterwards through @see {@link #getFilePath()} * * @param dir the directory where the resource will be saved * @param fileName the name of the file the resoruce will be saved in */ public void writeOnDisk(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){ioe.printStackTrace();} if(out != null) try{out.close();}catch(IOException ioe){ioe.printStackTrace();} } } /** * 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} if no resource was set for this * resource file writer or {@code writeToDisk} was unsuccessful */ public String getFilePath(){ return path; } private URL resource; private String path; }