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