View Javadoc

1   /*
2    * $Id: PlainTextDataSource.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  /*
12   * Copyright 2001-2004 The Apache Software Foundation
13   *
14   * Licensed under the Apache License, Version 2.0 (the "License");
15   * you may not use this file except in compliance with the License.
16   * You may obtain a copy of the License at
17   *
18   *     http://www.apache.org/licenses/LICENSE-2.0
19   *
20   * Unless required by applicable law or agreed to in writing, software
21   * distributed under the License is distributed on an "AS IS" BASIS,
22   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23   * See the License for the specific language governing permissions and
24   * limitations under the License.
25   */
26  
27  package org.mule.transport.email.transformers;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  
34  import javax.activation.DataSource;
35  
36  import org.apache.commons.io.output.ByteArrayOutputStream;
37  
38  public class PlainTextDataSource implements DataSource
39  {
40      public static final String CONTENT_TYPE = "text/plain";
41  
42      private final String name;
43      private byte[] data;
44      private ByteArrayOutputStream os;
45  
46      // FIXME: Doesn't work with non ascii string, but now it's ok. 
47      public PlainTextDataSource(String name, String data)
48      {
49          this.name = name;
50          this.data = data == null ? null : data.getBytes();
51          os = new ByteArrayOutputStream();
52      } // ctor
53  
54      public String getName()
55      {
56          return name;
57      } // getName
58  
59      public String getContentType()
60      {
61          return CONTENT_TYPE;
62      } // getContentType
63  
64      public InputStream getInputStream() throws IOException
65      {
66          if (os.size() != 0)
67          {
68              data = os.toByteArray();
69          }
70          return new ByteArrayInputStream(data == null ? new byte[0] : data);
71      } // getInputStream
72  
73      public OutputStream getOutputStream() throws IOException
74      {
75          if (os.size() != 0)
76          {
77              data = os.toByteArray();
78          }
79          return new ByteArrayOutputStream();
80      } // getOutputStream
81  
82  } // class PlainTextDataSource