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