KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.attachments;
17
18 import javax.activation.DataSource JavaDoc;
19 import javax.xml.transform.stream.StreamSource JavaDoc;
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.BufferedReader JavaDoc;
27 import java.io.BufferedInputStream JavaDoc;
28 import java.net.URL JavaDoc;
29
30 public class SourceDataSource implements DataSource JavaDoc {
31     public static final String JavaDoc CONTENT_TYPE = "text/xml";
32
33     private final String JavaDoc name;
34     private final String JavaDoc contentType;
35     private byte[] data;
36     private ByteArrayOutputStream JavaDoc os;
37
38     public SourceDataSource(String JavaDoc name, StreamSource JavaDoc data) {
39         this(name, CONTENT_TYPE, data);
40     } // ctor
41

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

86     public String JavaDoc getName() {
87         return name;
88     } // getName
89

90     public String JavaDoc getContentType() {
91         return contentType;
92     } // getContentType
93

94     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
95         if (os.size() != 0) {
96             data = os.toByteArray();
97             os.reset();
98         }
99         return new ByteArrayInputStream JavaDoc(data == null ? new byte[0] : data);
100     } // getInputStream
101

102     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
103         if (os.size() != 0) {
104             data = os.toByteArray();
105             os.reset();
106         }
107         return os;
108     } // getOutputStream
109
} // class SourceDataSource
110
Popular Tags