1 package org.sapia.clazzy.loader; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.net.MalformedURLException ; 8 import java.net.URL ; 9 10 import org.sapia.clazzy.URLEnabled; 11 import org.sapia.clazzy.Utils; 12 13 24 public class FileSystemLoader implements Loader, URLEnabled{ 25 26 private File _baseDir; 27 28 public FileSystemLoader(File baseDir){ 29 _baseDir = baseDir; 30 } 31 32 public File getBaseDir(){ 33 return _baseDir; 34 } 35 36 42 public URL getURL() throws MalformedURLException { 43 return _baseDir.toURL(); 44 } 45 46 49 public URL getURL(String resourceName) { 50 try{ 51 File toReturn = new File (_baseDir, resourceName); 52 if(toReturn.exists() && toReturn.isFile()){ 53 return toReturn.toURL(); 54 } 55 return null; 56 }catch(MalformedURLException e){ 57 return null; 58 } 59 } 60 61 64 public byte[] loadBytes(String resourceName){ 65 File toLoad = new File (_baseDir, resourceName); 66 if(!toLoad.exists()){ 67 return null; 68 } 69 InputStream byteStream = null; 70 try { 71 byteStream = new FileInputStream (toLoad); 72 return Utils.streamToBytes(byteStream); 73 } catch(IOException e) { 74 return null; 75 } finally { 76 if(byteStream != null) { 77 try { 78 byteStream.close(); 79 } catch(IOException e) { 80 } 81 } 82 } 83 } 84 85 88 public void close() { 89 } 90 } 91 | Popular Tags |