KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > activation > StreamDataSource


1 /**
2  * Title: Oyster Project
3  * Description: Creating S/MIME email transport capabilities.
4  * Copyright: Copyright (c) 2001
5  * @Author Vladimir Radisic
6  * @Version 2.1.5
7  */

8
9 package org.enhydra.oyster.activation;
10
11 import org.enhydra.oyster.exception.SMIMEException;
12 import org.enhydra.oyster.util.ConvertAssist;
13 import javax.activation.DataSource JavaDoc;
14 import javax.activation.MimetypesFileTypeMap JavaDoc;
15 import java.io.ByteArrayInputStream JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19
20
21 /**
22  * StreamDataSource represents implementation of DataSource interfaces, and as
23  * result of that, objects of this class could be used within MimeBodyPart
24  * objects to help them to read data from objects which they conveys. This class
25  * is designed to help in getting data from objects of type InputStream and all
26  * its subclasses.
27  */

28 public class StreamDataSource implements DataSource JavaDoc {
29
30 /**
31  * Storage for obtained content-type.
32  */

33   private String JavaDoc contentType = null;
34
35 /**
36  * Storage for obtained file name.
37  */

38   private String JavaDoc fileName = "";
39
40 /**
41  * Storage for data derived from InputStream.
42  */

43   private byte[] att = null;
44
45 /**
46  * Constructs StreamDataSource with given input stream and coresponding string
47  * which contains virtual or real file name. Extension of file name is used to
48  * get appropriate mime-type. All mime-types which are predefined according to
49  * particular file extensions can be found in mime.types file in META-INF directory
50  * from smime.jar file. This file can be changed or extended so that it could adjust
51  * mime-types to aditional requests. For more information see Java documentation
52  * related to class MimetypesFileTypeMap.
53  */

54   public StreamDataSource(InputStream JavaDoc in0, String JavaDoc fileName0) throws SMIMEException {
55
56     att = ConvertAssist.inStreamToByteArray(in0);
57
58     if(fileName0 != null) {
59       contentType = new MimetypesFileTypeMap JavaDoc().getContentType(fileName0);
60       fileName = new String JavaDoc(fileName0);
61     }
62     if( contentType == null || contentType.equalsIgnoreCase("") )
63       contentType = "application/octet-stream";
64
65     contentType = contentType + "; name=\"" + fileName0 + "\"";
66   }
67
68 /**
69  * Sets content type. Using of this metod should be avoided because Content-Type
70  * is set in the process of construction of StreamDataSource object, by using
71  * information got from "fileName0" parameter. This method will override
72  * value set after construction. This method will be used when construction is
73  * performed with null value of "fileName0" parameter, or in case when automatic
74  * obtaining of value for content-type did not satisfy what is expected.
75  * @param contType0 Content-Type for MIME message header field
76  */

77  public void setContentType(String JavaDoc contType0) {
78     contentType = contType0;
79  }
80
81 /**
82  * Implements getContentType method from DataSource interface
83  * @return Content-Type for MIME message header field
84  */

85   public String JavaDoc getContentType () {
86     return contentType;
87   }
88
89 /**
90  * Implements getInputStream method from DataSource interface
91  * @return CMS enveloped object
92  * @exception IOException
93  */

94   public InputStream JavaDoc getInputStream () throws IOException JavaDoc {
95      return new ByteArrayInputStream JavaDoc(att);
96   }
97
98 /**
99  * Implements getName method from DataSource interface
100  * @return Name: EnvelopedDataContentInfo
101  */

102   public String JavaDoc getName () {
103     return fileName;
104   }
105
106 /**
107  * Implements getOutputStream method from DataSource interface. This method is
108  * not in use.
109  * @return nothing
110  * @exception IOException is always thrown when this method is used.
111  */

112   public OutputStream JavaDoc getOutputStream () throws IOException JavaDoc {
113     throw new IOException JavaDoc("StreamDataSource does not support getOutputStream()");
114   }
115
116
117 }
Popular Tags