annotate java/src/uk/ac/qmul/eecs/ccmi/utils/ResourceFileWriter.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19
f@0 20 package uk.ac.qmul.eecs.ccmi.utils;
f@0 21
f@0 22 import java.io.File;
f@0 23 import java.io.FileOutputStream;
f@0 24 import java.io.IOException;
f@0 25 import java.io.InputStream;
f@0 26 import java.net.URL;
f@0 27
f@0 28 /**
f@0 29 * This class is used to store a resource (e.g. .dll file in windows)to the local file system.
f@0 30 *
f@0 31 * This class can be used to allow the virtual machine to load resources which come embedded
f@0 32 * into a jar file.
f@0 33 */
f@0 34 public class ResourceFileWriter {
f@0 35
f@0 36 /**
f@0 37 * Creates an instance of the the class linked to no resource. an resource file writer
f@0 38 * created with this constructor is useless, unless {@code setResource} is called before
f@0 39 * writing the resource on the disk.
f@0 40 *
f@0 41 * @see Class#getResource(String)
f@0 42 */
f@0 43 public ResourceFileWriter(){
f@0 44 }
f@0 45
f@0 46 /**
f@0 47 * Creates an instance of the the class linked to a specific resource
f@0 48 *
f@0 49 * @param resource the resource URL. The URL can be obtained by
f@0 50 * {@code getResource}, therefore can be called from a class within a jar file
f@0 51 * which needs to access embedded resources.
f@0 52 */
f@0 53 public ResourceFileWriter(URL resource){
f@0 54 this.resource = resource;
f@0 55 }
f@0 56
f@0 57 /**
f@0 58 * Sets a new resource for this resource file writer. Successive calls to {@code writeToDisk}
f@0 59 * will store the resouce passed as argument in the file system.
f@0 60 *
f@0 61 * @param resource the URL of the new resource this resource file writer is linked to
f@0 62 */
f@0 63 public void setResource(URL resource){
f@0 64 this.resource = resource;;
f@0 65 path = null;
f@0 66 }
f@0 67
f@0 68 /**
f@0 69 * Writes the resource in the file passed as argument.
f@0 70 *
f@0 71 * The path to the file can be retrieved afterwards through @see {@link #getFilePath()}
f@0 72 *
f@0 73 * @param dir the directory where the resource will be saved
f@0 74 * @param fileName the name of the file the resoruce will be saved in
f@0 75 */
f@0 76 public void writeOnDisk(String dir,String fileName){
f@0 77 if (resource == null)
f@0 78 return;
f@0 79 InputStream in = null;
f@0 80 FileOutputStream out = null;
f@0 81 File file = new File(dir,fileName);
f@0 82 if(file.exists()){ //if file already exists. no job needs to be done.
f@0 83 path = file.getAbsolutePath();
f@0 84 return;
f@0 85 }
f@0 86 try{
f@0 87 in = resource.openStream();
f@0 88 out = new FileOutputStream(file);
f@0 89 int byteRead;
f@0 90 byte[] b = new byte[1024];
f@0 91 while((byteRead = in.read(b)) > 0){
f@0 92 out.write(b, 0, byteRead);
f@0 93 }
f@0 94 path = file.getAbsolutePath();
f@0 95 }catch(IOException ioe){
f@0 96 path = null;
f@0 97 }finally{
f@0 98 if(in != null)
f@0 99 try{in.close();}catch(IOException ioe){ioe.printStackTrace();}
f@0 100 if(out != null)
f@0 101 try{out.close();}catch(IOException ioe){ioe.printStackTrace();}
f@0 102 }
f@0 103
f@0 104 }
f@0 105
f@0 106 /**
f@0 107 * Returns the absolute path of the last written file. If the writing wasn't successfully
f@0 108 * or no writing took place yet, then {@code null} is returned.
f@0 109 *
f@0 110 * @return the path of the last written file or {@code null} if no resource was set for this
f@0 111 * resource file writer or {@code writeToDisk} was unsuccessful
f@0 112 */
f@0 113 public String getFilePath(){
f@0 114 return path;
f@0 115 }
f@0 116
f@0 117 private URL resource;
f@0 118 private String path;
f@0 119 }