| 1 28 29 package com.idaremedia.antx.helpers; 30 31 import java.io.BufferedInputStream ; 32 import java.io.File ; 33 import java.io.FileNotFoundException ; 34 import java.io.FileOutputStream ; 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 import java.net.URL ; 38 import java.net.URLConnection ; 39 import java.util.Properties ; 40 41 50 51 public class InputFileLoader 52 { 53 56 private static final int BUFSIZ=8*1024; 57 58 59 62 public static final int NOLIMIT= Integer.MIN_VALUE; 63 64 65 68 public static final boolean NOCACHE= false; 69 70 71 72 75 public InputFileLoader() 76 { 77 this(0); 78 } 79 80 81 84 public InputFileLoader(int bufsiz) 85 { 86 if (bufsiz<=0) { 87 bufsiz=BUFSIZ; 88 } 89 m_transferBuffer= new byte[bufsiz]; 90 } 91 92 93 101 public final byte[] load(InputStream ins, int limit) 102 throws IOException  103 { 104 verify_(ins!=null,"load- nonzro inputstrm"); 105 byte[] bytes; 106 107 synchronized(m_transferBuffer) { 108 BufferedInputStream bins; 109 110 if (ins instanceof BufferedInputStream ) { 111 bins= (BufferedInputStream )ins; 112 } else { 113 bins= new BufferedInputStream (ins); 114 } 115 bytes = transferBytes(bins,limit); 116 bins = null; 117 } 118 119 return bytes; 120 } 121 122 123 129 public final byte[] load(InputStream ins) 130 throws IOException  131 { 132 return load(ins,NOLIMIT); 133 } 134 135 136 146 public final byte[] loadURL(URL url, int limit, boolean cacheOK) 147 throws IOException  148 { 149 verify_(url!=null,"loadURL- nonzro URL"); 150 byte[] bytes; 151 152 synchronized(m_transferBuffer) { 155 try { 156 URLConnection urlc = url.openConnection(); 157 urlc.setDefaultUseCaches(cacheOK); 158 urlc.setAllowUserInteraction(false); 159 160 BufferedInputStream ins = new BufferedInputStream (urlc.getInputStream()); 161 bytes = transferBytes(ins,limit); 162 163 Tk.closeQuietly(ins); 164 ins = null; 165 urlc= null; 166 } 167 catch(Exception anyx) { 168 if (anyx instanceof IOException ) { 169 throw (IOException )anyx; 170 } 171 throw new IOException ("Unable to load: "+url+"("+anyx.getMessage()+")"); } 173 } 175 return bytes; 176 } 177 178 179 180 189 public final byte[] loadURL(URL url, int limit) 190 throws IOException  191 { 192 return loadURL(url,limit,!NOCACHE); 193 } 194 195 196 197 203 public final byte[] loadURL(URL url) 204 throws IOException  205 { 206 return loadURL(url,NOLIMIT,!NOCACHE); 207 } 208 209 210 211 212 222 public final void loadURLToFile(URLConnection urlc, File file, int limit) 223 throws IOException  224 { 225 verify_(urlc!=null && file!=null,"loadURLToFile- nonzro URLcon and file"); 226 227 synchronized(m_transferBuffer) { 230 try { 231 BufferedInputStream ins = new BufferedInputStream (urlc.getInputStream()); 232 transferBytes(ins,limit,file); 233 Tk.closeQuietly(ins); 234 ins = null; 235 } 236 catch(Exception anyx) { 237 if (anyx instanceof IOException ) { 238 throw (IOException )anyx; 239 } 240 throw new IOException ("Unable to load to stream: "+urlc.getURL()+ 241 "("+anyx.getMessage()+")"); 242 } 243 } } 245 246 247 248 257 public final void loadURLToFile(URLConnection urlc, File file) 258 throws IOException  259 { 260 loadURLToFile(urlc,file,NOLIMIT); 261 } 262 263 264 265 276 public final void loadURLToFile(URL url, File file, int limit, boolean cacheOK) 277 throws IOException  278 { 279 verify_(url!=null && file!=null,"loadURLToFile- nonzro URL and file"); 280 281 synchronized(m_transferBuffer) { 284 try { 285 URLConnection urlc = url.openConnection(); 286 urlc.setDefaultUseCaches(cacheOK); 287 urlc.setAllowUserInteraction(false); 288 289 BufferedInputStream ins = new BufferedInputStream (urlc.getInputStream()); 290 transferBytes(ins,limit,file); 291 292 Tk.closeQuietly(ins); 293 ins = null; 294 urlc= null; 295 } 296 catch(Exception anyx) { 297 if (anyx instanceof IOException ) { 298 throw (IOException )anyx; 299 } 300 throw new IOException ("Unable to load to file: "+url+"("+anyx.getMessage()+")"); 301 } 302 } } 304 305 306 307 317 public final void loadURLToFile(URL url, File file, int limit) 318 throws IOException  319 { 320 loadURLToFile(url,file,limit,!NOCACHE); 321 } 322 323 324 325 333 public final void loadURLToFile(URL url, File file) 334 throws IOException  335 { 336 loadURLToFile(url,file,NOLIMIT,!NOCACHE); 337 } 338 339 340 341 350 public byte[] loadFile(String descriptor, int limit) 351 throws IOException  352 { 353 verify_(!Tk.isWhitespace(descriptor), "loadFil- nonzro descrip"); 354 355 URL url=null; 356 if (descriptor.indexOf("://")>0) { 357 url= new URL (descriptor); 358 } 359 else if (descriptor.toLowerCase().startsWith("file:") || 360 descriptor.toLowerCase().startsWith("jar:")) { 361 } 364 else { 365 File f= new File (descriptor); 366 try { 367 if (f.canRead()) { 368 url= f.toURL(); 369 } else { 370 throw new FileNotFoundException (f.getPath()); 371 } 372 } catch(SecurityException secx) { 373 throw new IOException ("Security exception caught: "+secx.getMessage()); 374 } 375 } 376 verify_(url!=null,"lod- nonzro url"); 377 return loadURL(url,limit,!NOCACHE); 378 } 379 380 381 388 public byte[] loadFile(String descriptor) 389 throws IOException  390 { 391 return loadFile(descriptor,NOLIMIT); 392 } 393 394 395 396 404 public byte[] loadResource(String resource, Class forClass) 405 throws IOException  406 { 407 verify_(!Tk.isWhitespace(resource), "loadRez- nonzro resname"); 408 verify_(forClass!=null,"loadRez- nonzro clas"); 409 410 InputStream rsrc = forClass.getResourceAsStream(resource); 411 if (rsrc==null) { 412 rsrc = ClassLoader.getSystemResourceAsStream(resource); 413 } 414 if (rsrc==null) { 415 throw new FileNotFoundException ("Class= "+forClass.getName()+", Resource= "+resource); 416 } 417 418 try { 419 return load(rsrc,NOLIMIT); 420 } finally { 421 Tk.closeQuietly(rsrc); 422 } 423 } 424 425 426 435 public final Properties loadProperties(URL url, Properties properties, boolean cacheOK) 436 throws IOException  437 { 438 verify_(url!=null,"loadProps- nonzro URL"); 439 440 if (properties==null) { 441 properties = new Properties (); 442 } 443 444 try { 445 URLConnection urlc = url.openConnection(); 446 urlc.setDefaultUseCaches(cacheOK); 447 urlc.setAllowUserInteraction(false); 448 449 properties.load(urlc.getInputStream()); 450 urlc= null; 451 } 452 catch(Exception anyx) { 453 if (anyx instanceof IOException ) { 454 throw (IOException )anyx; 455 } 456 throw new IOException ("Unable to load Properties: "+url+"("+anyx.getMessage()+")"); 457 } 458 459 return properties; 460 } 461 462 463 467 private byte[] transferBytes(BufferedInputStream ins, int limit) 468 throws IOException  469 { 470 byte[] bytes=null; 471 int cc; 472 473 final int maxChars = m_transferBuffer.length; 476 if (limit<=0) { 477 limit = Integer.MAX_VALUE-1; 478 } 479 while ((cc=ins.read(m_transferBuffer,0,maxChars))!= -1) { 480 if (cc==0) { continue; 482 } 483 if (bytes==null) { 484 bytes= new byte[cc]; 485 System.arraycopy(m_transferBuffer,0,bytes,0,cc); 486 } else { 487 int newcc= cc+bytes.length; 488 byte[] newbytes= new byte[newcc]; 489 System.arraycopy(bytes,0,newbytes,0,bytes.length); 490 bytes=null; 491 System.arraycopy(m_transferBuffer,0,newbytes,newcc-cc,cc); 492 bytes= newbytes; 493 } 494 if (bytes.length>=limit) { 495 break; 496 } 497 } 498 if (bytes==null) { 499 bytes = new byte[0]; 500 } 501 return bytes; 502 } 503 504 505 506 507 512 private void transferBytes(BufferedInputStream ins, int limit, File output) 513 throws IOException  514 { 515 FileOutputStream fos = new FileOutputStream (output); 516 int cc, cctotal=0; 517 518 final int maxChars = m_transferBuffer.length; 521 if (limit<=0) { 522 limit = Integer.MAX_VALUE-1; 523 } 524 try { 525 while ((cc=ins.read(m_transferBuffer,0,maxChars))!= -1) { 526 if (cc==0) { continue; 528 } 529 fos.write(m_transferBuffer,0,cc); 530 fos.flush(); 531 cctotal += cc; 532 if (cctotal>=limit) { 533 break; 534 } 535 } 536 } finally { 537 Tk.closeQuietly(fos); 538 } 539 } 540 541 542 543 544 549 public final static String loadString(URL url) 550 throws IOException  551 { 552 InputFileLoader fl= new InputFileLoader(); 553 byte[] bytes = fl.loadURL(url,NOLIMIT,NOCACHE); 554 String s = bytes.length>0 ? new String (bytes) : ""; 555 fl.dispose(); 556 fl= null; 557 bytes= null; 558 return s; 559 } 560 561 562 567 public final static String loadString(String filename) 568 throws IOException  569 { 570 InputFileLoader fl= new InputFileLoader(); 571 byte[] bytes = fl.loadFile(filename); 572 String s = bytes.length>0 ? new String (bytes) : ""; 573 fl.dispose(); 574 fl= null; 575 bytes= null; 576 return s; 577 } 578 579 580 585 public final static String loadString(File file) 586 throws IOException  587 { 588 return loadString(file.getAbsolutePath()); 589 } 590 591 592 598 public final static String loadString(String resource, Class forClass) 599 throws IOException  600 { 601 InputFileLoader fl= new InputFileLoader(); 602 byte[] bytes = fl.loadResource(resource, forClass); 603 String s = bytes.length>0 ? new String (bytes) : ""; 604 fl.dispose(); 605 fl= null; 606 bytes= null; 607 return s; 608 } 609 610 611 617 public final static Properties loadProperties(URL url, Properties p) 618 throws IOException  619 { 620 return new InputFileLoader().loadProperties(url,p,NOCACHE); 621 } 622 623 624 628 public void dispose() 629 { 630 m_transferBuffer = null; 631 } 632 633 634 637 private void verify_(boolean c, String msg) 638 { 639 if (!c) { 640 throw new IllegalArgumentException ("REQUIRE: InputFileLoader: "+msg); 641 } 642 } 643 644 645 private byte[] m_transferBuffer;} 647 648 649 | Popular Tags |