1 package org.sapia.clazzy.loader; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.net.MalformedURLException ; 7 import java.net.URL ; 8 import java.util.jar.JarFile ; 9 import java.util.zip.ZipEntry ; 10 11 import org.sapia.clazzy.ClazzyURLStreamHandlerFactory; 12 import org.sapia.clazzy.URLEnabled; 13 import org.sapia.clazzy.Utils; 14 15 28 public class JarLoader implements Loader, URLEnabled{ 29 30 private File _jar; 31 private JarFile _file; 32 33 public JarLoader(File jar) { 34 _jar = jar; 35 try{ 36 _file = new JarFile (jar); 37 } catch (IOException e){ 38 } 40 } 41 42 public File getJarFile(){ 43 return _jar; 44 } 45 46 52 public URL getURL() throws MalformedURLException { 53 return _jar.toURL(); 54 } 55 56 59 public URL getURL(String resourceName) { 60 if(_file == null){ 61 return null; 62 } 63 ZipEntry entry = _file.getEntry(resourceName); 64 if(entry == null){ 65 return null; 66 } 67 try{ 68 return new URL (ClazzyURLStreamHandlerFactory.PROTOCOL, "", _jar.getAbsolutePath()+"?"+resourceName); 69 }catch(MalformedURLException e){ 70 return null; 71 } 72 } 73 74 77 public byte[] loadBytes(String resourceName) { 78 if(_file == null){ 79 return null; 80 } 81 InputStream entryStream = null; 82 try { 83 ZipEntry entry = _file.getEntry(resourceName); 84 if(entry == null) { 85 return null; 86 } 87 entryStream = _file.getInputStream(entry); 88 return Utils.streamToBytes(entryStream); 89 } catch (IOException e){ 90 return null; 91 } finally { 92 if(entryStream != null) { 93 try { 94 entryStream.close(); 95 } catch(IOException e) { 96 } 97 } 98 } 99 } 100 101 104 public void close(){ 105 if(_file != null){ 106 try{ 107 _file.close(); 108 }catch(IOException e){ 109 } 111 } 112 } 113 114 } 115 | Popular Tags |