KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > handlers > image_gif


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)image_gif.java 1.2 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.handlers;
29
30 import java.io.*;
31 import java.awt.*;
32 import java.awt.datatransfer.DataFlavor JavaDoc;
33 import javax.activation.*;
34 import javax.mail.internet.*;
35
36 /**
37  * DataContentHandler for image/gif.
38  */

39 public class image_gif implements DataContentHandler {
40     private static ActivationDataFlavor myDF = new ActivationDataFlavor(
41     java.awt.Image JavaDoc.class,
42     "image/gif",
43     "GIF Image");
44
45     protected ActivationDataFlavor getDF() {
46     return myDF;
47     }
48
49     /**
50      * Return the DataFlavors for this <code>DataContentHandler</code>.
51      *
52      * @return The DataFlavors
53      */

54     public DataFlavor JavaDoc[] getTransferDataFlavors() { // throws Exception;
55
return new DataFlavor JavaDoc[] { getDF() };
56     }
57
58     /**
59      * Return the Transfer Data of type DataFlavor from InputStream.
60      *
61      * @param df The DataFlavor
62      * @param ins The InputStream corresponding to the data
63      * @return String object
64      */

65     public Object JavaDoc getTransferData(DataFlavor JavaDoc df, DataSource ds)
66             throws IOException {
67     // use myDF.equals to be sure to get ActivationDataFlavor.equals,
68
// which properly ignores Content-Type parameters in comparison
69
if (getDF().equals(df))
70         return getContent(ds);
71     else
72         return null;
73     }
74
75     public Object JavaDoc getContent(DataSource ds) throws IOException {
76     InputStream is = ds.getInputStream();
77     int pos = 0;
78     int count;
79     byte buf[] = new byte[1024];
80
81     while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
82         pos += count;
83         if (pos >= buf.length) {
84         int size = buf.length;
85         if (size < 256*1024)
86             size += size;
87         else
88             size += 256*1024;
89         byte tbuf[] = new byte[size];
90         System.arraycopy(buf, 0, tbuf, 0, pos);
91         buf = tbuf;
92         }
93     }
94     Toolkit tk = Toolkit.getDefaultToolkit();
95     return tk.createImage(buf, 0, pos);
96     }
97
98     /**
99      * Write the object to the output stream, using the specified MIME type.
100      */

101     public void writeTo(Object JavaDoc obj, String JavaDoc type, OutputStream os)
102             throws IOException {
103     if (!(obj instanceof Image))
104         throw new IOException("\"" + getDF().getMimeType() +
105         "\" DataContentHandler requires Image object, " +
106         "was given object of type " + obj.getClass().toString());
107
108     throw new IOException(getDF().getMimeType() + " encoding not supported");
109     }
110 }
111
Popular Tags