KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > email > transformers > PlainTextDataSource


1 /*
2  * $Id: PlainTextDataSource.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.providers.email.transformers;
28
29 import org.apache.commons.io.output.ByteArrayOutputStream;
30
31 import javax.activation.DataSource JavaDoc;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.OutputStream JavaDoc;
37
38 public class PlainTextDataSource implements DataSource JavaDoc
39 {
40     public static final String JavaDoc CONTENT_TYPE = "text/plain";
41
42     private final String JavaDoc name;
43     private byte[] data;
44     private ByteArrayOutputStream os;
45
46     public PlainTextDataSource(String JavaDoc name, String JavaDoc data)
47     {
48         this.name = name;
49         this.data = data == null ? null : data.getBytes();
50         os = new ByteArrayOutputStream();
51     } // ctor
52

53     public String JavaDoc getName()
54     {
55         return name;
56     } // getName
57

58     public String JavaDoc getContentType()
59     {
60         return CONTENT_TYPE;
61     } // getContentType
62

63     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
64     {
65         if (os.size() != 0)
66         {
67             data = os.toByteArray();
68         }
69         return new ByteArrayInputStream JavaDoc(data == null ? new byte[0] : data);
70     } // getInputStream
71

72     public OutputStream getOutputStream() throws IOException JavaDoc
73     {
74         if (os.size() != 0)
75         {
76             data = os.toByteArray();
77         }
78         return new ByteArrayOutputStream();
79     } // getOutputStream
80

81 } // class PlainTextDataSource
82
Popular Tags