1
2
3
4
5
6 package com.ozacc.mail.fetch.impl.sk_jp;
7
8 import java.io.ByteArrayInputStream;
9 import java.io.ByteArrayOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12
13 import javax.activation.DataSource;
14 import javax.mail.MessageAware;
15 import javax.mail.MessagingException;
16 import javax.mail.Part;
17 import javax.mail.internet.ContentType;
18 import javax.mail.internet.MimeBodyPart;
19 import javax.mail.internet.MimeMessage;
20 import javax.mail.internet.ParseException;
21
22 import com.ozacc.mail.fetch.impl.sk_jp.io.ByteToCharUTF7;
23
24 /***
25 * Content-Type:の不適合をISO-2022-JPに補正します。
26 * さらにcharset=UTF-7の場合にUTF-16のストリームに変換してgetContent()を
27 * 無理やり成功させます。<BR>
28 * また、未知のTES(Content-Transfer-Encoding:)だった場合に、"7bit"
29 * と見なしてボディを取得します。
30 * 使用方法は<PRE>
31 * Object o = new DataHandler(
32 * new CorrectedContentTypeDataSourceUTF7Support(part, charset)
33 * ).getContent();
34 * </PRE><P>のようになります。</P><P>
35 * スレッドセーフではありませんので利用者側で排他制御を行ってください。
36 * </P>
37 * @author Shin
38 * @version $Revision: 1.1.2.1 $ $Date: 2004/09/29 00:57:59 $
39 */
40 class CorrectedContentTypeDataSourceUTF7Support extends CorrectedContentTypeDataSource {
41
42 private boolean utf7 = false;
43
44 public CorrectedContentTypeDataSourceUTF7Support() {}
45
46 public CorrectedContentTypeDataSourceUTF7Support(DataSource dataSource, String defaultCharset) {
47 super(dataSource, defaultCharset);
48 }
49
50 public CorrectedContentTypeDataSourceUTF7Support(Part part, String defaultCharset)
51 throws MessagingException {
52 super(part, defaultCharset);
53 }
54
55 public void setDataSource(DataSource newSource) {
56 super.setDataSource(newSource);
57 utf7 = false;
58 }
59
60 public void setDefaultCharset(String defaultCharset) {
61 super.setDefaultCharset(defaultCharset);
62 utf7 = false;
63 }
64
65 public String getContentType() {
66 try {
67 ContentType contentType = new ContentType(super.getContentType());
68 String specifiedCharset = contentType.getParameter("charset");
69 if ("UTF-7".equalsIgnoreCase(specifiedCharset)) {
70
71
72 contentType.setParameter("charset", "UTF-16");
73 utf7 = true;
74 }
75 return contentType.toString();
76 } catch (ParseException e) {
77 throw new InternalError();
78 }
79 }
80
81 public InputStream getInputStream() throws IOException {
82 InputStream in = null;
83 if (isInvalidEncodingAsMultipart()) {
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170