KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8 package org.jboss.axis.attachments;
9
10 import javax.activation.DataSource JavaDoc;
11 import javax.mail.internet.MimeMultipart JavaDoc;
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.OutputStream JavaDoc;
17
18 public class MimeMultipartDataSource implements DataSource JavaDoc
19 {
20    public static final String JavaDoc CONTENT_TYPE = "multipart/mixed";
21
22    private final String JavaDoc name;
23    private final String JavaDoc contentType;
24    private byte[] data;
25    private ByteArrayInputStream JavaDoc is;
26    private ByteArrayOutputStream JavaDoc os;
27
28    public MimeMultipartDataSource(String JavaDoc name, MimeMultipart JavaDoc data)
29    {
30       this.name = name;
31       this.contentType = data == null ? CONTENT_TYPE : data.getContentType();
32       os = new ByteArrayOutputStream JavaDoc();
33       try
34       {
35          if (data != null)
36          {
37             data.writeTo(os);
38          }
39       }
40       catch (Exception JavaDoc e)
41       {
42          // Is this sufficient?
43
}
44    } // ctor
45

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

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

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

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