KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > activation > handlers > AbstractImageHandler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.activation.handlers;
18
19 import java.awt.Graphics2D JavaDoc;
20 import java.awt.Image JavaDoc;
21 import java.awt.datatransfer.DataFlavor JavaDoc;
22 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24 import java.awt.image.RenderedImage JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import javax.activation.DataContentHandler JavaDoc;
29 import javax.activation.DataSource JavaDoc;
30 import javax.activation.UnsupportedDataTypeException JavaDoc;
31 import javax.imageio.IIOImage JavaDoc;
32 import javax.imageio.ImageIO JavaDoc;
33 import javax.imageio.ImageReader JavaDoc;
34 import javax.imageio.ImageWriter JavaDoc;
35
36 /**
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public class AbstractImageHandler implements DataContentHandler JavaDoc {
40     private final DataFlavor JavaDoc flavour;
41
42     public AbstractImageHandler(DataFlavor JavaDoc flavour) {
43         this.flavour = flavour;
44     }
45
46     public DataFlavor JavaDoc[] getTransferDataFlavors() {
47         return new DataFlavor JavaDoc[]{flavour};
48     }
49
50     public Object JavaDoc getTransferData(DataFlavor JavaDoc dataFlavor, DataSource JavaDoc dataSource) throws UnsupportedFlavorException JavaDoc, IOException JavaDoc {
51         return flavour.equals(dataFlavor) ? getContent(dataSource) : null;
52     }
53
54     public Object JavaDoc getContent(DataSource JavaDoc ds) throws IOException JavaDoc {
55         Iterator JavaDoc i = ImageIO.getImageReadersByMIMEType(ds.getContentType());
56         if (!i.hasNext()) {
57             throw new UnsupportedDataTypeException JavaDoc();
58         }
59         ImageReader JavaDoc reader = (ImageReader JavaDoc) i.next();
60         return reader.read(0);
61     }
62
63     public void writeTo(Object JavaDoc obj, String JavaDoc mimeType, OutputStream JavaDoc os) throws IOException JavaDoc {
64         Iterator JavaDoc i = ImageIO.getImageWritersByMIMEType(mimeType);
65         if (!i.hasNext()) {
66             throw new UnsupportedDataTypeException JavaDoc();
67         }
68         ImageWriter JavaDoc writer = (ImageWriter JavaDoc) i.next();
69         writer.setOutput(os);
70
71         if (obj instanceof RenderedImage JavaDoc) {
72             writer.write((RenderedImage JavaDoc) obj);
73         } else if (obj instanceof BufferedImage JavaDoc) {
74             BufferedImage JavaDoc buffered = (BufferedImage JavaDoc) obj;
75             writer.write(new IIOImage JavaDoc(buffered.getRaster(), null, null));
76         } else if (obj instanceof Image JavaDoc) {
77             Image JavaDoc image = (Image JavaDoc) obj;
78             BufferedImage JavaDoc buffered = new BufferedImage JavaDoc(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
79             Graphics2D JavaDoc graphics = buffered.createGraphics();
80             graphics.drawImage(image, 0, 0, null, null);
81             writer.write(new IIOImage JavaDoc(buffered.getRaster(), null, null));
82         } else {
83             throw new UnsupportedDataTypeException JavaDoc();
84         }
85         os.flush();
86     }
87 }
88
Popular Tags