KickJava   Java API By Example, From Geeks To Geeks.

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


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.mail.internet.MimeMultipart 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
26 public class MimeMultipartDataSource implements DataSource JavaDoc {
27     public static final String JavaDoc CONTENT_TYPE = "multipart/mixed";
28
29     private final String JavaDoc name;
30     private final String JavaDoc contentType;
31     private byte[] data;
32     private ByteArrayOutputStream JavaDoc os;
33
34     public MimeMultipartDataSource(String JavaDoc name, MimeMultipart JavaDoc data) {
35         this.name = name;
36         this.contentType = data == null ? CONTENT_TYPE : data.getContentType();
37         os = new ByteArrayOutputStream JavaDoc();
38         try {
39             if (data != null) {
40                 data.writeTo(os);
41             }
42         }
43         catch (Exception JavaDoc e) {
44             // Is this sufficient?
45
}
46     } // ctor
47

48     public String JavaDoc getName() {
49         return name;
50     } // getName
51

52     public String JavaDoc getContentType() {
53         return contentType;
54     } // getContentType
55

56     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
57         if (os.size() != 0) {
58             data = os.toByteArray();
59             os.reset();
60         }
61         return new ByteArrayInputStream JavaDoc(data == null ? new byte[0] : data);
62     } // getInputStream
63

64     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
65         if (os.size() != 0) {
66             data = os.toByteArray();
67             os.reset();
68         }
69         return os;
70     } // getOutputStream
71
} // class MimeMultipartDataSource
72
Popular Tags