1 36 package org.ungoverned.moduleloader; 37 38 import java.io.ByteArrayOutputStream ; 39 import java.io.InputStream ; 40 41 50 public class SystemResourceSource implements ResourceSource 51 { 52 private static final int BUFSIZE = 4096; 53 private boolean m_opened = false; 54 55 public void open() 56 { 57 m_opened = true; 58 } 59 60 public void close() 61 { 62 m_opened = false; 63 } 64 65 public boolean hasResource(String name) throws IllegalStateException 66 { 67 if (!m_opened) 68 { 69 throw new IllegalStateException ("SystemResourceSource is not open"); 70 } 71 return (ClassLoader.getSystemClassLoader().getResource(name) != null); 72 } 73 74 public byte[] getBytes(String name) throws IllegalStateException 75 { 76 if (!m_opened) 77 { 78 throw new IllegalStateException ("SystemResourceSource is not open"); 79 } 80 InputStream is = null; 81 ByteArrayOutputStream baos = null; 82 83 try { 84 ClassLoader loader = ClassLoader.getSystemClassLoader(); 85 is = loader.getResourceAsStream(name); 86 if (is == null) 87 { 88 return null; 89 } 90 baos = new ByteArrayOutputStream (BUFSIZE); 91 byte[] buf = new byte[BUFSIZE]; 92 int n = 0; 93 while ((n = is.read(buf, 0, buf.length)) >= 0) 94 { 95 baos.write(buf, 0, n); 96 } 97 return baos.toByteArray(); 98 } catch (Exception ex) { 99 return null; 100 } finally { 101 try { 102 if (baos != null) baos.close(); 103 } catch (Exception ex) { 104 } 105 try { 106 if (is != null) is.close(); 107 } catch (Exception ex) { 108 } 109 } 110 } 111 } | Popular Tags |