001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.mail;
004:
005: import java.io.ByteArrayInputStream;
006: import java.io.ByteArrayOutputStream;
007: import java.io.File;
008: import java.io.FileInputStream;
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.OutputStream;
012: import java.io.UnsupportedEncodingException;
013:
014: import javax.activation.DataSource;
015: import javax.activation.FileTypeMap;
016:
017: /**
018: * Implements a DataSource from an InputStream, a byte array, a String, a File.
019: *
020: * This class has been build upon the similar one in javamail demos, but it
021: * is enhanced.
022: */
023: public class ByteArrayDataSource implements DataSource {
024:
025: private byte[] data; // data
026: private String type; // content-type
027:
028: /**
029: * Create a datasource from a File. If the Content-Type parameter is null,
030: * the type will be derived from the filename extension.
031: *
032: * @param f File object
033: * @param type Content-Type
034: */
035: public ByteArrayDataSource(File f, String type) throws IOException {
036: this (new FileInputStream(f), type);
037: if (this .type == null) {
038: this .type = FileTypeMap.getDefaultFileTypeMap()
039: .getContentType(f);
040: }
041: }
042:
043: /**
044: * Create a datasource from an input stream.
045: *
046: * @param is InputStream
047: * @param type Content-Type
048: */
049: public ByteArrayDataSource(InputStream is, String type)
050: throws IOException {
051: this .type = type;
052:
053: ByteArrayOutputStream os = new ByteArrayOutputStream(4096);
054:
055: byte buf[] = new byte[4096];
056: int len;
057: while (true) {
058: len = is.read(buf);
059: if (len < 0) {
060: break;
061: }
062: os.write(buf, 0, len);
063: }
064: data = os.toByteArray();
065: }
066:
067: /**
068: * Create a datasource from a byte array.
069: *
070: * @param data byte array
071: * @param type Content-Type
072: */
073: public ByteArrayDataSource(byte[] data, String type) {
074: this .type = type;
075: this .data = data;
076: }
077:
078: /**
079: * Create a datasource from a String. This method defaults to
080: * a String encoding of iso-8859-1. For a different encoding,
081: * specify a Mime "charset" in the Content-Type parameter.
082: *
083: * @param data byte array
084: * @param type Content-Type
085: */
086: public ByteArrayDataSource(String data, String type) {
087: this .type = type;
088: try {
089: this .data = data.getBytes("ISO-8859-1");
090: } catch (UnsupportedEncodingException uex) {
091: // ignore
092: }
093: }
094:
095: /**
096: * Return an InputStream to read the content.
097: *
098: * @return an InputStream with the content
099: */
100: public InputStream getInputStream() throws IOException {
101: if (data == null) {
102: throw new IOException("No data.");
103: }
104: return new ByteArrayInputStream(data);
105: }
106:
107: /**
108: * This DataSource cannot return an OutputStream, so this method is not
109: * implemented.
110: */
111: public OutputStream getOutputStream() throws IOException {
112: throw new IOException("getOutputStream() not supported.");
113: }
114:
115: /**
116: * Get the content type.
117: *
118: * @return Content-Type string
119: */
120: public String getContentType() {
121: return type;
122: }
123:
124: /**
125: * Set the content type.
126: *
127: * @param type Content-Type string
128: */
129: public void setContentType(String type) {
130: this .type = type;
131: }
132:
133: /**
134: * getName() is not implemented.
135: */
136: public String getName() {
137: return "";
138: }
139:
140: /**
141: * Write the content to an OutputStream.
142: *
143: * @param os OutputStream to write the entire content to
144: */
145: public void writeTo(OutputStream os) throws IOException {
146: os.write(data);
147: }
148:
149: /**
150: * Return the content as a byte array.
151: *
152: * @return byte array with the content
153: */
154: public byte[] toByteArray() {
155: return data;
156: }
157:
158: /**
159: * Return the number of bytes in the content.
160: *
161: * @return size of the byte array, or -1 if not set.
162: */
163: public int getSize() {
164: if (data == null) {
165: return -1;
166: } else {
167: return data.length;
168: }
169: }
170:
171: /**
172: * Return the content as a String. The Content-Type "charset" parameter
173: * will be used to determine the encoding, and if that's not available or
174: * invalid, "iso-8859-1".
175: *
176: * @return a String with the content
177: */
178: public String getText() {
179: try {
180: return new String(data, type);
181: } catch (UnsupportedEncodingException uex) {
182: try {
183: return new String(data, "ISO-8859-1");
184: } catch (UnsupportedEncodingException uex1) {
185: return null;
186: }
187: }
188: }
189: }
|