KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > handler > Application


1 /*
2  * Application.java
3  * Copyright (C) 2002 dog <dog@dog.net.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package gnu.mail.handler;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
24 import java.io.*;
25 import javax.activation.*;
26 import javax.mail.internet.ContentType JavaDoc;
27 import javax.mail.internet.MimeUtility JavaDoc;
28 import javax.mail.internet.ParseException JavaDoc;
29
30 /**
31  * A JAF data content handler for the application/* family of MIME content
32  * types.
33  * This provides the basic behaviour for any number of byte-array-handling
34  * subtypes which simply need to override their default constructor to provide
35  * the correct MIME content-type and description.
36  */

37 public abstract class Application
38   implements DataContentHandler
39 {
40
41   /**
42    * Our favorite data flavor.
43    */

44   protected DataFlavor JavaDoc flavor;
45   
46   /**
47    * Constructor specifying the data flavor.
48    * @param mimeType the MIME content type
49    * @param description the description of the content type
50    */

51   protected Application(String JavaDoc mimeType, String JavaDoc description)
52   {
53     flavor = new ActivationDataFlavor(byte[].class, mimeType,
54         description);
55   }
56
57   /**
58    * Returns an array of DataFlavor objects indicating the flavors the data
59    * can be provided in.
60    * @return the DataFlavors
61    */

62   public DataFlavor JavaDoc[] getTransferDataFlavors()
63   {
64     DataFlavor JavaDoc[] flavors = new DataFlavor JavaDoc[1];
65     flavors[0] = flavor;
66     return flavors;
67   }
68
69   /**
70    * Returns an object which represents the data to be transferred.
71    * The class of the object returned is defined by the representation class
72    * of the flavor.
73    * @param flavor the data flavor representing the requested type
74    * @param source the data source representing the data to be converted
75    * @return the constructed object
76    */

77   public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor, DataSource source)
78     throws UnsupportedFlavorException JavaDoc, IOException
79   {
80     if (this.flavor.equals(flavor))
81       return getContent(source);
82     return null;
83   }
84
85   /**
86    * Return an object representing the data in its most preferred form.
87    * Generally this will be the form described by the first data flavor
88    * returned by the <code>getTransferDataFlavors</code> method.
89    * @param source the data source representing the data to be converted
90    * @return a byte array
91    */

92   public Object JavaDoc getContent(DataSource source)
93     throws IOException
94   {
95     InputStream in = source.getInputStream();
96     ByteArrayOutputStream out = new ByteArrayOutputStream();
97     byte[] buf = new byte[4096]; // TODO make configurable
98
while (true)
99     {
100       int len = in.read(buf);
101       if (len > -1)
102         out.write(buf, 0, len);
103       else
104         break;
105     }
106     return out.toByteArray();
107   }
108
109   /**
110    * Convert the object to a byte stream of the specified MIME type and
111    * write it to the output stream.
112    * @param object the object to be converted
113    * @param mimeType the requested MIME content type to write as
114    * @param out the output stream into which to write the converted object
115    */

116   public void writeTo(Object JavaDoc object, String JavaDoc mimeType, OutputStream out)
117     throws IOException
118   {
119     // We only handle arrays of byte
120
byte[] bytes = null;
121     if (object instanceof byte[])
122       bytes = (byte[])object;
123     out.write(bytes);
124     out.flush();
125   }
126
127 }
128
Popular Tags