KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > attachments > PlainTextDataSource


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8 package org.jboss.axis.attachments;
9
10 import javax.activation.DataSource JavaDoc;
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16
17 public class PlainTextDataSource implements DataSource JavaDoc
18 {
19    public static final String JavaDoc CONTENT_TYPE = "text/plain";
20
21    private final String JavaDoc name;
22    private byte[] data;
23    private ByteArrayInputStream JavaDoc is;
24    private ByteArrayOutputStream JavaDoc os;
25
26    public PlainTextDataSource(String JavaDoc name, String JavaDoc data)
27    {
28       this.name = name;
29       this.data = data == null ? null : data.getBytes();
30       os = new ByteArrayOutputStream JavaDoc();
31    } // ctor
32

33    public String JavaDoc getName()
34    {
35       return name;
36    } // getName
37

38    public String JavaDoc getContentType()
39    {
40       return CONTENT_TYPE;
41    } // getContentType
42

43    public InputStream JavaDoc getInputStream() throws IOException JavaDoc
44    {
45       if (os.size() != 0)
46       {
47          data = os.toByteArray();
48       }
49       return new ByteArrayInputStream JavaDoc(data == null ? new byte[0] : data);
50    } // getInputStream
51

52    public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
53    {
54       if (os.size() != 0)
55       {
56          data = os.toByteArray();
57       }
58       return new ByteArrayOutputStream JavaDoc();
59    } // getOutputStream
60
} // class PlainTextDataSource
61
Popular Tags