KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)text_plain.java 1.18 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.datatransfer.DataFlavor JavaDoc;
32 import javax.activation.*;
33 import javax.mail.internet.*;
34
35 /**
36  * DataContentHandler for text/plain.
37  *
38  * @version 1.18, 05/08/29
39  */

40 public class text_plain implements DataContentHandler {
41     private static ActivationDataFlavor myDF = new ActivationDataFlavor(
42     java.lang.String JavaDoc.class,
43     "text/plain",
44     "Text String");
45
46     protected ActivationDataFlavor getDF() {
47     return myDF;
48     }
49
50     /**
51      * Return the DataFlavors for this <code>DataContentHandler</code>.
52      *
53      * @return The DataFlavors
54      */

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

66     public Object JavaDoc getTransferData(DataFlavor JavaDoc df, DataSource ds)
67             throws IOException {
68     // use myDF.equals to be sure to get ActivationDataFlavor.equals,
69
// which properly ignores Content-Type parameters in comparison
70
if (getDF().equals(df))
71         return getContent(ds);
72     else
73         return null;
74     }
75
76     public Object JavaDoc getContent(DataSource ds) throws IOException {
77     String JavaDoc enc = null;
78     InputStreamReader is = null;
79     
80     try {
81         enc = getCharset(ds.getContentType());
82         is = new InputStreamReader(ds.getInputStream(), enc);
83     } catch (IllegalArgumentException JavaDoc iex) {
84         /*
85          * An unknown charset of the form ISO-XXX-XXX will cause
86          * the JDK to throw an IllegalArgumentException. The
87          * JDK will attempt to create a classname using this string,
88          * but valid classnames must not contain the character '-',
89          * and this results in an IllegalArgumentException, rather than
90          * the expected UnsupportedEncodingException. Yikes.
91          */

92         throw new UnsupportedEncodingException(enc);
93     }
94
95     int pos = 0;
96     int count;
97     char buf[] = new char[1024];
98
99     while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
100         pos += count;
101         if (pos >= buf.length) {
102         int size = buf.length;
103         if (size < 256*1024)
104             size += size;
105         else
106             size += 256*1024;
107         char tbuf[] = new char[size];
108         System.arraycopy(buf, 0, tbuf, 0, pos);
109         buf = tbuf;
110         }
111     }
112     return new String JavaDoc(buf, 0, pos);
113     }
114     
115     /**
116      * Write the object to the output stream, using the specified MIME type.
117      */

118     public void writeTo(Object JavaDoc obj, String JavaDoc type, OutputStream os)
119             throws IOException {
120     if (!(obj instanceof String JavaDoc))
121         throw new IOException("\"" + getDF().getMimeType() +
122         "\" DataContentHandler requires String object, " +
123         "was given object of type " + obj.getClass().toString());
124
125     String JavaDoc enc = null;
126     OutputStreamWriter osw = null;
127
128     try {
129         enc = getCharset(type);
130         osw = new OutputStreamWriter(os, enc);
131     } catch (IllegalArgumentException JavaDoc iex) {
132         /*
133          * An unknown charset of the form ISO-XXX-XXX will cause
134          * the JDK to throw an IllegalArgumentException. The
135          * JDK will attempt to create a classname using this string,
136          * but valid classnames must not contain the character '-',
137          * and this results in an IllegalArgumentException, rather than
138          * the expected UnsupportedEncodingException. Yikes.
139          */

140         throw new UnsupportedEncodingException(enc);
141     }
142
143     String JavaDoc s = (String JavaDoc)obj;
144     osw.write(s, 0, s.length());
145     osw.flush();
146     }
147
148     private String JavaDoc getCharset(String JavaDoc type) {
149     try {
150         ContentType ct = new ContentType(type);
151         String JavaDoc charset = ct.getParameter("charset");
152         if (charset == null)
153         // If the charset parameter is absent, use US-ASCII.
154
charset = "us-ascii";
155         return MimeUtility.javaCharset(charset);
156     } catch (Exception JavaDoc ex) {
157         return null;
158     }
159     }
160 }
161
Popular Tags