View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.mail.util;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.ByteArrayInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  
28  import javax.activation.DataSource;
29  import javax.mail.internet.ContentType;
30  import javax.mail.internet.ParseException;
31  import javax.mail.internet.MimeUtility;
32  
33  
34  /**
35   * An activation DataSource object that sources the data from
36   * a byte[] array.
37   * @version $Rev: 467553 $ $Date: 2006-10-25 00:01:51 -0400 (Wed, 25 Oct 2006) $
38   */
39  public class ByteArrayDataSource implements DataSource {
40      // the data source
41      private byte[] source;
42      // the content MIME type
43      private String contentType;
44      // the name information (defaults to a null string)
45      private String name = "";
46  
47  
48      /**
49       * Create a ByteArrayDataSource from an input stream.
50       *
51       * @param in     The source input stream.
52       * @param type   The MIME-type of the data.
53       *
54       * @exception IOException
55       */
56      public ByteArrayDataSource(InputStream in, String type) throws IOException {
57          ByteArrayOutputStream sink = new ByteArrayOutputStream();
58  
59          // ok, how I wish you could just pipe an input stream into an output stream :-)
60          byte[] buffer = new byte[8192];
61          int bytesRead;
62  
63          while ((bytesRead = in.read(buffer)) > 0) {
64              sink.write(buffer, 0, bytesRead);
65          }
66  
67          source = sink.toByteArray();
68          contentType = type;
69      }
70  
71  
72      /**
73       * Create a ByteArrayDataSource directly from a byte array.
74       *
75       * @param data   The source byte array (not copied).
76       * @param type   The content MIME-type.
77       */
78      public ByteArrayDataSource(byte[] data, String type) {
79          source = data;
80          contentType = type;
81      }
82  
83      /**
84       * Create a ByteArrayDataSource from a string value.  If the
85       * type information includes a charset parameter, that charset
86       * is used to extract the bytes.  Otherwise, the default Java
87       * char set is used.
88       *
89       * @param data   The source data string.
90       * @param type   The MIME type information.
91       *
92       * @exception IOException
93       */
94      public ByteArrayDataSource(String data, String type) throws IOException {
95          String charset = null;
96          try {
97              // the charset can be encoded in the content type, which we parse using
98              // the ContentType class.
99              ContentType content = new ContentType(type);
100             charset = content.getParameter("charset");
101         } catch (ParseException e) {
102             // ignored...just use the default if this fails
103         }
104         if (charset == null) {
105             charset = MimeUtility.getDefaultJavaCharset();
106         }
107         else {
108             // the type information encodes a MIME charset, which may need mapping to a Java one.
109             charset = MimeUtility.javaCharset(charset);
110         }
111 
112         // get the source using the specified charset
113         source = data.getBytes(charset);
114         contentType = type;
115     }
116 
117 
118     /**
119      * Create an input stream for this data.  A new input stream
120      * is created each time.
121      *
122      * @return An InputStream for reading the encapsulated data.
123      * @exception IOException
124      */
125     public InputStream getInputStream() throws IOException {
126         return new ByteArrayInputStream(source);
127     }
128 
129 
130     /**
131      * Open an output stream for the DataSource.  This is not
132      * supported by this DataSource, so an IOException is always
133      * throws.
134      *
135      * @return Nothing...an IOException is always thrown.
136      * @exception IOException
137      */
138     public OutputStream getOutputStream() throws IOException {
139         throw new IOException("Writing to a ByteArrayDataSource is not supported");
140     }
141 
142 
143     /**
144      * Get the MIME content type information for this DataSource.
145      *
146      * @return The MIME content type string.
147      */
148     public String getContentType() {
149         return contentType;
150     }
151 
152 
153     /**
154      * Retrieve the DataSource name.  If not explicitly set, this
155      * returns "".
156      *
157      * @return The currently set DataSource name.
158      */
159     public String getName() {
160         return name;
161     }
162 
163 
164     /**
165      * Set a new DataSource name.
166      *
167      * @param name   The new name.
168      */
169     public void setName(String name) {
170         this.name = name;
171     }
172 }
173