| 1 7 8 package javax.imageio; 9 10 import java.awt.image.BufferedImage ; 11 import java.awt.image.RenderedImage ; 12 import java.io.File ; 13 import java.io.InputStream ; 14 import java.io.IOException ; 15 import java.io.OutputStream ; 16 import java.lang.reflect.Method ; 17 import java.net.URL ; 18 import java.security.AccessController ; 19 import java.util.Arrays ; 20 import java.util.ArrayList ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.NoSuchElementException ; 24 import java.util.Set ; 25 import javax.imageio.spi.IIORegistry ; 26 import javax.imageio.spi.ImageReaderSpi ; 27 import javax.imageio.spi.ImageWriterSpi ; 28 import javax.imageio.spi.ImageInputStreamSpi ; 29 import javax.imageio.spi.ImageOutputStreamSpi ; 30 import javax.imageio.spi.ImageTranscoderSpi ; 31 import javax.imageio.spi.ServiceRegistry ; 32 import javax.imageio.stream.ImageInputStream ; 33 import javax.imageio.stream.ImageOutputStream ; 34 import sun.awt.AppContext; 35 import sun.security.action.GetPropertyAction; 36 37 44 public final class ImageIO { 45 46 private static final IIORegistry theRegistry = 47 IIORegistry.getDefaultInstance(); 48 49 52 private ImageIO() {} 53 54 90 public static void scanForPlugins() { 91 theRegistry.registerApplicationClasspathSpis(); 92 } 93 94 96 101 static class CacheInfo { 102 boolean useCache = true; 103 File cacheDirectory = null; 104 Boolean hasPermission = null; 105 106 public CacheInfo() {} 107 108 public boolean getUseCache() { 109 return useCache; 110 } 111 112 public void setUseCache(boolean useCache) { 113 this.useCache = useCache; 114 } 115 116 public File getCacheDirectory() { 117 return cacheDirectory; 118 } 119 120 public void setCacheDirectory(File cacheDirectory) { 121 this.cacheDirectory = cacheDirectory; 122 } 123 124 public Boolean getHasPermission() { 125 return hasPermission; 126 } 127 128 public void setHasPermission(Boolean hasPermission) { 129 this.hasPermission = hasPermission; 130 } 131 } 132 133 137 private static synchronized CacheInfo getCacheInfo() { 138 AppContext context = AppContext.getAppContext(); 139 CacheInfo info = (CacheInfo)context.get(CacheInfo.class); 140 if (info == null) { 141 info = new CacheInfo(); 142 context.put(CacheInfo.class, info); 143 } 144 return info; 145 } 146 147 151 private static String getTempDir() { 152 GetPropertyAction a = new GetPropertyAction("java.io.tmpdir"); 153 return (String )AccessController.doPrivileged(a); 154 } 155 156 163 private static boolean hasCachePermission() { 164 Boolean hasPermission = getCacheInfo().getHasPermission(); 165 166 if (hasPermission != null) { 167 return hasPermission.booleanValue(); 168 } else { 169 try { 170 SecurityManager security = System.getSecurityManager(); 171 if (security != null) { 172 File cachedir = getCacheDirectory(); 173 String cachepath; 174 175 if (cachedir != null) { 176 cachepath = cachedir.getPath(); 177 } else { 178 cachepath = getTempDir(); 179 180 if (cachepath == null) { 181 getCacheInfo().setHasPermission(Boolean.FALSE); 182 return false; 183 } 184 } 185 186 security.checkWrite(cachepath); 187 } 188 } catch (SecurityException e) { 189 getCacheInfo().setHasPermission(Boolean.FALSE); 190 return false; 191 } 192 193 getCacheInfo().setHasPermission(Boolean.TRUE); 194 return true; 195 } 196 } 197 198 224 public static void setUseCache(boolean useCache) { 225 getCacheInfo().setUseCache(useCache); 226 } 227 228 238 public static boolean getUseCache() { 239 return getCacheInfo().getUseCache(); 240 } 241 242 259 public static void setCacheDirectory(File cacheDirectory) { 260 if ((cacheDirectory != null) && !(cacheDirectory.isDirectory())) { 261 throw new IllegalArgumentException ("Not a directory!"); 262 } 263 getCacheInfo().setCacheDirectory(cacheDirectory); 264 getCacheInfo().setHasPermission(null); 265 } 266 267 278 public static File getCacheDirectory() { 279 return getCacheInfo().getCacheDirectory(); 280 } 281 282 308 public static ImageInputStream createImageInputStream(Object input) 309 throws IOException { 310 if (input == null) { 311 throw new IllegalArgumentException ("input == null!"); 312 } 313 314 Iterator iter; 315 try { 317 iter = theRegistry.getServiceProviders(ImageInputStreamSpi .class, 318 true); 319 } catch (IllegalArgumentException e) { 320 return null; 321 } 322 323 boolean usecache = getUseCache() && hasCachePermission(); 324 325 while (iter.hasNext()) { 326 ImageInputStreamSpi spi = (ImageInputStreamSpi )iter.next(); 327 if (spi.getInputClass().isInstance(input)) { 328 try { 329 return spi.createInputStreamInstance(input, 330 usecache, 331 getCacheDirectory()); 332 } catch (IOException e) { 333 throw new IIOException ("Can't create cache file!", e); 334 } 335 } 336 } 337 338 return null; 339 } 340 341 343 370 public static ImageOutputStream createImageOutputStream(Object output) 371 throws IOException { 372 if (output == null) { 373 throw new IllegalArgumentException ("output == null!"); 374 } 375 376 Iterator iter; 377 try { 379 iter = theRegistry.getServiceProviders(ImageOutputStreamSpi .class, 380 true); 381 } catch (IllegalArgumentException e) { 382 return null; 383 } 384 385 boolean usecache = getUseCache() && hasCachePermission(); 386 387 while (iter.hasNext()) { 388 ImageOutputStreamSpi spi = (ImageOutputStreamSpi )iter.next(); 389 if (spi.getOutputClass().isInstance(output)) { 390 try { 391 return spi.createOutputStreamInstance(output, 392 usecache, 393 getCacheDirectory()); 394 } catch (IOException e) { 395 throw new IIOException ("Can't create cache file!", e); 396 } 397 } 398 } 399 400 return null; 401 } 402 403 405 private static String [] toStringArray(Set s) { 406 String [] val = new String [s.size()]; 407 Iterator iter = s.iterator(); 408 int index = 0; 409 while (iter.hasNext()) { 410 val[index++] = (String )iter.next(); 411 } 412 413 return val; 414 } 415 416 423 public static String [] getReaderFormatNames() { 424 Iterator iter; 425 try { 427 iter = theRegistry.getServiceProviders(ImageReaderSpi .class, true); 428 } catch (IllegalArgumentException e) { 429 return new String [0]; 430 } 431 432 Set s = new HashSet (); 433 while (iter.hasNext()) { 434 ImageReaderSpi spi = (ImageReaderSpi )iter.next(); 435 String [] names = spi.getFormatNames(); 436 for (int i = 0; i < names.length; i++) { 437 s.add(names[i]); 438 } 439 } 440 441 return toStringArray(s); 442 } 443 444 451 public static String [] getReaderMIMETypes() { 452 Iterator iter; 453 try { 455 iter = theRegistry.getServiceProviders(ImageReaderSpi .class, true); 456 } catch (IllegalArgumentException e) { 457 return new String [0]; 458 } 459 460 Set s = new HashSet (); 461 while (iter.hasNext()) { 462 ImageReaderSpi spi = (ImageReaderSpi )iter.next(); 463 String [] names = spi.getMIMETypes(); 464 for (int i = 0; i < names.length; i++) { 465 s.add(names[i]); 466 } 467 } 468 469 return toStringArray(s); 470 } 471 472 static class ImageReaderIterator implements Iterator <ImageReader > { 473 public Iterator iter; 475 476 public ImageReaderIterator(Iterator iter) { 477 this.iter = iter; 478 } 479 480 public boolean hasNext() { 481 return iter.hasNext(); 482 } 483 484 public ImageReader next() { 485 ImageReaderSpi spi = null; 486 try { 487 spi = (ImageReaderSpi )iter.next(); 488 return spi.createReaderInstance(); 489 } catch (IOException e) { 490 theRegistry.deregisterServiceProvider(spi, ImageReaderSpi .class); 493 } 494 return null; 495 } 496 497 public void remove() { 498 throw new UnsupportedOperationException (); 499 } 500 } 501 502 static class CanDecodeInputFilter 503 implements ServiceRegistry.Filter { 504 505 Object input; 506 507 public CanDecodeInputFilter(Object input) { 508 this.input = input; 509 } 510 511 public boolean filter(Object elt) { 512 try { 513 ImageReaderSpi spi = (ImageReaderSpi )elt; 514 ImageInputStream stream = null; 515 if (input instanceof ImageInputStream ) { 516 stream = (ImageInputStream )input; 517 } 518 519 boolean canDecode = false; 523 if (stream != null) { 524 stream.mark(); 525 } 526 canDecode = spi.canDecodeInput(input); 527 if (stream != null) { 528 stream.reset(); 529 } 530 531 return canDecode; 532 } catch (IOException e) { 533 return false; 534 } 535 } 536 } 537 538 static class CanEncodeImageAndFormatFilter 539 implements ServiceRegistry.Filter { 540 541 ImageTypeSpecifier type; 542 String formatName; 543 544 public CanEncodeImageAndFormatFilter(ImageTypeSpecifier type, 545 String formatName) { 546 this.type = type; 547 this.formatName = formatName; 548 } 549 550 public boolean filter(Object elt) { 551 ImageWriterSpi spi = (ImageWriterSpi )elt; 552 return Arrays.asList(spi.getFormatNames()).contains(formatName) && 553 spi.canEncodeImage(type); 554 } 555 } 556 557 static class ContainsFilter 558 implements ServiceRegistry.Filter { 559 560 Method method; 561 String name; 562 563 public ContainsFilter(Method method, 565 String name) { 566 this.method = method; 567 this.name = name; 568 } 569 570 public boolean filter(Object elt) { 571 try { 572 return contains((String [])method.invoke(elt, null), name); 573 } catch (Exception e) { 574 return false; 575 } 576 } 577 } 578 579 598 public static Iterator <ImageReader > getImageReaders(Object input) { 599 if (input == null) { 600 throw new IllegalArgumentException ("input == null!"); 601 } 602 Iterator iter; 603 try { 605 iter = theRegistry.getServiceProviders(ImageReaderSpi .class, 606 new CanDecodeInputFilter(input), 607 true); 608 } catch (IllegalArgumentException e) { 609 return new HashSet ().iterator(); 610 } 611 612 return new ImageReaderIterator(iter); 613 } 614 615 private static Method readerFormatNamesMethod; 616 private static Method readerFileSuffixesMethod; 617 private static Method readerMIMETypesMethod; 618 private static Method writerFormatNamesMethod; 619 private static Method writerFileSuffixesMethod; 620 private static Method writerMIMETypesMethod; 621 622 static { 623 try { 624 readerFormatNamesMethod = 625 ImageReaderSpi .class.getMethod("getFormatNames", null); 626 readerFileSuffixesMethod = 627 ImageReaderSpi .class.getMethod("getFileSuffixes", null); 628 readerMIMETypesMethod = 629 ImageReaderSpi .class.getMethod("getMIMETypes", null); 630 631 writerFormatNamesMethod = 632 ImageWriterSpi .class.getMethod("getFormatNames", null); 633 writerFileSuffixesMethod = 634 ImageWriterSpi .class.getMethod("getFileSuffixes", null); 635 writerMIMETypesMethod = 636 ImageWriterSpi .class.getMethod("getMIMETypes", null); 637 } catch (NoSuchMethodException e) { 638 e.printStackTrace(); 639 } 640 } 641 642 658 public static Iterator <ImageReader > 659 getImageReadersByFormatName(String formatName) 660 { 661 if (formatName == null) { 662 throw new IllegalArgumentException ("formatName == null!"); 663 } 664 Iterator iter; 665 try { 667 iter = theRegistry.getServiceProviders(ImageReaderSpi .class, 668 new ContainsFilter(readerFormatNamesMethod, 669 formatName), 670 true); 671 } catch (IllegalArgumentException e) { 672 return new HashSet ().iterator(); 673 } 674 return new ImageReaderIterator(iter); 675 } 676 677 693 public static Iterator <ImageReader > 694 getImageReadersBySuffix(String fileSuffix) 695 { 696 if (fileSuffix == null) { 697 throw new IllegalArgumentException ("fileSuffix == null!"); 698 } 699 Iterator iter; 701 try { 702 iter = theRegistry.getServiceProviders(ImageReaderSpi .class, 703 new ContainsFilter(readerFileSuffixesMethod, 704 fileSuffix), 705 true); 706 } catch (IllegalArgumentException e) { 707 return new HashSet ().iterator(); 708 } 709 return new ImageReaderIterator(iter); 710 } 711 712 728 public static Iterator <ImageReader > 729 getImageReadersByMIMEType(String MIMEType) 730 { 731 if (MIMEType == null) { 732 throw new IllegalArgumentException ("MIMEType == null!"); 733 } 734 Iterator iter; 736 try { 737 iter = theRegistry.getServiceProviders(ImageReaderSpi .class, 738 new ContainsFilter(readerMIMETypesMethod, 739 MIMEType), 740 true); 741 } catch (IllegalArgumentException e) { 742 return new HashSet ().iterator(); 743 } 744 return new ImageReaderIterator(iter); 745 } 746 747 749 756 public static String [] getWriterFormatNames() { 757 Iterator iter; 758 try { 760 iter = theRegistry.getServiceProviders(ImageWriterSpi .class, true); 761 } catch (IllegalArgumentException e) { 762 re
|