Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/axis/attachments/SourceDataSource.java


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;
19  import javax.xml.transform.stream.StreamSource;
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.io.Reader;
26  import java.io.BufferedReader;
27  import java.io.BufferedInputStream;
28  import java.net.URL;
29  
30  public class SourceDataSource implements DataSource {
31      public static final String CONTENT_TYPE = "text/xml";
32  
33      private final String name;
34      private final String contentType;
35      private byte[] data;
36      private ByteArrayOutputStream os;
37  
38      public SourceDataSource(String name, StreamSource data) {
39          this(name, CONTENT_TYPE, data);
40      } // ctor
41  
42      public SourceDataSource(String name, String contentType, StreamSource data) {
43          this.name = name;
44          this.contentType = contentType == null ? CONTENT_TYPE : contentType;
45          os = new ByteArrayOutputStream();
46          try {
47              if (data != null) {
48                  // Try the Reader first
49                  Reader reader = data.getReader();
50                  if (reader != null) {
51                      reader = new BufferedReader(reader);
52                      int ch;
53                      while ((ch = reader.read())!=-1) {
54                          os.write(ch);
55                      }
56                  } else {
57                      // Try the InputStream
58                      InputStream is = data.getInputStream();
59                      if (is == null) {
60                          // Try the URL
61                          String id = data.getSystemId();
62                          if (id != null) {
63                              URL url = new URL(id);
64                              is = url.openStream();
65                          }
66                      }
67                      if (is != null) {
68                          is = new BufferedInputStream(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 e) {
82              // Is this sufficient?
83          }
84      } // ctor
85  
86      public String getName() {
87          return name;
88      } // getName
89  
90      public String getContentType() {
91          return contentType;
92      } // getContentType
93  
94      public InputStream getInputStream() throws IOException {
95          if (os.size() != 0) {
96              data = os.toByteArray();
97              os.reset();
98          }
99          return new ByteArrayInputStream(data == null ? new byte[0] : data);
100     } // getInputStream
101 
102     public OutputStream getOutputStream() throws IOException {
103         if (os.size() != 0) {
104             data = os.toByteArray();
105             os.reset();
106         }
107         return os;
108     } // getOutputStream
109 } // class SourceDataSource