1 8 package org.jboss.axis.attachments; 9 10 import javax.activation.DataSource ; 11 import javax.xml.transform.stream.StreamSource ; 12 import java.io.BufferedInputStream ; 13 import java.io.BufferedReader ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.OutputStream ; 19 import java.io.Reader ; 20 import java.net.URL ; 21 22 public class SourceDataSource implements DataSource 23 { 24 public static final String CONTENT_TYPE = "text/xml"; 25 26 private final String name; 27 private final String contentType; 28 private byte[] data; 29 private ByteArrayInputStream is; 30 private ByteArrayOutputStream os; 31 32 public SourceDataSource(String name, StreamSource data) 33 { 34 this(name, CONTENT_TYPE, data); 35 } 37 public SourceDataSource(String name, String contentType, StreamSource data) 38 { 39 this.name = name; 40 this.contentType = contentType == null ? CONTENT_TYPE : contentType; 41 os = new ByteArrayOutputStream (); 42 try 43 { 44 if (data != null) 45 { 46 Reader reader = data.getReader(); 48 if (reader != null) 49 { 50 reader = new BufferedReader (reader); 51 int ch; 52 while ((ch = reader.read()) != -1) 53 { 54 os.write(ch); 55 } 56 } 57 else 58 { 59 InputStream is = data.getInputStream(); 61 if (is == null) 62 { 63 String id = data.getSystemId(); 65 if (id != null) 66 { 67 URL url = new URL (id); 68 is = url.openStream(); 69 } 70 } 71 if (is != null) 72 { 73 is = new BufferedInputStream (is); 74 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 e) 89 { 90 } 92 } 94 public String getName() 95 { 96 return name; 97 } 99 public String getContentType() 100 { 101 return contentType; 102 } 104 public InputStream getInputStream() throws IOException 105 { 106 if (os.size() != 0) 107 { 108 data = os.toByteArray(); 109 os.reset(); 110 } 111 return new ByteArrayInputStream (data == null ? new byte[0] : data); 112 } 114 public OutputStream getOutputStream() throws IOException 115 { 116 if (os.size() != 0) 117 { 118 data = os.toByteArray(); 119 os.reset(); 120 } 121 return os; 122 } } | Popular Tags |