001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.mail;
017:
018: import java.io.BufferedInputStream;
019: import java.io.BufferedOutputStream;
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.io.UnsupportedEncodingException;
026:
027: import javax.activation.DataSource;
028:
029: /**
030: * This class implements a typed DataSource from:<br>
031: *
032: * - an InputStream<br>
033: * - a byte array<br>
034: * - a String<br>
035: *
036: * @since 1.0
037: * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a>
038: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
039: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
040: * @version $Id: ByteArrayDataSource.java 225600 2005-07-27 20:16:23Z rdonkin $
041: */
042: public class ByteArrayDataSource implements DataSource {
043: /** define the buffer size */
044: public static final int BUFFER_SIZE = 512;
045:
046: /** Stream containg the Data */
047: private ByteArrayOutputStream baos;
048:
049: /** Content-type. */
050: private String type = "application/octet-stream";
051:
052: /**
053: * Create a datasource from a byte array.
054: *
055: * @param data A byte[].
056: * @param aType A String.
057: * @throws IOException IOException
058: * @since 1.0
059: */
060: public ByteArrayDataSource(byte[] data, String aType)
061: throws IOException {
062: ByteArrayInputStream bis = null;
063:
064: try {
065: bis = new ByteArrayInputStream(data);
066: this .byteArrayDataSource(bis, aType);
067: } catch (IOException ioex) {
068: throw ioex;
069: } finally {
070: if (bis != null) {
071: bis.close();
072: }
073: }
074: }
075:
076: /**
077: * Create a datasource from an input stream.
078: *
079: * @param aIs An InputStream.
080: * @param aType A String.
081: * @throws IOException IOException
082: * @since 1.0
083: */
084: public ByteArrayDataSource(InputStream aIs, String aType)
085: throws IOException {
086: this .byteArrayDataSource(aIs, aType);
087: }
088:
089: /**
090: * Create a datasource from a String.
091: *
092: * @param data A String.
093: * @param aType A String.
094: * @throws IOException IOException
095: * @since 1.0
096: */
097: public ByteArrayDataSource(String data, String aType)
098: throws IOException {
099: this .type = aType;
100:
101: try {
102: baos = new ByteArrayOutputStream();
103:
104: // Assumption that the string contains only ASCII
105: // characters! Else just pass in a charset into this
106: // constructor and use it in getBytes().
107: baos.write(data.getBytes("iso-8859-1"));
108: baos.flush();
109: baos.close();
110: } catch (UnsupportedEncodingException uex) {
111: throw new IOException(
112: "The Character Encoding is not supported.");
113: } finally {
114: if (baos != null) {
115: baos.close();
116: }
117: }
118: }
119:
120: /**
121: * Create a datasource from an input stream.
122: *
123: * @param aIs An InputStream.
124: * @param aType A String.
125: * @throws IOException IOException
126: */
127: private void byteArrayDataSource(InputStream aIs, String aType)
128: throws IOException {
129: this .type = aType;
130:
131: BufferedInputStream bis = null;
132: BufferedOutputStream osWriter = null;
133:
134: try {
135: int length = 0;
136: byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE];
137:
138: bis = new BufferedInputStream(aIs);
139: baos = new ByteArrayOutputStream();
140: osWriter = new BufferedOutputStream(baos);
141:
142: //Write the InputData to OutputStream
143: while ((length = bis.read(buffer)) != -1) {
144: osWriter.write(buffer, 0, length);
145: }
146: osWriter.flush();
147: osWriter.close();
148:
149: } catch (IOException ioex) {
150: throw ioex;
151: } finally {
152: if (bis != null) {
153: bis.close();
154: }
155: if (baos != null) {
156: baos.close();
157: }
158: if (osWriter != null) {
159: osWriter.close();
160: }
161: }
162: }
163:
164: /**
165: * Get the content type.
166: *
167: * @return A String.
168: * @since 1.0
169: */
170: public String getContentType() {
171: return type == null ? "application/octet-stream" : type;
172: }
173:
174: /**
175: * Get the input stream.
176: *
177: * @return An InputStream.
178: * @throws IOException IOException
179: * @since 1.0
180: */
181: public InputStream getInputStream() throws IOException {
182: if (baos == null) {
183: throw new IOException("no data");
184: }
185: return new ByteArrayInputStream(baos.toByteArray());
186: }
187:
188: /**
189: * Get the name.
190: *
191: * @return A String.
192: * @since 1.0
193: */
194: public String getName() {
195: return "ByteArrayDataSource";
196: }
197:
198: /**
199: * Get the OutputStream to write to
200: *
201: * @return An OutputStream
202: * @since 1.0
203: */
204: public OutputStream getOutputStream() {
205: baos = new ByteArrayOutputStream();
206: return baos;
207: }
208: }
|