Source Code Cross Referenced for ImageIO.java in  » Apache-Harmony-Java-SE » javax-package » javax » imageio » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.imageio 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        /**
018:         * @author Rustem V. Rafikov
019:         * @version $Revision: 1.3 $
020:         */package javax.imageio;
021:
022:        import javax.imageio.stream.ImageInputStream;
023:        import javax.imageio.stream.ImageOutputStream;
024:        import javax.imageio.spi.*;
025:
026:        import org.apache.harmony.luni.util.NotImplementedException;
027:
028:        import java.io.File;
029:        import java.io.IOException;
030:        import java.io.InputStream;
031:        import java.io.OutputStream;
032:        import java.util.Iterator;
033:        import java.util.Arrays;
034:        import java.awt.image.BufferedImage;
035:        import java.awt.image.RenderedImage;
036:        import java.net.URL;
037:
038:        public final class ImageIO {
039:
040:            private static final IIORegistry registry = IIORegistry
041:                    .getDefaultInstance();
042:
043:            private ImageIO() {
044:            }
045:
046:            public static void scanForPlugins() throws NotImplementedException {
047:                // TODO: implement
048:                throw new NotImplementedException();
049:            }
050:
051:            public static void setUseCache(boolean useCache)
052:                    throws NotImplementedException {
053:                // TODO: implement
054:                throw new NotImplementedException();
055:            }
056:
057:            public static boolean getUseCache() {
058:                // TODO: implement
059:                return false;
060:            }
061:
062:            public static void setCacheDirectory(File cacheDirectory)
063:                    throws NotImplementedException {
064:                // TODO: implement
065:                throw new NotImplementedException();
066:            }
067:
068:            public static File getCacheDirectory()
069:                    throws NotImplementedException {
070:                // TODO: implement
071:                //-- null indicates system-dep default temporary directory
072:                return null;
073:            }
074:
075:            public static ImageInputStream createImageInputStream(Object input)
076:                    throws IOException {
077:
078:                if (input == null) {
079:                    throw new IllegalArgumentException(
080:                            "input source cannot be NULL");
081:                }
082:
083:                Iterator<ImageInputStreamSpi> it = registry
084:                        .getServiceProviders(ImageInputStreamSpi.class, true);
085:
086:                while (it.hasNext()) {
087:                    ImageInputStreamSpi spi = it.next();
088:                    if (spi.getInputClass().isInstance(input)) {
089:                        return spi.createInputStreamInstance(input);
090:                    }
091:                }
092:                return null;
093:            }
094:
095:            public static ImageOutputStream createImageOutputStream(
096:                    Object output) throws IOException {
097:                if (output == null) {
098:                    throw new IllegalArgumentException(
099:                            "output destination cannot be NULL");
100:                }
101:
102:                Iterator<ImageOutputStreamSpi> it = registry
103:                        .getServiceProviders(ImageOutputStreamSpi.class, true);
104:
105:                while (it.hasNext()) {
106:                    ImageOutputStreamSpi spi = it.next();
107:                    if (spi.getOutputClass().isInstance(output)) {
108:                        // todo - use getUseCache and getCacheDir here
109:                        return spi.createOutputStreamInstance(output);
110:                    }
111:                }
112:                return null;
113:            }
114:
115:            public static String[] getReaderFormatNames()
116:                    throws NotImplementedException {
117:                // TODO: implement
118:                throw new NotImplementedException();
119:            }
120:
121:            public static String[] getReaderMIMETypes()
122:                    throws NotImplementedException {
123:                // TODO: implement
124:                throw new NotImplementedException();
125:            }
126:
127:            public static Iterator<ImageReader> getImageReaders(Object input) {
128:                if (input == null) {
129:                    throw new NullPointerException("input cannot be NULL");
130:                }
131:
132:                Iterator<ImageReaderSpi> it = registry.getServiceProviders(
133:                        ImageReaderSpi.class, new CanReadFilter(input), true);
134:
135:                return new SpiIteratorToReadersIteratorWrapper(it);
136:            }
137:
138:            public static Iterator<ImageReader> getImageReadersByFormatName(
139:                    String formatName) {
140:                if (formatName == null) {
141:                    throw new NullPointerException("format name cannot be NULL");
142:                }
143:
144:                Iterator<ImageReaderSpi> it = registry.getServiceProviders(
145:                        ImageReaderSpi.class, new FormatFilter(formatName),
146:                        true);
147:
148:                return new SpiIteratorToReadersIteratorWrapper(it);
149:            }
150:
151:            public static Iterator<ImageReader> getImageReadersBySuffix(
152:                    String fileSuffix) {
153:                if (fileSuffix == null) {
154:                    throw new NullPointerException("suffix cannot be NULL");
155:                }
156:                Iterator<ImageReaderSpi> it = registry.getServiceProviders(
157:                        ImageReaderSpi.class, new SuffixFilter(fileSuffix),
158:                        true);
159:
160:                return new SpiIteratorToReadersIteratorWrapper(it);
161:            }
162:
163:            public static Iterator<ImageReader> getImageReadersByMIMEType(
164:                    String MIMEType) throws NotImplementedException {
165:                // TODO: implement
166:                throw new NotImplementedException();
167:            }
168:
169:            public static String[] getWriterFormatNames()
170:                    throws NotImplementedException {
171:                // TODO: implement
172:                throw new NotImplementedException();
173:            }
174:
175:            public static String[] getWriterMIMETypes()
176:                    throws NotImplementedException {
177:                // TODO: implement
178:                throw new NotImplementedException();
179:            }
180:
181:            public static Iterator<ImageWriter> getImageWritersByFormatName(
182:                    String formatName) {
183:                if (formatName == null) {
184:                    throw new NullPointerException("format name cannot be NULL");
185:                }
186:
187:                Iterator<ImageWriterSpi> it = registry.getServiceProviders(
188:                        ImageWriterSpi.class, new FormatFilter(formatName),
189:                        true);
190:
191:                return new SpiIteratorToWritersIteratorWrapper(it);
192:            }
193:
194:            public static Iterator<ImageWriter> getImageWritersBySuffix(
195:                    String fileSuffix) {
196:                if (fileSuffix == null) {
197:                    throw new NullPointerException("suffix cannot be NULL");
198:                }
199:                Iterator<ImageWriterSpi> it = registry.getServiceProviders(
200:                        ImageWriterSpi.class, new SuffixFilter(fileSuffix),
201:                        true);
202:                return new SpiIteratorToWritersIteratorWrapper(it);
203:            }
204:
205:            public static Iterator<ImageWriter> getImageWritersByMIMEType(
206:                    String MIMEType) throws NotImplementedException {
207:                // TODO: implement
208:                throw new NotImplementedException();
209:            }
210:
211:            public static ImageWriter getImageWriter(ImageReader reader)
212:                    throws NotImplementedException {
213:                // TODO: implement
214:                throw new NotImplementedException();
215:            }
216:
217:            public static ImageReader getImageReader(ImageWriter writer)
218:                    throws NotImplementedException {
219:                // TODO: implement
220:                throw new NotImplementedException();
221:            }
222:
223:            public static Iterator<ImageWriter> getImageWriters(
224:                    ImageTypeSpecifier type, String formatName) {
225:                if (type == null) {
226:                    throw new NullPointerException("type cannot be NULL");
227:                }
228:
229:                if (formatName == null) {
230:                    throw new NullPointerException("format name cannot be NULL");
231:                }
232:
233:                Iterator<ImageWriterSpi> it = registry.getServiceProviders(
234:                        ImageWriterSpi.class, new FormatAndEncodeFilter(type,
235:                                formatName), true);
236:
237:                return new SpiIteratorToWritersIteratorWrapper(it);
238:            }
239:
240:            public static Iterator<ImageTranscoder> getImageTranscoders(
241:                    ImageReader reader, ImageWriter writer)
242:                    throws NotImplementedException {
243:                // TODO: implement
244:                throw new NotImplementedException();
245:            }
246:
247:            public static BufferedImage read(File input) throws IOException {
248:                if (input == null) {
249:                    throw new IllegalArgumentException("input == null!");
250:                }
251:
252:                ImageInputStream stream = createImageInputStream(input);
253:                return read(stream);
254:            }
255:
256:            public static BufferedImage read(InputStream input)
257:                    throws IOException {
258:                if (input == null) {
259:                    throw new IllegalArgumentException("input == null!");
260:                }
261:
262:                ImageInputStream stream = createImageInputStream(input);
263:                return read(stream);
264:            }
265:
266:            public static BufferedImage read(URL input) throws IOException {
267:                if (input == null) {
268:                    throw new IllegalArgumentException("input == null!");
269:                }
270:
271:                InputStream stream = input.openStream();
272:                BufferedImage res = read(stream);
273:                stream.close();
274:
275:                return res;
276:            }
277:
278:            public static BufferedImage read(ImageInputStream stream)
279:                    throws IOException {
280:                if (stream == null) {
281:                    throw new IllegalArgumentException("stream == null!");
282:                }
283:
284:                Iterator<ImageReader> imageReaders = getImageReaders(stream);
285:                if (!imageReaders.hasNext()) {
286:                    return null;
287:                }
288:
289:                ImageReader reader = imageReaders.next();
290:                reader.setInput(stream, false, true);
291:                BufferedImage res = reader.read(0);
292:                reader.dispose();
293:
294:                try {
295:                    stream.close();
296:                } catch (IOException e) {
297:                    // Stream could be already closed, proceed silently in this case
298:                }
299:
300:                return res;
301:            }
302:
303:            public static boolean write(RenderedImage im, String formatName,
304:                    ImageOutputStream output) throws IOException {
305:
306:                if (im == null) {
307:                    throw new IllegalArgumentException("image cannot be NULL");
308:                }
309:                if (formatName == null) {
310:                    throw new IllegalArgumentException(
311:                            "format name cannot be NULL");
312:                }
313:                if (output == null) {
314:                    throw new IllegalArgumentException("output cannot be NULL");
315:                }
316:
317:                Iterator<ImageWriter> it = getImageWriters(ImageTypeSpecifier
318:                        .createFromRenderedImage(im), formatName);
319:                if (it.hasNext()) {
320:                    ImageWriter writer = it.next();
321:                    writer.setOutput(output);
322:                    writer.write(im);
323:                    output.flush();
324:                    writer.dispose();
325:                    return true;
326:                }
327:                return false;
328:            }
329:
330:            public static boolean write(RenderedImage im, String formatName,
331:                    File output) throws IOException {
332:
333:                if (output == null) {
334:                    throw new IllegalArgumentException("output cannot be NULL");
335:                }
336:
337:                if (output.exists()) {
338:                    output.delete();
339:                }
340:
341:                ImageOutputStream ios = createImageOutputStream(output);
342:                boolean rt = write(im, formatName, ios);
343:                ios.close();
344:                return rt;
345:            }
346:
347:            public static boolean write(RenderedImage im, String formatName,
348:                    OutputStream output) throws IOException {
349:
350:                if (output == null) {
351:                    throw new IllegalArgumentException("output cannot be NULL");
352:                }
353:
354:                ImageOutputStream ios = createImageOutputStream(output);
355:                boolean rt = write(im, formatName, ios);
356:                ios.close();
357:                return rt;
358:            }
359:
360:            /**
361:             * Filter to match spi by format name
362:             */
363:            static class FormatFilter implements  ServiceRegistry.Filter {
364:                private String name;
365:
366:                public FormatFilter(String name) {
367:                    this .name = name;
368:                }
369:
370:                public boolean filter(Object provider) {
371:                    ImageReaderWriterSpi spi = (ImageReaderWriterSpi) provider;
372:                    return Arrays.asList(spi.getFormatNames()).contains(name);
373:                }
374:            }
375:
376:            /**
377:             * Filter to match spi by format name and encoding possibility
378:             */
379:            static class FormatAndEncodeFilter extends FormatFilter {
380:
381:                private ImageTypeSpecifier type;
382:
383:                public FormatAndEncodeFilter(ImageTypeSpecifier type,
384:                        String name) {
385:                    super (name);
386:                    this .type = type;
387:                }
388:
389:                @Override
390:                public boolean filter(Object provider) {
391:                    ImageWriterSpi spi = (ImageWriterSpi) provider;
392:                    return super .filter(provider) && spi.canEncodeImage(type);
393:                }
394:            }
395:
396:            /**
397:             * Filter to match spi by suffix
398:             */
399:            static class SuffixFilter implements  ServiceRegistry.Filter {
400:                private String suf;
401:
402:                public SuffixFilter(String suf) {
403:                    this .suf = suf;
404:                }
405:
406:                public boolean filter(Object provider) {
407:                    ImageReaderWriterSpi spi = (ImageReaderWriterSpi) provider;
408:                    return Arrays.asList(spi.getFileSuffixes()).contains(suf);
409:                }
410:            }
411:
412:            /**
413:             * Filter to match spi by decoding possibility
414:             */
415:            static class CanReadFilter implements  ServiceRegistry.Filter {
416:                private Object input;
417:
418:                public CanReadFilter(Object input) {
419:                    this .input = input;
420:                }
421:
422:                public boolean filter(Object provider) {
423:                    ImageReaderSpi spi = (ImageReaderSpi) provider;
424:                    try {
425:                        return spi.canDecodeInput(input);
426:                    } catch (IOException e) {
427:                        return false;
428:                    }
429:                }
430:            }
431:
432:            /**
433:             * Wraps Spi's iterator to ImageWriter iterator
434:             */
435:            static class SpiIteratorToWritersIteratorWrapper implements 
436:                    Iterator<ImageWriter> {
437:
438:                private Iterator<ImageWriterSpi> backend;
439:
440:                public SpiIteratorToWritersIteratorWrapper(
441:                        Iterator<ImageWriterSpi> backend) {
442:                    this .backend = backend;
443:                }
444:
445:                public ImageWriter next() {
446:                    try {
447:                        return backend.next().createWriterInstance();
448:                    } catch (IOException e) {
449:                        e.printStackTrace();
450:                        return null;
451:                    }
452:                }
453:
454:                public boolean hasNext() {
455:                    return backend.hasNext();
456:                }
457:
458:                public void remove() {
459:                    throw new UnsupportedOperationException(
460:                            "Use deregisterServiceprovider instead of Iterator.remove()");
461:                }
462:            }
463:
464:            /**
465:             * Wraps spi's iterator to ImageReader iterator
466:             */
467:            static class SpiIteratorToReadersIteratorWrapper implements 
468:                    Iterator<ImageReader> {
469:                private Iterator<ImageReaderSpi> backend;
470:
471:                public SpiIteratorToReadersIteratorWrapper(
472:                        Iterator<ImageReaderSpi> backend) {
473:                    this .backend = backend;
474:                }
475:
476:                public ImageReader next() {
477:                    try {
478:                        return backend.next().createReaderInstance();
479:                    } catch (IOException e) {
480:                        e.printStackTrace();
481:                        return null;
482:                    }
483:                }
484:
485:                public boolean hasNext() {
486:                    return backend.hasNext();
487:                }
488:
489:                public void remove() {
490:                    throw new UnsupportedOperationException(
491:                            "Use deregisterServiceprovider instead of Iterator.remove()");
492:                }
493:            }
494:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.