| 1 36 package org.ungoverned.oscar; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.net.URL ; 42 import java.net.URLConnection ; 43 import java.security.Permission ; 44 45 import org.ungoverned.moduleloader.Module; 46 import org.ungoverned.moduleloader.ModuleManager; 47 import org.ungoverned.moduleloader.ResourceSource; 48 49 class BundleURLConnection extends URLConnection  50 { 51 private ModuleManager m_mgr = null; 52 private long id; 53 private int contentLength; 54 private long contentTime; 55 private String contentType; 56 private InputStream is; 57 58 public BundleURLConnection(ModuleManager mgr, URL url) 59 { 60 super(url); 61 m_mgr = mgr; 62 } 63 64 public void connect() throws IOException  65 { 66 if (!connected) 67 { 68 71 Module module = m_mgr.getModule(url.getHost()); 72 if (module == null) 73 { 74 throw new IOException ("Unable to find bundle's module."); 75 } 76 77 String resource = url.getFile(); 78 if (resource == null) 79 { 80 throw new IOException ("Unable to find resource: " + url.toString()); 81 } 82 if (resource.startsWith("/")) 83 { 84 resource = resource.substring(1); 85 } 86 int rsIdx = -1; 87 try 88 { 89 rsIdx = Integer.parseInt(resource.substring(0, resource.indexOf("/"))); 90 } 91 catch (NumberFormatException ex) 92 { 93 new IOException ("Error parsing resource index."); 94 } 95 resource = resource.substring(resource.indexOf("/") + 1); 96 97 byte[] bytes = null; 99 ResourceSource[] resSources = module.getResourceSources(); 100 if ((resSources != null) && (rsIdx < resSources.length)) 101 { 102 if (resSources[rsIdx].hasResource(resource)) 103 { 104 bytes = resSources[rsIdx].getBytes(resource); 105 } 106 } 107 108 if (bytes == null) 109 { 110 throw new IOException ("Unable to find resource: " + url.toString()); 111 } 112 113 is = new ByteArrayInputStream (bytes); 114 contentLength = bytes.length; 115 contentTime = 0L; contentType = URLConnection.guessContentTypeFromName(resource); 117 connected = true; 118 } 119 } 120 121 public InputStream getInputStream() 122 throws IOException  123 { 124 if (!connected) 125 { 126 connect(); 127 } 128 return is; 129 } 130 131 public int getContentLength() 132 { 133 if (!connected) 134 { 135 try { 136 connect(); 137 } catch(IOException ex) { 138 return -1; 139 } 140 } 141 return contentLength; 142 } 143 144 public long getLastModified() 145 { 146 if (!connected) 147 { 148 try { 149 connect(); 150 } catch(IOException ex) { 151 return 0; 152 } 153 } 154 if(contentTime != -1L) 155 return contentTime; 156 else 157 return 0L; 158 } 159 160 public String getContentType() 161 { 162 if (!connected) 163 { 164 try { 165 connect(); 166 } catch(IOException ex) { 167 return null; 168 } 169 } 170 return contentType; 171 } 172 173 public Permission getPermission() 174 { 175 return null; 181 } 182 } | Popular Tags |