View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.email.transformers;
8   
9   import java.io.ByteArrayInputStream;
10  import java.io.IOException;
11  import java.io.InputStream;
12  import java.io.OutputStream;
13  
14  import javax.activation.DataSource;
15  
16  import org.apache.commons.io.output.ByteArrayOutputStream;
17  
18  public class PlainTextDataSource implements DataSource
19  {
20      public static final String CONTENT_TYPE = "text/plain";
21  
22      private final String name;
23      private byte[] data;
24      private ByteArrayOutputStream os;
25  
26      // FIXME: Doesn't work with non ascii string, but now it's ok. 
27      public PlainTextDataSource(String name, String data)
28      {
29          this.name = name;
30          this.data = data == null ? null : data.getBytes();
31          os = new ByteArrayOutputStream();
32      } // ctor
33  
34      public String getName()
35      {
36          return name;
37      } // getName
38  
39      public String getContentType()
40      {
41          return CONTENT_TYPE;
42      } // getContentType
43  
44      public InputStream getInputStream() throws IOException
45      {
46          if (os.size() != 0)
47          {
48              data = os.toByteArray();
49          }
50          return new ByteArrayInputStream(data == null ? new byte[0] : data);
51      } // getInputStream
52  
53      public OutputStream getOutputStream() throws IOException
54      {
55          if (os.size() != 0)
56          {
57              data = os.toByteArray();
58          }
59          return new ByteArrayOutputStream();
60      } // getOutputStream
61  
62  } // class PlainTextDataSource