KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.xml.transform.stream.StreamSource JavaDoc;
12 import java.io.BufferedInputStream JavaDoc;
13 import java.io.BufferedReader JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.io.OutputStream JavaDoc;
19 import java.io.Reader JavaDoc;
20 import java.net.URL JavaDoc;
21
22 public class SourceDataSource implements DataSource JavaDoc
23 {
24    public static final String JavaDoc CONTENT_TYPE = "text/xml";
25
26    private final String JavaDoc name;
27    private final String JavaDoc contentType;
28    private byte[] data;
29    private ByteArrayInputStream JavaDoc is;
30    private ByteArrayOutputStream JavaDoc os;
31
32    public SourceDataSource(String JavaDoc name, StreamSource JavaDoc data)
33    {
34       this(name, CONTENT_TYPE, data);
35    } // ctor
36

37    public SourceDataSource(String JavaDoc name, String JavaDoc contentType, StreamSource JavaDoc data)
38    {
39       this.name = name;
40       this.contentType = contentType == null ? CONTENT_TYPE : contentType;
41       os = new ByteArrayOutputStream JavaDoc();
42       try
43       {
44          if (data != null)
45          {
46             // Try the Reader first
47
Reader JavaDoc reader = data.getReader();
48             if (reader != null)
49             {
50                reader = new BufferedReader JavaDoc(reader);
51                int ch;
52                while ((ch = reader.read()) != -1)
53                {
54                   os.write(ch);
55                }
56             }
57             else
58             {
59                // Try the InputStream
60
InputStream JavaDoc is = data.getInputStream();
61                if (is == null)
62                {
63                   // Try the URL
64
String JavaDoc id = data.getSystemId();
65                   if (id != null)
66                   {
67                      URL JavaDoc url = new URL JavaDoc(id);
68                      is = url.openStream();
69                   }
70                }
71                if (is != null)
72                {
73                   is = new BufferedInputStream JavaDoc(is);
74                   // If reading from a socket or URL, we could get a partial read.
75
byte[] bytes = null;
76                   int avail;
77                   while ((avail = is.available()) > 0)
78                   {
79                      if (bytes == null || avail > bytes.length)
80                         bytes = new byte[avail];
81                      is.read(bytes, 0, avail);
82                      os.write(bytes, 0, avail);
83                   }
84                }
85             }
86          }
87       }
88       catch (Exception JavaDoc e)
89       {
90          // Is this sufficient?
91
}
92    } // ctor
93

94    public String JavaDoc getName()
95    {
96       return name;
97    } // getName
98

99    public String JavaDoc getContentType()
100    {
101       return contentType;
102    } // getContentType
103

104    public InputStream JavaDoc getInputStream() throws IOException JavaDoc
105    {
106       if (os.size() != 0)
107       {
108          data = os.toByteArray();
109          os.reset();
110       }
111       return new ByteArrayInputStream JavaDoc(data == null ? new byte[0] : data);
112    } // getInputStream
113

114    public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
115    {
116       if (os.size() != 0)
117       {
118          data = os.toByteArray();
119          os.reset();
120       }
121       return os;
122    } // getOutputStream
123
} // class SourceDataSource
124
Popular Tags