/[classpathx]/mail/source/gnu/mail/handler/Application.java
ViewVC logotype

Contents of /mail/source/gnu/mail/handler/Application.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Wed Apr 16 17:25:43 2003 UTC (21 years ago) by dog
Branch: MAIN
CVS Tags: release-20040420, release-1-0-0
Branch point for: release-1-0
Changes since 1.2: +16 -8 lines
updated licence on handler classes
added new multipart/signed handler
maildir appendMessages
basic maildir provider testing

1 /*
2 * Application.java
3 * Copyright (C) 2003 Chris Burdess <dog@gnu.org>
4 *
5 * This file is part of GNU JavaMail, a library.
6 *
7 * GNU JavaMail is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * GNU JavaMail is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * As a special exception, if you link this library with other files to
22 * produce an executable, this library does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * This exception does not however invalidate any other reasons why the
25 * executable file might be covered by the GNU General Public License.
26 */
27
28 package gnu.mail.handler;
29
30 import java.awt.datatransfer.DataFlavor;
31 import java.awt.datatransfer.UnsupportedFlavorException;
32 import java.io.*;
33 import javax.activation.*;
34 import javax.mail.internet.ContentType;
35 import javax.mail.internet.MimeUtility;
36 import javax.mail.internet.ParseException;
37
38 /**
39 * A JAF data content handler for the application/* family of MIME content
40 * types.
41 * This provides the basic behaviour for any number of byte-array-handling
42 * subtypes which simply need to override their default constructor to provide
43 * the correct MIME content-type and description.
44 */
45 public abstract class Application
46 implements DataContentHandler
47 {
48
49 /**
50 * Our favorite data flavor.
51 */
52 protected DataFlavor flavor;
53
54 /**
55 * Constructor specifying the data flavor.
56 * @param mimeType the MIME content type
57 * @param description the description of the content type
58 */
59 protected Application(String mimeType, String description)
60 {
61 flavor = new ActivationDataFlavor(byte[].class, mimeType,
62 description);
63 }
64
65 /**
66 * Returns an array of DataFlavor objects indicating the flavors the data
67 * can be provided in.
68 * @return the DataFlavors
69 */
70 public DataFlavor[] getTransferDataFlavors()
71 {
72 DataFlavor[] flavors = new DataFlavor[1];
73 flavors[0] = flavor;
74 return flavors;
75 }
76
77 /**
78 * Returns an object which represents the data to be transferred.
79 * The class of the object returned is defined by the representation class
80 * of the flavor.
81 * @param flavor the data flavor representing the requested type
82 * @param source the data source representing the data to be converted
83 * @return the constructed object
84 */
85 public Object getTransferData(DataFlavor flavor, DataSource source)
86 throws UnsupportedFlavorException, IOException
87 {
88 if (this.flavor.equals(flavor))
89 return getContent(source);
90 return null;
91 }
92
93 /**
94 * Return an object representing the data in its most preferred form.
95 * Generally this will be the form described by the first data flavor
96 * returned by the <code>getTransferDataFlavors</code> method.
97 * @param source the data source representing the data to be converted
98 * @return a byte array
99 */
100 public Object getContent(DataSource source)
101 throws IOException
102 {
103 InputStream in = source.getInputStream();
104 ByteArrayOutputStream out = new ByteArrayOutputStream();
105 byte[] buf = new byte[4096]; // TODO make configurable
106 while (true)
107 {
108 int len = in.read(buf);
109 if (len > -1)
110 out.write(buf, 0, len);
111 else
112 break;
113 }
114 return out.toByteArray();
115 }
116
117 /**
118 * Convert the object to a byte stream of the specified MIME type and
119 * write it to the output stream.
120 * @param object the object to be converted
121 * @param mimeType the requested MIME content type to write as
122 * @param out the output stream into which to write the converted object
123 */
124 public void writeTo(Object object, String mimeType, OutputStream out)
125 throws IOException
126 {
127 // We only handle arrays of byte
128 byte[] bytes = null;
129 if (object instanceof byte[])
130 bytes = (byte[])object;
131 out.write(bytes);
132 out.flush();
133 }
134
135 }

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26