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 return new String [0]; 763 } 764 765 Set s = new HashSet (); 766 while (iter.hasNext()) { 767 ImageWriterSpi spi = (ImageWriterSpi )iter.next(); 768 String [] names = spi.getFormatNames(); 769 for (int i = 0; i < names.length; i++) { 770 s.add(names[i]); 771 } 772 } 773 774 return toStringArray(s); 775 } 776 777 784 public static String [] getWriterMIMETypes() { 785 Iterator iter; 786 try { 788 iter = theRegistry.getServiceProviders(ImageWriterSpi .class, true); 789 } catch (IllegalArgumentException e) { 790 return new String [0]; 791 } 792 793 Set s = new HashSet (); 794 while (iter.hasNext()) { 795 ImageWriterSpi spi = (ImageWriterSpi )iter.next(); 796 String [] names = spi.getMIMETypes(); 797 for (int i = 0; i < names.length; i++) { 798 s.add(names[i]); 799 } 800 } 801 802 return toStringArray(s); 803 } 804 805 static class ImageWriterIterator implements Iterator <ImageWriter > { 806 public Iterator iter; 808 809 public ImageWriterIterator(Iterator iter) { 810 this.iter = iter; 811 } 812 813 public boolean hasNext() { 814 return iter.hasNext(); 815 } 816 817 public ImageWriter next() { 818 ImageWriterSpi spi = null; 819 try { 820 spi = (ImageWriterSpi )iter.next(); 821 return spi.createWriterInstance(); 822 } catch (IOException e) { 823 theRegistry.deregisterServiceProvider(spi, ImageWriterSpi .class); 825 } 826 return null; 827 } 828 829 public void remove() { 830 throw new UnsupportedOperationException (); 831 } 832 } 833 834 private static boolean contains(String [] names, String name) { 835 for (int i = 0; i < names.length; i++) { 836 if (name.equalsIgnoreCase(names[i])) { 837 return true; 838 } 839 } 840 841 return false; 842 } 843 844 860 public static Iterator <ImageWriter > 861 getImageWritersByFormatName(String formatName) 862 { 863 if (formatName == null) { 864 throw new IllegalArgumentException ("formatName == null!"); 865 } 866 Iterator iter; 867 try { 869 iter = theRegistry.getServiceProviders(ImageWriterSpi .class, 870 new ContainsFilter(writerFormatNamesMethod, 871 formatName), 872 true); 873 } catch (IllegalArgumentException e) { 874 return new HashSet ().iterator(); 875 } 876 return new ImageWriterIterator(iter); 877 } 878 879 894 public static Iterator <ImageWriter > 895 getImageWritersBySuffix(String fileSuffix) 896 { 897 if (fileSuffix == null) { 898 throw new IllegalArgumentException ("fileSuffix == null!"); 899 } 900 Iterator iter; 901 try { 903 iter = theRegistry.getServiceProviders(ImageWriterSpi .class, 904 new ContainsFilter(writerFileSuffixesMethod, 905 fileSuffix), 906 true); 907 } catch (IllegalArgumentException e) { 908 return new HashSet ().iterator(); 909 } 910 return new ImageWriterIterator(iter); 911 } 912 913 928 public static Iterator <ImageWriter > 929 getImageWritersByMIMEType(String MIMEType) 930 { 931 if (MIMEType == null) { 932 throw new IllegalArgumentException ("MIMEType == null!"); 933 } 934 Iterator iter; 935 try { 937 iter = theRegistry.getServiceProviders(ImageWriterSpi .class, 938 new ContainsFilter(writerMIMETypesMethod, 939 MIMEType), 940 true); 941 } catch (IllegalArgumentException e) { 942 return new HashSet ().iterator(); 943 } 944 return new ImageWriterIterator(iter); 945 } 946 947 976 public static ImageWriter getImageWriter(ImageReader reader) { 977 if (reader == null) { 978 throw new IllegalArgumentException ("reader == null!"); 979 } 980 981 ImageReaderSpi readerSpi = reader.getOriginatingProvider(); 982 if (readerSpi == null) { 983 Iterator readerSpiIter; 984 try { 986 readerSpiIter = 987 theRegistry.getServiceProviders(ImageReaderSpi .class, 988 false); 989 } catch (IllegalArgumentException e) { 990 return null; 991 } 992 993 while (readerSpiIter.hasNext()) { 994 ImageReaderSpi temp = (ImageReaderSpi ) readerSpiIter.next(); 995 if (temp.isOwnReader(reader)) { 996 readerSpi = temp; 997 break; 998 } 999 } 1000 if (readerSpi == null) { 1001 return null; 1002 } 1003 } 1004 1005 String [] writerNames = readerSpi.getImageWriterSpiNames(); 1006 if (writerNames == null) { 1007 return null; 1008 } 1009 1010 Class writerSpiClass = null; 1011 try { 1012 writerSpiClass = Class.forName(writerNames[0], true, 1013 ClassLoader.getSystemClassLoader()); 1014 } catch (ClassNotFoundException e) { 1015 return null; 1016 } 1017 1018 ImageWriterSpi writerSpi = (ImageWriterSpi ) 1019 theRegistry.getServiceProviderByClass(writerSpiClass); 1020 if (writerSpi == null) { 1021 return null; 1022 } 1023 1024 try { 1025 return writerSpi.createWriterInstance(); 1026 } catch (IOException e) { 1027 theRegistry.deregisterServiceProvider(writerSpi, 1029 ImageWriterSpi .class); 1030 return null; 1031 } 1032 } 1033 1034 1056 public static ImageReader getImageReader(ImageWriter writer) { 1057 if (writer == null) { 1058 throw new IllegalArgumentException ("writer == null!"); 1059 } 1060 1061 ImageWriterSpi writerSpi = writer.getOriginatingProvider(); 1062 if (writerSpi == null) { 1063 Iterator writerSpiIter; 1064 try { 1066 writerSpiIter = 1067 theRegistry.getServiceProviders(ImageWriterSpi .class, 1068 false); 1069 } catch (IllegalArgumentException e) { 1070 return null; 1071 } 1072 1073 while (writerSpiIter.hasNext()) { 1074 ImageWriterSpi temp = (ImageWriterSpi ) writerSpiIter.next(); 1075 if (temp.isOwnWriter(writer)) { 1076 writerSpi = temp; 1077 break; 1078 } 1079 } 1080 if (writerSpi == null) { 1081 return null; 1082 } 1083 } 1084 1085 String [] readerNames = writerSpi.getImageReaderSpiNames(); 1086 if (readerNames == null) { 1087 return null; 1088 } 1089 1090 Class readerSpiClass = null; 1091 try { 1092 readerSpiClass = Class.forName(readerNames[0], true, 1093 ClassLoader.getSystemClassLoader()); 1094 } catch (ClassNotFoundException e) { 1095 return null; 1096 } 1097 1098 ImageReaderSpi readerSpi = (ImageReaderSpi ) 1099 theRegistry.getServiceProviderByClass(readerSpiClass); 1100 if (readerSpi == null) { 1101 return null; 1102 } 1103 1104 try { 1105 return readerSpi.createReaderInstance(); 1106 } catch (IOException e) { 1107 theRegistry.deregisterServiceProvider(readerSpi, 1109 ImageReaderSpi .class); 1110 return null; 1111 } 1112 } 1113 1114 1131 public static Iterator <ImageWriter > 1132 getImageWriters(ImageTypeSpecifier type, String formatName) 1133 { 1134 if (type == null) { 1135 throw new IllegalArgumentException ("type == null!"); 1136 } 1137 if (formatName == null) { 1138 throw new IllegalArgumentException ("formatName == null!"); 1139 } 1140 1141 Iterator iter; 1142 try { 1144 iter = theRegistry.getServiceProviders(ImageWriterSpi .class, 1145 new CanEncodeImageAndFormatFilter(type, 1146 formatName), 1147 true); 1148 } catch (IllegalArgumentException e) { 1149 return new HashSet ().iterator(); 1150 } 1151 1152 return new ImageWriterIterator(iter); 1153 } 1154 1155 static class ImageTranscoderIterator 1156 implements Iterator <ImageTranscoder > 1157 { 1158 public Iterator iter; 1160 1161 public ImageTranscoderIterator(Iterator iter) { 1162 this.iter = iter; 1163 } 1164 1165 public boolean hasNext() { 1166 return iter.hasNext(); 1167 } 1168 1169 public ImageTranscoder next() { 1170 ImageTranscoderSpi spi = null; 1171 spi = (ImageTranscoderSpi )iter.next(); 1172 return spi.createTranscoderInstance(); 1173 } 1174 1175 public void remove() { 1176 throw new UnsupportedOperationException (); 1177 } 1178 } 1179 1180 static class TranscoderFilter 1181 implements ServiceRegistry.Filter { 1182 1183 String readerSpiName; 1184 String writerSpiName; 1185 1186 public TranscoderFilter(ImageReaderSpi readerSpi, 1187 ImageWriterSpi writerSpi) { 1188 this.readerSpiName = readerSpi.getClass().getName(); 1189 this.writerSpiName = writerSpi.getClass().getName(); 1190 } 1191 1192 public boolean filter(Object elt) { 1193 ImageTranscoderSpi spi = (ImageTranscoderSpi )elt; 1194 String readerName = spi.getReaderServiceProviderName(); 1195 String writerName = spi.getWriterServiceProviderName(); 1196 return (readerName.equals(readerSpiName) && 1197 writerName.equals(writerSpiName)); 1198 } 1199 } 1200 1201 1216 public static Iterator <ImageTranscoder > 1217 getImageTranscoders(ImageReader reader, ImageWriter writer) 1218 { 1219 if (reader == null) { 1220 throw new IllegalArgumentException ("reader == null!"); 1221 } 1222 if (writer == null) { 1223 throw new IllegalArgumentException ("writer == null!"); 1224 } 1225 ImageReaderSpi readerSpi = reader.getOriginatingProvider(); 1226 ImageWriterSpi writerSpi = writer.getOriginatingProvider(); 1227 ServiceRegistry.Filter filter = 1228 new TranscoderFilter(readerSpi, writerSpi); 1229 1230 Iterator iter; 1231 try { 1233 iter = theRegistry.getServiceProviders(ImageTranscoderSpi .class, 1234 filter, true); 1235 } catch (IllegalArgumentException e) { 1236 return new HashSet ().iterator(); 1237 } 1238 return new ImageTranscoderIterator(iter); 1239 } 1240 1241 1243 1274 public static BufferedImage read(File input) throws IOException { 1275 if (input == null) { 1276 throw new IllegalArgumentException ("input == null!"); 1277 } 1278 if (!input.canRead()) { 1279 throw new IIOException ("Can't read input file!"); 1280 } 1281 1282 ImageInputStream stream = createImageInputStream(input); 1283 if (stream == null) { 1284 throw new IIOException ("Can't create an ImageInputStream!"); 1285 } 1286 return read(stream); 1287 } 1288 1289 1316 public static BufferedImage read(InputStream input) throws IOException { 1317 if (input == null) { 1318 throw new IllegalArgumentException ("input == null!"); 1319 } 1320 1321 ImageInputStream stream = createImageInputStream(input); 1322 return read(stream); 1323 } 1324 1325 1352 public static BufferedImage read(URL input) throws IOException { 1353 if (input == null) { 1354 throw new IllegalArgumentException ("input == null!"); 1355 } 1356 1357 InputStream istream = null; 1358 try { 1359 istream = input.openStream(); 1360 } catch (IOException e) { 1361 throw new IIOException ("Can't get input stream from URL!", e); 1362 } 1363 ImageInputStream stream = createImageInputStream(istream); 1364 BufferedImage bi = read(stream); 1365 istream.close(); 1366 return bi; 1367 } 1368 1369 1386 public static BufferedImage read(ImageInputStream stream) 1387 throws IOException { 1388 if (stream == null) { 1389 throw new IllegalArgumentException ("stream == null!"); 1390 } 1391 1392 Iterator iter = getImageReaders(stream); 1393 if (!iter.hasNext()) { 1394 return null; 1395 } 1396 1397 ImageReader reader = (ImageReader )iter.next(); 1398 ImageReadParam param = reader.getDefaultReadParam(); 1399 reader.setInput(stream, true, true); 1400 BufferedImage bi = reader.read(0, param); 1401 stream.close(); 1402 reader.dispose(); 1403 return bi; 1404 } 1405 1406 1425 public static boolean write(RenderedImage im, 1426 String formatName, 1427 ImageOutputStream output) throws IOException { 1428 if (im == null) { 1429 throw new IllegalArgumentException ("im == null!"); 1430 } 1431 if (formatName == null) { 1432 throw new IllegalArgumentException ("formatName == null!"); 1433 } 1434 if (output == null) { 1435 throw new IllegalArgumentException ("output == null!"); 1436 } 1437 1438 ImageWriter writer = null; 1439 ImageTypeSpecifier type = 1440 ImageTypeSpecifier.createFromRenderedImage(im); 1441 Iterator iter = getImageWriters(type, formatName); 1442 if (iter.hasNext()) { 1443 writer = (ImageWriter )iter.next(); 1444 } 1445 if (writer == null) { 1446 return false; 1447 } 1448 1449 writer.setOutput(output); 1450 writer.write(im); 1451 output.flush(); 1452 writer.dispose(); 1453 1454 return true; 1455 } 1456 1457 1474 public static boolean write(RenderedImage im, 1475 String formatName, 1476 File output) throws IOException { 1477 if (output == null) { 1478 throw new IllegalArgumentException ("output == null!"); 1479 } 1480 ImageOutputStream stream = null; 1481 try { 1482 output.delete(); 1483 stream = createImageOutputStream(output); 1484 } catch (IOException e) { 1485 throw new IIOException ("Can't create output stream!", e); 1486 } 1487 1488 boolean val = write(im, formatName, stream); 1489 stream.close(); 1490 return val; 1491 } 1492 1493 1511 public static boolean write(RenderedImage im, 1512 String formatName, 1513 OutputStream output) throws IOException { 1514 if (output == null) { 1515 throw new IllegalArgumentException ("output == null!"); 1516 } 1517 ImageOutputStream stream = null; 1518 try { 1519 stream = createImageOutputStream(output); 1520 } catch (IOException e) { 1521 throw new IIOException ("Can't create output stream!", e); 1522 } 1523 1524 boolean val = write(im, formatName, stream); 1525 stream.close(); 1526 return val; 1527 } 1528} 1529 | Popular Tags |