1 28 package net.sf.jasperreports.engine.util; 29 30 import java.io.BufferedInputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.FileNotFoundException ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.ObjectInputStream ; 38 import java.net.MalformedURLException ; 39 import java.net.URL ; 40 import java.net.URLStreamHandlerFactory ; 41 42 import net.sf.jasperreports.engine.JRException; 43 44 45 49 public class JRLoader 50 { 51 52 53 56 58 59 62 public static Object loadObject(String fileName) throws JRException 63 { 64 return loadObject(new File (fileName)); 65 } 66 67 68 71 public static Object loadObject(File file) throws JRException 72 { 73 if (!file.exists() || !file.isFile()) 74 { 75 throw new JRException( new FileNotFoundException (String.valueOf(file)) ); 76 } 77 78 Object obj = null; 79 80 FileInputStream fis = null; 81 ObjectInputStream ois = null; 82 83 try 84 { 85 fis = new FileInputStream (file); 86 BufferedInputStream bufferedIn = new BufferedInputStream (fis); 87 ois = new ObjectInputStream (bufferedIn); 88 obj = ois.readObject(); 89 } 90 catch (IOException e) 91 { 92 throw new JRException("Error loading object from file : " + file, e); 93 } 94 catch (ClassNotFoundException e) 95 { 96 throw new JRException("Class not found when loading object from file : " + file, e); 97 } 98 finally 99 { 100 if (ois != null) 101 { 102 try 103 { 104 ois.close(); 105 } 106 catch(IOException e) 107 { 108 } 109 } 110 111 if (fis != null) 112 { 113 try 114 { 115 fis.close(); 116 } 117 catch(IOException e) 118 { 119 } 120 } 121 } 122 123 return obj; 124 } 125 126 127 130 public static Object loadObject(URL url) throws JRException 131 { 132 Object obj = null; 133 134 InputStream is = null; 135 ObjectInputStream ois = null; 136 137 try 138 { 139 is = url.openStream(); 140 ois = new ObjectInputStream (is); 141 obj = ois.readObject(); 142 } 143 catch (IOException e) 144 { 145 throw new JRException("Error loading object from URL : " + url, e); 146 } 147 catch (ClassNotFoundException e) 148 { 149 throw new JRException("Class not found when loading object from URL : " + url, e); 150 } 151 finally 152 { 153 if (ois != null) 154 { 155 try 156 { 157 ois.close(); 158 } 159 catch(IOException e) 160 { 161 } 162 } 163 164 if (is != null) 165 { 166 try 167 { 168 is.close(); 169 } 170 catch(IOException e) 171 { 172 } 173 } 174 } 175 176 return obj; 177 } 178 179 180 183 public static Object loadObject(InputStream is) throws JRException 184 { 185 Object obj = null; 186 187 ObjectInputStream ois = null; 188 189 try 190 { 191 ois = new ObjectInputStream (is); 192 obj = ois.readObject(); 193 } 194 catch (IOException e) 195 { 196 throw new JRException("Error loading object from InputStream", e); 197 } 198 catch (ClassNotFoundException e) 199 { 200 throw new JRException("Class not found when loading object from InputStream", e); 201 } 202 finally 203 { 204 if (ois != null) 206 { 207 try 208 { 209 ois.close(); 210 } 211 catch(IOException e) 212 { 213 } 214 } 215 } 216 217 return obj; 218 } 219 220 221 224 public static Object loadObjectFromLocation(String location) throws JRException 225 { 226 return loadObjectFromLocation(location, null, null); 227 } 228 229 230 233 public static Object loadObjectFromLocation(String location, ClassLoader classLoader) throws JRException 234 { 235 return loadObjectFromLocation(location, classLoader, null); 236 } 237 238 239 242 public static Object loadObjectFromLocation( 243 String location, 244 ClassLoader classLoader, 245 URLStreamHandlerFactory urlHandlerFactory 246 ) throws JRException 247 { 248 URL url = JRResourcesUtil.createURL(location, urlHandlerFactory); 249 if (url != null) 250 { 251 return loadObject(url); 252 } 253 254 File file = new File (location); 255 if (file.exists() && file.isFile()) 256 { 257 return loadObject(file); 258 } 259 260 url = JRResourcesUtil.findClassLoaderResource(location, classLoader, JRLoader.class); 261 if (url != null) 262 { 263 return loadObject(url); 264 } 265 266 throw new JRException("Could not load object from location : " + location); 267 } 268 269 270 273 public static byte[] loadBytes(File file) throws JRException 274 { 275 ByteArrayOutputStream baos = null; 276 FileInputStream fis = null; 277 278 try 279 { 280 fis = new FileInputStream (file); 281 baos = new ByteArrayOutputStream (); 282 283 byte[] bytes = new byte[10000]; 284 int ln = 0; 285 while ((ln = fis.read(bytes)) > 0) 286 { 287 baos.write(bytes, 0, ln); 288 } 289 290 baos.flush(); 291 } 292 catch (IOException e) 293 { 294 throw new JRException("Error loading byte data : " + file, e); 295 } 296 finally 297 { 298 if (baos != null) 299 { 300 try 301 { 302 baos.close(); 303 } 304 catch(IOException e) 305 { 306 } 307 } 308 309 if (fis != null) 310 { 311 try 312 { 313 fis.close(); 314 } 315 catch(IOException e) 316 { 317 } 318 } 319 } 320 321 return baos.toByteArray(); 322 } 323 324 325 328 public static byte[] loadBytes(URL url) throws JRException 329 { 330 ByteArrayOutputStream baos = null; 331 InputStream is = null; 332 333 try 334 { 335 is = url.openStream(); 336 baos = new ByteArrayOutputStream (); 337 338 byte[] bytes = new byte[10000]; 339 int ln = 0; 340 while ((ln = is.read(bytes)) > 0) 341 { 342 baos.write(bytes, 0, ln); 343 } 344 345 baos.flush(); 346 } 347 catch (IOException e) 348 { 349 throw new JRException("Error loading byte data : " + url, e); 350 } 351 finally 352 { 353 if (baos != null) 354 { 355 try 356 { 357 baos.close(); 358 } 359 catch(IOException e) 360 { 361 } 362 } 363 364 if (is != null) 365 { 366 try 367 { 368 is.close(); 369 } 370 catch(IOException e) 371 { 372 } 373 } 374 } 375 376 return baos.toByteArray(); 377 } 378 379 380 383 public static byte[] loadBytes(InputStream is) throws JRException 384 { 385 ByteArrayOutputStream baos = null; 386 387 try 388 { 389 baos = new ByteArrayOutputStream (); 390 391 byte[] bytes = new byte[10000]; 392 int ln = 0; 393 while ((ln = is.read(bytes)) > 0) 394 { 395 baos.write(bytes, 0, ln); 396 } 397 398 baos.flush(); 399 } 400 catch (IOException e) 401 { 402 throw new JRException("Error loading byte data from input stream.", e); 403 } 404 finally 405 { 406 if (baos != null) 407 { 408 try 409 { 410 baos.close(); 411 } 412 catch(IOException e) 413 { 414 } 415 } 416 } 417 418 return baos.toByteArray(); 419 } 420 421 422 425 public static byte[] loadBytesFromLocation(String location) throws JRException 426 { 427 return loadBytesFromLocation(location, null, null); 428 } 429 430 431 434 public static byte[] loadBytesFromLocation(String location, ClassLoader classLoader) throws JRException 435 { 436 return loadBytesFromLocation(location, classLoader, null); 437 } 438 439 440 443 public static byte[] loadBytesFromLocation( 444 String location, 445 ClassLoader classLoader, 446 URLStreamHandlerFactory urlHandlerFactory 447 ) throws JRException 448 { 449 URL url = JRResourcesUtil.createURL(location, urlHandlerFactory); 450 if (url != null) 451 { 452 return loadBytes(url); 453 } 454 455 File file = new File (location); 456 if (file.exists() && file.isFile()) 457 { 458 return loadBytes(file); 459 } 460 461 url = JRResourcesUtil.findClassLoaderResource(location, classLoader, JRLoader.class); 462 if (url != null) 463 { 464 return loadBytes(url); 465 } 466 467 throw new JRException("Byte data not found at location : " + location); 468 } 469 470 471 483 public static InputStream getLocationInputStream(String location) throws JRException 484 { 485 InputStream is = null; 486 487 is = getResourceInputStream(location); 488 489 if (is == null) 490 { 491 is = getFileInputStream(location); 492 } 493 494 if (is == null) 495 { 496 is = getURLInputStream(location); 497 } 498 499 return is; 500 } 501 502 503 510 public static InputStream getFileInputStream(String filename) throws JRException 511 { 512 InputStream is = null; 513 514 File file = new File (filename); 515 if (file.exists() && file.isFile()) 516 { 517 try 518 { 519 is = new FileInputStream (file); 520 } 521 catch (FileNotFoundException e) 522 { 523 throw new JRException("Error opening file " + filename); 524 } 525 } 526 527 return is; 528 } 529 530 531 537 public static InputStream getResourceInputStream(String resource) 538 { 539 InputStream is = null; 540 541 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 542 if (classLoader != null) 543 { 544 is = classLoader.getResourceAsStream(resource); 545 } 546 547 if (is == null) 548 { 549 classLoader = JRLoader.class.getClassLoader(); 550 if (classLoader != null) 551 { 552 is = classLoader.getResourceAsStream(resource); 553 } 554 555 if (is == null) 556 { 557 is = JRProperties.class.getResourceAsStream("/" + resource); 558 } 559 } 560 561 return is; 562 } 563 564 565 572 public static InputStream getURLInputStream(String spec) throws JRException 573 { 574 InputStream is = null; 575 576 try 577 { 578 URL url = new URL (spec); 579 is = url.openStream(); 580 } 581 catch (MalformedURLException e) 582 { 583 } 584 catch (IOException e) 585 { 586 throw new JRException("Error opening URL " + spec); 587 } 588 589 return is; 590 } 591 } 592 | Popular Tags |