1 25 26 package net.yagga.util; 27 28 import javax.swing.*; 29 import java.io.*; 30 import java.net.*; 31 32 95 public class ResourceMgr extends ClassLoader { 96 97 98 public static boolean debug=false; 99 100 103 private static MetaJarResources mjr=null; 104 105 127 static public ImageIcon retrieveImageIcon(String image) { 128 129 if(debug) 131 Ut.infoln("Try with URL getResource with /"); 132 URL u=ResourceMgr.class.getResource("/"+image); 133 ImageIcon ii=null; 134 if(u!=null){ 135 ii=new ImageIcon(u); 136 if(ii!=null) 137 return ii; 138 } 139 140 try{ 141 if(debug) 142 Ut.infoln("Try with openResource..."); 143 InputStream is=openResource(image); 144 145 int c=-1; 146 byte buf[]=new byte[0]; 147 while((c=is.available())>0){ 148 byte b[]=new byte[c]; 149 is.read(b); 150 byte tmp[]=new byte[buf.length+b.length]; 151 System.arraycopy(buf,0,tmp,0,buf.length); 152 System.arraycopy(b,0,tmp,buf.length,b.length); 153 buf=tmp; 154 } 155 if(debug){ 156 System.err.println("READ "+buf.length+"BYTES"); 157 } 158 159 ii=new ImageIcon(buf); 160 if(ii!=null) 161 return ii; 162 }catch(IOException ioe){ 163 if(debug) Ut.error("Error reading image bytes "+ioe.getMessage()); 164 } 165 if(debug) Ut.error("error retieving ImageIcon" ); 166 return null; 167 } 168 169 static public ImageIcon retrieveImageIcon2(String image) { 170 try{ 171 if(debug) 172 Ut.infoln("Try 2 with getResource with /"); 173 ImageIcon ii=new ImageIcon(ResourceMgr.class.getResource("/"+image)); 174 ii.getIconHeight(); 175 return ii; 176 }catch(NullPointerException npe){ 177 try{ 178 if(debug) 179 Ut.infoln("Try 2 with MetaJarClassLoader"); 180 if(mjr==null){ 181 ResourceMgr rm=new ResourceMgr(); 183 ClassLoader cl=ResourceMgr.class.getClassLoader(); 184 String jarFile=""; 185 if(cl instanceof BootstrapJarClassLoader){ 186 jarFile=((BootstrapJarClassLoader)cl).getActualJarName(); 187 Ut.infoln("BootstrapJarClassLoader: retrieving image:'"+image+"'"); 188 } 189 mjr = new MetaJarResources(jarFile); 190 } 191 byte b[]=mjr.getBytes(image); 192 ImageIcon ii=new ImageIcon(b); 193 194 ii.getIconHeight(); 196 return ii; 198 }catch(NullPointerException npe2){ 199 Ut.error("RM1b: retrieving image:'"+image+"'"); 200 } 201 } 202 return null; 203 } 204 205 206 218 public static String retrieveFile(String fileName){ 219 String file="",line=""; 220 try{ 221 Reader r=new InputStreamReader(openResource(fileName)); 222 BufferedReader read=new BufferedReader(r); 223 while((line=read.readLine())!=null){ 224 file+=line+Ut.nl; 225 } 226 r.close(); 227 } 228 catch(IOException ioe){ 229 Ut.error("RM1: reading (resource) file '"+fileName+"':"+ioe); 230 return ""; 231 } 232 return file; 233 } 234 235 236 237 250 static public InputStream openResource(String filename) 251 { 252 253 try 254 { 255 if(debug) 256 Ut.infoln("read as simple resource with /"); 257 258 259 InputStream is=ResourceMgr.class.getResourceAsStream("/"+filename); 260 is.available(); return is; 262 } 263 catch (java.io.IOException e) 264 { 265 Ut.error("RM1: file not found:"+filename); 266 return null; 267 } 268 catch(NullPointerException e2){ 269 if(debug) 270 Ut.infoln("read as simple resource w/o /"); 271 InputStream is= ResourceMgr.class.getResourceAsStream(filename); 273 if(is==null) 274 { 275 try{ 276 if(debug) 277 Ut.infoln("read as file"); 278 is= new FileInputStream(filename); 279 } 280 catch(FileNotFoundException fnfe) 281 { 282 if(debug) 283 Ut.infoln("Now read bytes from class loader"); 284 285 try{ 286 if(mjr==null){ 287 ResourceMgr rm=new ResourceMgr(); 289 ClassLoader cl=ResourceMgr.class.getClassLoader(); 290 String jarFile=""; 291 if(cl instanceof BootstrapJarClassLoader) 292 jarFile=((BootstrapJarClassLoader)cl).getActualJarName(); 293 mjr = new MetaJarResources(jarFile); 294 } 295 byte b[]=mjr.getBytes(filename); 296 298 is=new ByteArrayInputStream(b); 299 300 is.available(); 302 return is; 304 }catch(IOException npe2){ 305 Ut.error("RM1cb: retrieving stream:'"+filename+"'"); 306 }catch(NullPointerException npe2){ 307 Ut.error("RM1b: retrieving stream:'"+filename+"'"); 308 } 309 try{ 313 is=Runtime.getRuntime().getClass().getResourceAsStream(filename); 314 if(is==null) 315 is=Runtime.getRuntime().getClass().getResourceAsStream("/"+filename); 316 is.available(); 317 return is; 318 }catch(IOException ioe){ 319 Ut.error("RM1c: retrieving local stream:'"+filename+"'"); 320 }catch(NullPointerException npe2){ 321 Ut.error("RM1c1: retrieving stream:'"+filename+"'"); 322 } 323 catch(Exception e){ 324 Ut.error("RM1c3: retrieving stream:'"+filename+"'"); 325 } 326 327 return null; 329 } 330 return is; 331 } 332 else 333 return is; 334 } 335 } 337 338 public static Class retrieveClass(String className) throws ClassNotFoundException 339 { 340 ResourceMgr rm=new ResourceMgr(); 341 return rm.findClass(className); 342 } 343 344 352 protected Class findClass(String className) throws ClassNotFoundException 353 { 354 try{ 356 return Class.forName(className); 357 }catch(ClassNotFoundException cnfe){ 358 if(debug) 359 Ut.infoln("cant' find in simple way"); 360 } 361 362 String urlName=className.replace('.', '/') + ".class"; 363 374 try{ 387 InputStream is=openResource(urlName); 389 int c=-1; 390 byte buf[]=new byte[0]; 391 while((c=is.available())>0){ 392 byte b[]=new byte[c]; 393 is.read(b); 394 byte tmp[]=new byte[buf.length+b.length]; 395 System.arraycopy(buf,0,tmp,0,buf.length); 396 System.arraycopy(b,0,tmp,buf.length,b.length); 397 buf=tmp; 398 } 399 return defineClass(className,buf,0,buf.length); 400 } 401 catch(IOException ioe){ 402 Ut.error("Errore IO:"+ioe); 403 }catch(ClassFormatError cfe){ 404 Ut.error("'"+className+"':"+cfe); 405 } 406 throw new ClassNotFoundException ("Can't find class '"+className+"'"); 407 } 408 421 public static String toHexString(byte[] orig, String prefix, String newLineSep, int limit){ 422 StringBuffer sb=new StringBuffer (); 423 int len=limit> orig.length ? orig.length : limit; 424 for(int i=0;i<len;i++){ 425 byte c=orig[i]; 426 sb.append(prefix).append("'").append(c).append("' = ").append(Integer.toHexString(c)).append(newLineSep); 427 } 428 return sb.toString(); 429 } 430 431 } | Popular Tags |