001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.mail;
018
019import java.io.BufferedInputStream;
020import java.io.BufferedOutputStream;
021import java.io.ByteArrayInputStream;
022import java.io.ByteArrayOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.OutputStream;
026import java.io.UnsupportedEncodingException;
027
028import javax.activation.DataSource;
029
030/**
031 * This class implements a typed DataSource from:<br>
032 *
033 * - an InputStream<br>
034 * - a byte array<br>
035 * - a String<br>
036 *
037 * <p>
038 * From version 1.3.1, it is possible to set a name for this DataSource,
039 * and it is recommended to do so.
040 *
041 * @since 1.0
042 * @version $Id: ByteArrayDataSource.java 1459997 2013-03-22 21:23:15Z tn $
043 * @deprecated since 1.4, use {@link javax.mail.util.ByteArrayDataSource} instead
044 */
045@Deprecated
046public class ByteArrayDataSource implements DataSource
047{
048    /** Define the buffer size. */
049    public static final int BUFFER_SIZE = 512;
050
051    /** Stream containing the Data. */
052    private ByteArrayOutputStream baos;
053
054    /** The Content-type. */
055    private final String type; // = "application/octet-stream";
056
057    /**
058     * The name associated with this data source.
059     * By default, the name is an empty string, similar to javax.mail.util.ByteArrayDataSource.
060     * @since 1.3.1
061     */
062    private String name = "";
063
064    /**
065     * Create a datasource from a byte array.
066     *
067     * @param data A byte[].
068     * @param aType A String.
069     * @throws IOException IOException
070     * @since 1.0
071     */
072    public ByteArrayDataSource(byte[] data, String aType) throws IOException
073    {
074        this.type = aType;
075        ByteArrayInputStream bis = null;
076
077        try
078        {
079            bis = new ByteArrayInputStream(data);
080            this.byteArrayDataSource(bis);
081        }
082        finally
083        {
084            if (bis != null)
085            {
086                bis.close();
087            }
088        }
089    }
090
091    /**
092     * Create a datasource from an input stream.
093     *
094     * @param aIs An InputStream.
095     * @param aType A String.
096     * @throws IOException IOException
097     * @since 1.0
098     */
099    public ByteArrayDataSource(InputStream aIs, String aType) throws IOException
100    {
101        this.type = aType;
102        this.byteArrayDataSource(aIs);
103    }
104
105    /**
106     * Create a datasource from a String.
107     * N.B. assumes the data string can be converted using the charset iso-8859-1.
108     *
109     * @param data A String.
110     * @param aType A String.
111     * @throws IOException IOException
112     * @since 1.0
113     */
114    public ByteArrayDataSource(String data, String aType) throws IOException
115    {
116        this.type = aType;
117
118        try
119        {
120            baos = new ByteArrayOutputStream();
121
122            // Assumption that the string contains only ASCII characters!
123            // Else just pass in a charset into this constructor and use it in getBytes().
124            baos.write(data.getBytes("iso-8859-1"));
125            baos.flush();
126            baos.close();
127        }
128        catch (UnsupportedEncodingException uex)
129        {
130            throw new IOException("The Character Encoding is not supported.");
131        }
132        finally
133        {
134            if (baos != null)
135            {
136                baos.close();
137            }
138        }
139    }
140
141    /**
142      * Create a datasource from an input stream.
143      *
144      * @param aIs An InputStream.
145      * @throws IOException IOException
146      */
147    private void byteArrayDataSource(InputStream aIs)
148        throws IOException
149    {
150        BufferedInputStream bis = null;
151        BufferedOutputStream osWriter = null;
152
153        try
154        {
155            int length = 0;
156            byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE];
157
158            bis = new BufferedInputStream(aIs);
159            baos = new ByteArrayOutputStream();
160            osWriter = new BufferedOutputStream(baos);
161
162            // Write the InputData to OutputStream
163            while ((length = bis.read(buffer)) != -1)
164            {
165                osWriter.write(buffer, 0, length);
166            }
167            osWriter.flush();
168            osWriter.close();
169
170        }
171        finally
172        {
173            if (bis != null)
174            {
175                bis.close();
176            }
177            if (baos != null)
178            {
179                baos.close();
180            }
181            if (osWriter != null)
182            {
183                osWriter.close();
184            }
185        }
186    }
187
188    /**
189     * Get the content type.
190     *
191     * @return A String.
192     * @since 1.0
193     */
194    public String getContentType()
195    {
196        return type == null ? "application/octet-stream" : type;
197    }
198
199    /**
200     * Get the input stream.
201     *
202     * @return An InputStream.
203     * @throws IOException IOException
204     * @since 1.0
205     */
206    public InputStream getInputStream() throws IOException
207    {
208        if (baos == null)
209        {
210            throw new IOException("no data");
211        }
212        return new ByteArrayInputStream(baos.toByteArray());
213    }
214
215    /**
216     * Sets the name for this DataSource.
217     *
218     * @param name The name.
219     * @since 1.3.1
220     */
221    public void setName(String name)
222    {
223        this.name = name;
224    }
225
226    /**
227     * Get the name.
228     *
229     * @return A String.
230     * @since 1.0
231     */
232    public String getName()
233    {
234        return name;
235    }
236
237    /**
238     * Get the OutputStream to write to.
239     *
240     * @return  An OutputStream
241     * @since 1.0
242     */
243    public OutputStream getOutputStream()
244    {
245        baos = new ByteArrayOutputStream();
246        return baos;
247    }
248}