KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > util > mime > ByteArrayDataSource


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.util.mime;
59
60 import java.io.*;
61 import org.apache.soap.Constants;
62 import javax.activation.*;
63
64 /**
65  * This class implements a typed DataSource from
66  * an InputStream,
67  * a byte array,
68  * a String,
69  * a File.
70  *
71  * @author Wouter Cloetens (wcloeten@raleigh.ibm.com)
72  */

73
74 public class ByteArrayDataSource implements DataSource {
75     private byte[] data; // data
76
private String JavaDoc type; // content-type
77

78     /**
79      * Create a datasource from a File. If the Content-Type parameter is null,
80      * the type will be derived from the filename extension.
81      *
82      * @param f File object
83      * @param type Content-Type
84      */

85     public ByteArrayDataSource(File f, String JavaDoc type) throws IOException {
86         this(new FileInputStream(f), type);
87         if (this.type == null)
88             this.type = FileTypeMap.getDefaultFileTypeMap().getContentType(f);
89     }
90
91     /**
92      * Create a datasource from an input stream.
93      *
94      * @param is InputStream
95      * @param type Content-Type
96      */

97     public ByteArrayDataSource(InputStream is, String JavaDoc type)
98         throws IOException {
99         this.type = type;
100
101         ByteArrayOutputStream os = new ByteArrayOutputStream();
102
103         byte buf[] = new byte[4096];
104         int len;
105         while (true){
106             len = is.read(buf);
107             if (len < 0)
108                 break;
109             os.write(buf, 0, len);
110         }
111         data = os.toByteArray();
112     }
113
114     /**
115      * Create a datasource from a byte array.
116      *
117      * @param data byte array
118      * @param type Content-Type
119      */

120     public ByteArrayDataSource(byte[] data, String JavaDoc type)
121     {
122         this.type = type;
123         this.data = data;
124     }
125
126     /**
127      * Create a datasource from a String. This method defaults to
128      * a String encoding of iso-8859-1. For a different encoding,
129      * specify a Mime "charset" in the Content-Type parameter.
130      *
131      * @param data byte array
132      * @param type Content-Type
133      */

134     public ByteArrayDataSource(String JavaDoc data, String JavaDoc type)
135     {
136         this.type = type;
137         try {
138             this.data = data.getBytes(MimeUtils.getEncoding(type,
139                 Constants.HEADERVAL_DEFAULT_CHARSET));
140         } catch (UnsupportedEncodingException uex) {
141         }
142     }
143
144     /**
145      * Return an InputStream to read the content.
146      *
147      * @return an InputStream with the content
148      */

149     public InputStream getInputStream() throws IOException {
150         if (data == null)
151             throw new IOException("No data.");
152         return new ByteArrayInputStream(data);
153     }
154
155     /**
156      * This DataSource cannot return an OutputStream, so this method is not
157      * implemented.
158      */

159     public OutputStream getOutputStream() throws IOException {
160         throw new IOException("getOutputStream() not supported.");
161     }
162
163     /**
164      * Get the content type.
165      *
166      * @return Content-Type string
167      */

168     public String JavaDoc getContentType() {
169         return type;
170     }
171
172     /**
173      * Set the content type.
174      *
175      * @param Content-Type string
176      */

177     public void setContentType(String JavaDoc type) {
178         this.type = type;
179     }
180
181     /**
182      * getName() is not implemented.
183      */

184     public String JavaDoc getName() {
185         return "";
186     }
187
188     /**
189      * Write the content to an OutputStream.
190      *
191      * @param os OutputStream to write the entire content to
192      */

193     public void writeTo(OutputStream os) throws IOException {
194         os.write(data);
195     }
196
197     /**
198      * Return the content as a byte array.
199      *
200      * @return byte array with the content
201      */

202     public byte[] toByteArray() {
203         return data;
204     }
205
206     /**
207      * Return the number of bytes in the content.
208      *
209      * @return size of the byte array, or -1 if not set.
210      */

211     public int getSize() {
212         if (data == null)
213             return -1;
214         else
215             return data.length;
216     }
217
218     /**
219      * Return the content as a String. The Content-Type "charset" parameter
220      * will be used to determine the encoding, and if that's not available or
221      * invalid, "iso-8859-1".
222      *
223      * @return a String with the content
224      */

225     public String JavaDoc getText() {
226         try {
227             return new String JavaDoc(data, MimeUtils.getEncoding(type,
228                 Constants.HEADERVAL_DEFAULT_CHARSET));
229         } catch (UnsupportedEncodingException uex) {
230             try {
231                 return new String JavaDoc(data, Constants.HEADERVAL_DEFAULT_CHARSET);
232             } catch (UnsupportedEncodingException uex1) {
233                 return null;
234             }
235         }
236     }
237 }
238
Popular Tags