KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > mail > ByteArrayDataSource


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.mail;
4
5 import java.io.ByteArrayInputStream JavaDoc;
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13
14 import javax.activation.DataSource JavaDoc;
15 import javax.activation.FileTypeMap JavaDoc;
16
17 /**
18  * Implements a DataSource from an InputStream, a byte array, a String, a File.
19  *
20  * This class has been build upon the similar one in javamail demos, but it
21  * is enhanced.
22  */

23 public class ByteArrayDataSource implements DataSource JavaDoc {
24
25     private byte[] data; // data
26
private String JavaDoc type; // content-type
27

28     /**
29      * Create a datasource from a File. If the Content-Type parameter is null,
30      * the type will be derived from the filename extension.
31      *
32      * @param f File object
33      * @param type Content-Type
34      */

35     public ByteArrayDataSource(File JavaDoc f, String JavaDoc type) throws IOException JavaDoc {
36         this(new FileInputStream JavaDoc(f), type);
37         if (this.type == null) {
38             this.type = FileTypeMap.getDefaultFileTypeMap().getContentType(f);
39         }
40     }
41
42     /**
43      * Create a datasource from an input stream.
44      *
45      * @param is InputStream
46      * @param type Content-Type
47      */

48     public ByteArrayDataSource(InputStream JavaDoc is, String JavaDoc type) throws IOException JavaDoc {
49         this.type = type;
50
51         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc(4096);
52
53         byte buf[] = new byte[4096];
54         int len;
55         while (true) {
56             len = is.read(buf);
57             if (len < 0) break;
58             os.write(buf, 0, len);
59         }
60         data = os.toByteArray();
61     }
62
63     /**
64      * Create a datasource from a byte array.
65      *
66      * @param data byte array
67      * @param type Content-Type
68      */

69     public ByteArrayDataSource(byte[] data, String JavaDoc type) {
70         this.type = type;
71         this.data = data;
72     }
73
74     /**
75      * Create a datasource from a String. This method defaults to
76      * a String encoding of iso-8859-1. For a different encoding,
77      * specify a Mime "charset" in the Content-Type parameter.
78      *
79      * @param data byte array
80      * @param type Content-Type
81      */

82     public ByteArrayDataSource(String JavaDoc data, String JavaDoc type) {
83         this.type = type;
84         try {
85             this.data = data.getBytes("ISO-8859-1");
86         } catch (UnsupportedEncodingException JavaDoc uex) {
87             // ignore
88
}
89     }
90
91     /**
92      * Return an InputStream to read the content.
93      *
94      * @return an InputStream with the content
95      */

96     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
97         if (data == null) {
98             throw new IOException JavaDoc("No data.");
99         }
100         return new ByteArrayInputStream JavaDoc(data);
101     }
102
103     /**
104      * This DataSource cannot return an OutputStream, so this method is not
105      * implemented.
106      */

107     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
108         throw new IOException JavaDoc("getOutputStream() not supported.");
109     }
110
111     /**
112      * Get the content type.
113      *
114      * @return Content-Type string
115      */

116     public String JavaDoc getContentType() {
117         return type;
118     }
119
120     /**
121      * Set the content type.
122      *
123      * @param type Content-Type string
124      */

125     public void setContentType(String JavaDoc type) {
126         this.type = type;
127     }
128
129     /**
130      * getName() is not implemented.
131      */

132     public String JavaDoc getName() {
133         return "";
134     }
135
136     /**
137      * Write the content to an OutputStream.
138      *
139      * @param os OutputStream to write the entire content to
140      */

141     public void writeTo(OutputStream JavaDoc os) throws IOException JavaDoc {
142         os.write(data);
143     }
144
145     /**
146      * Return the content as a byte array.
147      *
148      * @return byte array with the content
149      */

150     public byte[] toByteArray() {
151         return data;
152     }
153
154     /**
155      * Return the number of bytes in the content.
156      *
157      * @return size of the byte array, or -1 if not set.
158      */

159     public int getSize() {
160         if (data == null)
161             return -1;
162         else
163             return data.length;
164     }
165
166     /**
167      * Return the content as a String. The Content-Type "charset" parameter
168      * will be used to determine the encoding, and if that's not available or
169      * invalid, "iso-8859-1".
170      *
171      * @return a String with the content
172      */

173     public String JavaDoc getText() {
174         try {
175             return new String JavaDoc(data, type);
176         } catch (UnsupportedEncodingException JavaDoc uex) {
177             try {
178                 return new String JavaDoc(data, "ISO-8859-1");
179             } catch (UnsupportedEncodingException JavaDoc uex1) {
180                 return null;
181             }
182         }
183     }
184 }
185
Popular Tags