View Javadoc

1   /*
2    * $Id: PlainTextDataSource.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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      public PlainTextDataSource(String name, String data)
47      {
48          this.name = name;
49          this.data = data == null ? null : data.getBytes();
50          os = new ByteArrayOutputStream();
51      } // ctor
52  
53      public String getName()
54      {
55          return name;
56      } // getName
57  
58      public String getContentType()
59      {
60          return CONTENT_TYPE;
61      } // getContentType
62  
63      public InputStream getInputStream() throws IOException
64      {
65          if (os.size() != 0)
66          {
67              data = os.toByteArray();
68          }
69          return new ByteArrayInputStream(data == null ? new byte[0] : data);
70      } // getInputStream
71  
72      public OutputStream getOutputStream() throws IOException
73      {
74          if (os.size() != 0)
75          {
76              data = os.toByteArray();
77          }
78          return new ByteArrayOutputStream();
79      } // getOutputStream
80  
81  } // class PlainTextDataSource