KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > util > ByteArrayDataSource


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  * @(#)ByteArrayDataSource.java 1.6 05/08/29
24  *
25  * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.util;
29
30 import java.io.*;
31 import javax.activation.*;
32 import javax.mail.internet.*;
33
34 /**
35  * A DataSource backed by a byte array. The byte array may be
36  * passed in directly, or may be initialized from an InputStream
37  * or a String.
38  *
39  * @since JavaMail 1.4
40  * @author John Mani
41  * @author Bill Shannon
42  * @author Max Spivak
43  */

44 public class ByteArrayDataSource implements DataSource {
45     private byte[] data; // data
46
private String JavaDoc type; // content-type
47
private String JavaDoc name = "";
48
49     /**
50      * Create a ByteArrayDataSource with data from the
51      * specified InputStream and with the specified MIME type.
52      * The InputStream is read completely and the data is
53      * stored in a byte array.
54      *
55      * @param is the InputStream
56      * @param type the MIME type
57      * @exception IOException errors reading the stream
58      */

59     public ByteArrayDataSource(InputStream is, String JavaDoc type) throws IOException {
60     ByteArrayOutputStream os = new ByteArrayOutputStream();
61     byte[] buf = new byte[8192];
62     int len;
63     while ((len = is.read(buf)) > 0)
64         os.write(buf, 0, len);
65     this.data = os.toByteArray();
66         this.type = type;
67     }
68
69     /**
70      * Create a ByteArrayDataSource with data from the
71      * specified byte array and with the specified MIME type.
72      *
73      * @param data the data
74      * @param type the MIME type
75      */

76     public ByteArrayDataSource(byte[] data, String JavaDoc type) {
77         this.data = data;
78     this.type = type;
79     }
80
81     /**
82      * Create a ByteArrayDataSource with data from the
83      * specified String and with the specified MIME type.
84      * The MIME type should include a <code>charset</code>
85      * parameter specifying the charset to be used for the
86      * string. If the parameter is not included, the
87      * default charset is used.
88      *
89      * @param data the String
90      * @param type the MIME type
91      * @exception IOException errors reading the String
92      */

93     public ByteArrayDataSource(String JavaDoc data, String JavaDoc type) throws IOException {
94     String JavaDoc charset = null;
95     try {
96         ContentType ct = new ContentType(type);
97         charset = ct.getParameter("charset");
98     } catch (ParseException pex) { }
99     if (charset == null)
100         charset = MimeUtility.getDefaultJavaCharset();
101     // XXX - could convert to bytes on demand rather than copying here
102
this.data = data.getBytes(charset);
103     this.type = type;
104     }
105
106     /**
107      * Return an InputStream for the data.
108      * Note that a new stream is returned each time
109      * this method is called.
110      *
111      * @return the InputStream
112      * @exception IOException if no data has been set
113      */

114     public InputStream getInputStream() throws IOException {
115     if (data == null)
116         throw new IOException("no data");
117     return new ByteArrayInputStream(data);
118     }
119
120     /**
121      * Return an OutputStream for the data.
122      * Writing the data is not supported; an <code>IOException</code>
123      * is always thrown.
124      *
125      * @exception IOException always
126      */

127     public OutputStream getOutputStream() throws IOException {
128     throw new IOException("cannot do this");
129     }
130
131     /**
132      * Get the MIME content type of the data.
133      *
134      * @return the MIME type
135      */

136     public String JavaDoc getContentType() {
137         return type;
138     }
139
140     /**
141      * Get the name of the data.
142      * By default, an empty string ("") is returned.
143      *
144      * @return the name of this data
145      */

146     public String JavaDoc getName() {
147         return name;
148     }
149
150     /**
151      * Set the name of the data.
152      *
153      * @param name the name of this data
154      */

155     public void setName(String JavaDoc name) {
156     this.name = name;
157     }
158 }
159
Popular Tags