1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.mail.internet;
21
22 import java.io.BufferedInputStream;
23 import java.io.BufferedReader;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.OutputStream;
30 import java.io.UnsupportedEncodingException;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.NoSuchElementException;
34 import java.util.StringTokenizer;
35
36 import javax.activation.DataHandler;
37 import javax.activation.DataSource;
38 import javax.mail.MessagingException;
39
40 import org.apache.geronimo.mail.util.ASCIIUtil;
41 import org.apache.geronimo.mail.util.Base64;
42 import org.apache.geronimo.mail.util.Base64DecoderStream;
43 import org.apache.geronimo.mail.util.Base64Encoder;
44 import org.apache.geronimo.mail.util.Base64EncoderStream;
45 import org.apache.geronimo.mail.util.QuotedPrintableDecoderStream;
46 import org.apache.geronimo.mail.util.QuotedPrintableEncoderStream;
47 import org.apache.geronimo.mail.util.QuotedPrintableEncoder;
48 import org.apache.geronimo.mail.util.QuotedPrintable;
49 import org.apache.geronimo.mail.util.SessionUtil;
50 import org.apache.geronimo.mail.util.UUDecoderStream;
51 import org.apache.geronimo.mail.util.UUEncoderStream;
52
53
54
55
56
57
58
59 public class MimeUtility {
60
61 private static final String MIME_FOLDENCODEDWORDS = "mail.mime.foldencodedwords";
62 private static final String MIME_DECODE_TEXT_STRICT = "mail.mime.decodetext.strict";
63 private static final String MIME_FOLDTEXT = "mail.mime.foldtext";
64 private static final int FOLD_THRESHOLD = 76;
65
66 private MimeUtility() {
67 }
68
69 public static final int ALL = -1;
70
71 private static String defaultJavaCharset;
72 private static String escapedChars = "\"\\\r\n";
73 private static String linearWhiteSpace = " \t\r\n";
74
75 private static String QP_WORD_SPECIALS = "=_?\"#$%&'(),.:;<>@[\\]^`{|}~";
76 private static String QP_TEXT_SPECIALS = "=_?";
77
78
79
80 private static Map java2mime;
81 private static Map mime2java;
82
83 static {
84
85 loadCharacterSetMappings();
86 }
87
88 public static InputStream decode(InputStream in, String encoding) throws MessagingException {
89 encoding = encoding.toLowerCase();
90
91
92 if (encoding.equals("binary") || encoding.equals("7bit") || encoding.equals("8bit")) {
93 return in;
94 }
95 else if (encoding.equals("base64")) {
96 return new Base64DecoderStream(in);
97 }
98
99 else if (encoding.equals("uuencode") || encoding.equals("x-uuencode") || encoding.equals("x-uue")) {
100 return new UUDecoderStream(in);
101 }
102 else if (encoding.equals("quoted-printable")) {
103 return new QuotedPrintableDecoderStream(in);
104 }
105 else {
106 throw new MessagingException("Unknown encoding " + encoding);
107 }
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121 public static String decodeText(String text) throws UnsupportedEncodingException {
122
123
124 if (text.indexOf("=?") < 0) {
125 return text;
126 }
127
128
129 if (!SessionUtil.getBooleanProperty(MIME_DECODE_TEXT_STRICT, true)) {
130 return decodeTextNonStrict(text);
131 }
132
133 int offset = 0;
134 int endOffset = text.length();
135
136 int startWhiteSpace = -1;
137 int endWhiteSpace = -1;
138
139 StringBuffer decodedText = new StringBuffer(text.length());
140
141 boolean previousTokenEncoded = false;
142
143 while (offset < endOffset) {
144 char ch = text.charAt(offset);
145
146
147 if (linearWhiteSpace.indexOf(ch) != -1) {
148 startWhiteSpace = offset;
149 while (offset < endOffset) {
150
151 ch = text.charAt(offset);
152 if (linearWhiteSpace.indexOf(ch) != -1) {
153 offset++;
154 }
155 else {
156
157
158 endWhiteSpace = offset;
159 break;
160 }
161 }
162 }
163 else {
164
165 int wordStart = offset;
166
167 while (offset < endOffset) {
168
169 ch = text.charAt(offset);
170 if (linearWhiteSpace.indexOf(ch) == -1) {
171 offset++;
172 }
173 else {
174 break;
175 }
176
177
178 }
179
180 String word = text.substring(wordStart, offset);
181
182 if (word.startsWith("=?")) {
183 try {
184
185 String decodedWord = decodeWord(word);
186
187
188 if (!previousTokenEncoded) {
189 if (startWhiteSpace != -1) {
190 decodedText.append(text.substring(startWhiteSpace, endWhiteSpace));
191 startWhiteSpace = -1;
192 }
193 }
194
195 previousTokenEncoded = true;
196
197 decodedText.append(decodedWord);
198
199
200 continue;
201
202 } catch (ParseException e) {
203 }
204 }
205
206
207 if (startWhiteSpace != -1) {
208 decodedText.append(text.substring(startWhiteSpace, endWhiteSpace));
209 startWhiteSpace = -1;
210 }
211
212 previousTokenEncoded = false;
213 decodedText.append(word);
214 }
215 }
216
217 return decodedText.toString();
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 private static String decodeTextNonStrict(String text) throws UnsupportedEncodingException {
236 int offset = 0;
237 int endOffset = text.length();
238
239 int startWhiteSpace = -1;
240 int endWhiteSpace = -1;
241
242 StringBuffer decodedText = new StringBuffer(text.length());
243
244 boolean previousTokenEncoded = false;
245
246 while (offset < endOffset) {
247 char ch = text.charAt(offset);
248
249
250 if (linearWhiteSpace.indexOf(ch) != -1) {
251 startWhiteSpace = offset;
252 while (offset < endOffset) {
253
254 ch = text.charAt(offset);
255 if (linearWhiteSpace.indexOf(ch) != -1) {
256 offset++;
257 }
258 else {
259
260
261 endWhiteSpace = offset;
262 break;
263 }
264 }
265 }
266 else {
267
268 int wordStart = offset;
269
270 while (offset < endOffset) {
271
272 ch = text.charAt(offset);
273 if (linearWhiteSpace.indexOf(ch) == -1) {
274 offset++;
275 }
276 else {
277 break;
278 }
279
280
281 }
282
283 String word = text.substring(wordStart, offset);
284
285 int decodeStart = 0;
286
287
288 while (decodeStart < word.length()) {
289 int tokenStart = word.indexOf("=?", decodeStart);
290 if (tokenStart == -1) {
291
292
293 if (startWhiteSpace != -1) {
294 decodedText.append(text.substring(startWhiteSpace, endWhiteSpace));
295 startWhiteSpace = -1;
296 }
297
298 previousTokenEncoded = false;
299 decodedText.append(word.substring(decodeStart));
300
301 break;
302 }
303
304 else {
305
306 if (tokenStart != decodeStart) {
307
308
309 if (startWhiteSpace != -1) {
310 decodedText.append(text.substring(startWhiteSpace, endWhiteSpace));
311 startWhiteSpace = -1;
312 }
313
314 previousTokenEncoded = false;
315 decodedText.append(word.substring(decodeStart, tokenStart));
316 }
317
318
319 int tokenEnd = word.indexOf("?=", tokenStart);
320
321 if (tokenEnd == -1) {
322
323
324 if (startWhiteSpace != -1) {
325 decodedText.append(text.substring(startWhiteSpace, endWhiteSpace));
326 startWhiteSpace = -1;
327 }
328
329 previousTokenEncoded = false;
330 decodedText.append(word.substring(tokenStart));
331
332 break;
333 }
334 else {
335
336 decodeStart = tokenEnd + 2;
337
338 String token = word.substring(tokenStart, tokenEnd);
339 try {
340
341 String decodedWord = decodeWord(token);
342
343
344 if (!previousTokenEncoded) {
345 if (startWhiteSpace != -1) {
346 decodedText.append(text.substring(startWhiteSpace, endWhiteSpace));
347 startWhiteSpace = -1;
348 }
349 }
350
351 previousTokenEncoded = true;
352
353 decodedText.append(decodedWord);
354
355
356 continue;
357
358 } catch (ParseException e) {
359 }
360
361
362 if (startWhiteSpace != -1) {
363 decodedText.append(text.substring(startWhiteSpace, endWhiteSpace));
364 startWhiteSpace = -1;
365 }
366
367 previousTokenEncoded = false;
368 decodedText.append(token);
369 }
370 }
371 }
372 }
373 }
374
375 return decodedText.toString();
376 }
377
378
379
380
381
382
383
384
385
386
387
388
389
390 public static String decodeWord(String word) throws ParseException, UnsupportedEncodingException {
391
392
393
394 if (!word.startsWith("=?")) {
395 throw new ParseException("Invalid RFC 2047 encoded-word: " + word);
396 }
397
398 int charsetPos = word.indexOf('?', 2);
399 if (charsetPos == -1) {
400 throw new ParseException("Missing charset in RFC 2047 encoded-word: " + word);
401 }
402
403
404 String charset = word.substring(2, charsetPos).toLowerCase();
405
406
407 int encodingPos = word.indexOf('?', charsetPos + 1);
408 if (encodingPos == -1) {
409 throw new ParseException("Missing encoding in RFC 2047 encoded-word: " + word);
410 }
411
412 String encoding = word.substring(charsetPos + 1, encodingPos);
413
414
415 int encodedTextPos = word.indexOf("?=", encodingPos + 1);
416 if (encodedTextPos == -1) {
417 throw new ParseException("Missing encoded text in RFC 2047 encoded-word: " + word);
418 }
419
420 String encodedText = word.substring(encodingPos + 1, encodedTextPos);
421
422
423 if (encodedText.length() == 0) {
424 return "";
425 }
426
427 try {
428
429 ByteArrayOutputStream out = new ByteArrayOutputStream(encodedText.length());
430
431 byte[] encodedData = encodedText.getBytes("US-ASCII");
432
433
434 if (encoding.equals("B")) {
435 Base64.decode(encodedData, out);
436 }
437
438 else if (encoding.equals("Q")) {
439 QuotedPrintableEncoder dataEncoder = new QuotedPrintableEncoder();
440 dataEncoder.decodeWord(encodedData, out);
441 }
442 else {
443 throw new UnsupportedEncodingException("Unknown RFC 2047 encoding: " + encoding);
444 }
445
446 byte[] decodedData = out.toByteArray();
447 return new String(decodedData, javaCharset(charset));
448 } catch (IOException e) {
449 throw new UnsupportedEncodingException("Invalid RFC 2047 encoding");
450 }
451
452 }
453
454
455
456
457
458
459
460
461
462
463
464 public static OutputStream encode(OutputStream out, String encoding) throws MessagingException {
465
466 if (encoding == null) {
467 return out;
468 }
469
470 encoding = encoding.toLowerCase();
471
472
473 if (encoding.equals("binary") || encoding.equals("7bit") || encoding.equals("8bit")) {
474 return out;
475 }
476 else if (encoding.equals("base64")) {
477 return new Base64EncoderStream(out);
478 }
479
480 else if (encoding.equals("uuencode") || encoding.equals("x-uuencode") || encoding.equals("x-uue")) {
481 return new UUEncoderStream(out);
482 }
483 else if (encoding.equals("quoted-printable")) {
484 return new QuotedPrintableEncoderStream(out);
485 }
486 else {
487 throw new MessagingException("Unknown encoding " + encoding);
488 }
489 }
490
491
492
493
494
495
496
497
498
499
500
501
502 public static OutputStream encode(OutputStream out, String encoding, String filename) throws MessagingException {
503 encoding = encoding.toLowerCase();
504
505
506 if (encoding.equals("binary") || encoding.equals("7bit") || encoding.equals("8bit")) {
507 return out;
508 }
509 else if (encoding.equals("base64")) {
510 return new Base64EncoderStream(out);
511 }
512
513 else if (encoding.equals("uuencode") || encoding.equals("x-uuencode") || encoding.equals("x-uue")) {
514 return new UUEncoderStream(out, filename);
515 }
516 else if (encoding.equals("quoted-printable")) {
517 return new QuotedPrintableEncoderStream(out);
518 }
519 else {
520 throw new MessagingException("Unknown encoding " + encoding);
521 }
522 }
523
524
525 public static String encodeText(String word) throws UnsupportedEncodingException {
526 return encodeText(word, null, null);
527 }
528
529 public static String encodeText(String word, String charset, String encoding) throws UnsupportedEncodingException {
530 return encodeWord(word, charset, encoding, false);
531 }
532
533 public static String encodeWord(String word) throws UnsupportedEncodingException {
534 return encodeWord(word, null, null);
535 }
536
537 public static String encodeWord(String word, String charset, String encoding) throws UnsupportedEncodingException {
538 return encodeWord(word, charset, encoding, true);
539 }
540
541
542 private static String encodeWord(String word, String charset, String encoding, boolean encodingWord) throws UnsupportedEncodingException {
543
544
545 String encoder = ASCIIUtil.getTextTransferEncoding(word);
546
547 if (encoder.equals("7bit")) {
548 return word;
549 }
550
551
552 if (charset == null) {
553 charset = getDefaultMIMECharset();
554 }
555
556
557 if (encoding != null) {
558 if (encoding.equalsIgnoreCase("B")) {
559 encoder = "base64";
560 }
561 else if (encoding.equalsIgnoreCase("Q")) {
562 encoder = "quoted-printable";
563 }
564 else {
565 throw new UnsupportedEncodingException("Unknown transfer encoding: " + encoding);
566 }
567 }
568
569 try {
570
571
572 StringBuffer result = new StringBuffer();
573
574
575
576 int sizeLimit = 75 - 7 - charset.length();
577
578
579 if (encoder.equals("base64")) {
580 Base64Encoder dataEncoder = new Base64Encoder();
581
582
583 encodeBase64(word, result, sizeLimit, charset, dataEncoder, true, SessionUtil.getBooleanProperty(MIME_FOLDENCODEDWORDS, false));
584 }
585 else {
586 QuotedPrintableEncoder dataEncoder = new QuotedPrintableEncoder();
587 encodeQuotedPrintable(word, result, sizeLimit, charset, dataEncoder, true,
588 SessionUtil.getBooleanProperty(MIME_FOLDENCODEDWORDS, false), encodingWord ? QP_WORD_SPECIALS : QP_TEXT_SPECIALS);
589 }
590 return result.toString();
591 } catch (IOException e) {
592 throw new UnsupportedEncodingException("Invalid encoding");
593 }
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615 static private void encodeBase64(String data, StringBuffer out, int sizeLimit, String charset, Base64Encoder encoder, boolean firstSegment, boolean foldSegments) throws IOException
616 {
617
618 byte [] bytes = data.getBytes(javaCharset(charset));
619
620 int estimatedSize = encoder.estimateEncodedLength(bytes);
621
622
623
624 if (estimatedSize > sizeLimit) {
625
626 encodeBase64(data.substring(0, data.length() / 2), out, sizeLimit, charset, encoder, firstSegment, foldSegments);
627
628 encodeBase64(data.substring(data.length() / 2), out, sizeLimit, charset, encoder, false, foldSegments);
629 }
630 else
631 {
632
633
634 if (!firstSegment) {
635 if (foldSegments) {
636 out.append("\r\n");
637 }
638 else {
639 out.append(' ');
640 }
641 }
642
643 encoder.encodeWord(bytes, out, charset);
644 }
645 }
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666 static private void encodeQuotedPrintable(String data, StringBuffer out, int sizeLimit, String charset, QuotedPrintableEncoder encoder,
667 boolean firstSegment, boolean foldSegments, String specials) throws IOException
668 {
669
670 byte [] bytes = data.getBytes(javaCharset(charset));
671
672 int estimatedSize = encoder.estimateEncodedLength(bytes, specials);
673
674
675
676 if (estimatedSize > sizeLimit) {
677
678 encodeQuotedPrintable(data.substring(0, data.length() / 2), out, sizeLimit, charset, encoder, firstSegment, foldSegments, specials);
679
680 encodeQuotedPrintable(data.substring(data.length() / 2), out, sizeLimit, charset, encoder, false, foldSegments, specials);
681 }
682 else
683 {
684
685
686 if (!firstSegment) {
687 if (foldSegments) {
688 out.append("\r\n");
689 }
690 else {
691 out.append(' ');
692 }
693 }
694
695 encoder.encodeWord(bytes, out, charset, specials);
696 }
697 }
698
699
700
701
702
703
704
705
706
707
708
709
710 public static String getEncoding(DataHandler handler) {
711
712
713
714
715
716 DataSource ds = handler.getDataSource();
717 if (ds != null) {
718 return getEncoding(ds);
719 }
720
721 try {
722
723 ContentType content = new ContentType(ds.getContentType());
724
725
726
727
728 ContentCheckingOutputStream checker = new ContentCheckingOutputStream();
729
730 handler.writeTo(checker);
731
732
733 if (content.match("text/*")) {
734 return checker.getTextTransferEncoding();
735 }
736 else {
737 return checker.getBinaryTransferEncoding();
738 }
739
740 } catch (Exception e) {
741
742 return "base64";
743 }
744 }
745
746
747
748
749
750
751
752
753
754
755
756 public static String getEncoding(DataSource source) {
757 InputStream in = null;
758
759 try {
760
761 ContentType content = new ContentType(source.getContentType());
762
763
764 in = source.getInputStream();
765
766 if (!content.match("text/*")) {
767
768
769 return ASCIIUtil.getBinaryTransferEncoding(in);
770 }
771 else {
772 return ASCIIUtil.getTextTransferEncoding(in);
773 }
774 } catch (Exception e) {
775
776
777 return "base64";
778 } finally {
779
780 try {
781 if (in != null) {
782 in.close();
783 }
784 } catch (IOException e) {
785 }
786 }
787 }
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802 public static String quote(String word, String specials) {
803 int wordLength = word.length();
804 boolean requiresQuoting = false;
805
806 for (int i =0; i < wordLength; i++) {
807 char ch = word.charAt(i);
808
809 if (escapedChars.indexOf(ch) >= 0) {
810 return quoteAndEscapeString(word);
811 }
812
813 if (ch < 32 || ch >= 127 || specials.indexOf(ch) >= 0) {
814
815
816 return quoteAndEscapeString(word);
817 }
818 }
819 return word;
820 }
821
822
823
824
825
826
827
828
829
830 private static String quoteAndEscapeString(String word) {
831 int wordLength = word.length();
832
833 StringBuffer buffer = new StringBuffer(wordLength + 10);
834
835 buffer.append('"');
836
837 for (int i = 0; i < wordLength; i++) {
838 char ch = word.charAt(i);
839
840 if (escapedChars.indexOf(ch) >= 0) {
841
842 buffer.append('\\');
843 }
844 buffer.append(ch);
845 }
846
847 buffer.append('"');
848 return buffer.toString();
849 }
850
851
852
853
854
855
856
857
858
859 public static String javaCharset(String charset) {
860
861 if (charset == null) {
862 return null;
863 }
864
865 String mappedCharset = (String)mime2java.get(charset.toLowerCase());
866
867
868 return mappedCharset == null ? charset : mappedCharset;
869 }
870
871
872
873
874
875
876
877
878 public static String mimeCharset(String charset) {
879
880 if (charset == null) {
881 return null;
882 }
883
884 String mappedCharset = (String)java2mime.get(charset.toLowerCase());
885
886
887 return mappedCharset == null ? charset : mappedCharset;
888 }
889
890
891
892
893
894
895
896
897
898
899
900 public static String getDefaultJavaCharset() {
901 String charset = SessionUtil.getProperty("mail.mime.charset");
902 if (charset != null) {
903 return javaCharset(charset);
904 }
905 return SessionUtil.getProperty("file.encoding", "8859_1");
906 }
907
908
909
910
911
912
913
914
915
916
917 static String getDefaultMIMECharset() {
918
919 String charset = SessionUtil.getProperty("mail.mime.charset");
920 if (charset != null) {
921 return charset;
922 }
923
924
925 return mimeCharset(SessionUtil.getProperty("file.encoding", "8859_1"));
926 }
927
928
929
930
931
932
933
934
935
936
937 static private void loadCharacterSetMappings() {
938 java2mime = new HashMap();
939 mime2java = new HashMap();
940
941
942
943 try {
944 InputStream map = javax.mail.internet.MimeUtility.class.getResourceAsStream("/META-INF/javamail.charset.map");
945
946 if (map != null) {
947
948 BufferedReader reader = new BufferedReader(new InputStreamReader(map));
949
950 readMappings(reader, java2mime);
951 readMappings(reader, mime2java);
952 }
953 } catch (Exception e) {
954 }
955
956
957
958
959
960
961 if (java2mime.isEmpty()) {
962 java2mime.put("8859_1", "ISO-8859-1");
963 java2mime.put("iso8859_1", "ISO-8859-1");
964 java2mime.put("iso8859-1", "ISO-8859-1");
965
966 java2mime.put("8859_2", "ISO-8859-2");
967 java2mime.put("iso8859_2", "ISO-8859-2");
968 java2mime.put("iso8859-2", "ISO-8859-2");
969
970 java2mime.put("8859_3", "ISO-8859-3");
971 java2mime.put("iso8859_3", "ISO-8859-3");
972 java2mime.put("iso8859-3", "ISO-8859-3");
973
974 java2mime.put("8859_4", "ISO-8859-4");
975 java2mime.put("iso8859_4", "ISO-8859-4");
976 java2mime.put("iso8859-4", "ISO-8859-4");
977
978 java2mime.put("8859_5", "ISO-8859-5");
979 java2mime.put("iso8859_5", "ISO-8859-5");
980 java2mime.put("iso8859-5", "ISO-8859-5");
981
982 java2mime.put ("8859_6", "ISO-8859-6");
983 java2mime.put("iso8859_6", "ISO-8859-6");
984 java2mime.put("iso8859-6", "ISO-8859-6");
985
986 java2mime.put("8859_7", "ISO-8859-7");
987 java2mime.put("iso8859_7", "ISO-8859-7");
988 java2mime.put("iso8859-7", "ISO-8859-7");
989
990 java2mime.put("8859_8", "ISO-8859-8");
991 java2mime.put("iso8859_8", "ISO-8859-8");
992 java2mime.put("iso8859-8", "ISO-8859-8");
993
994 java2mime.put("8859_9", "ISO-8859-9");
995 java2mime.put("iso8859_9", "ISO-8859-9");
996 java2mime.put("iso8859-9", "ISO-8859-9");
997
998 java2mime.put("sjis", "Shift_JIS");
999 java2mime.put ("jis", "ISO-2022-JP");
1000 java2mime.put("iso2022jp", "ISO-2022-JP");
1001 java2mime.put("euc_jp", "euc-jp");
1002 java2mime.put("koi8_r", "koi8-r");
1003 java2mime.put("euc_cn", "euc-cn");
1004 java2mime.put("euc_tw", "euc-tw");
1005 java2mime.put("euc_kr", "euc-kr");
1006 }
1007
1008 if (mime2java.isEmpty ()) {
1009 mime2java.put("iso-2022-cn", "ISO2022CN");
1010 mime2java.put("iso-2022-kr", "ISO2022KR");
1011 mime2java.put("utf-8", "UTF8");
1012 mime2java.put("utf8", "UTF8");
1013 mime2java.put("ja_jp.iso2022-7", "ISO2022JP");
1014 mime2java.put("ja_jp.eucjp", "EUCJIS");
1015 mime2java.put ("euc-kr", "KSC5601");
1016 mime2java.put("euckr", "KSC5601");
1017 mime2java.put("us-ascii", "ISO-8859-1");
1018 mime2java.put("x-us-ascii", "ISO-8859-1");
1019 }
1020 }
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 static private void readMappings(BufferedReader reader, Map table) throws IOException {
1034
1035 while (true) {
1036 String line = reader.readLine();
1037
1038 if (line == null) {
1039 return;
1040 }
1041
1042
1043 line = line.trim();
1044
1045 if (line.length() == 0 || line.startsWith("#")) {
1046 continue;
1047 }
1048
1049
1050 if (line.startsWith("--") && line.endsWith("--")) {
1051 return;
1052 }
1053
1054
1055 StringTokenizer tokenizer = new StringTokenizer(line, " \t");
1056
1057 try {
1058 String from = tokenizer.nextToken().toLowerCase();
1059 String to = tokenizer.nextToken();
1060
1061 table.put(from, to);
1062 } catch (NoSuchElementException e) {
1063
1064 }
1065 }
1066 }
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079 public static String fold(int used, String s) {
1080
1081 if (!SessionUtil.getBooleanProperty(MIME_FOLDTEXT, true)) {
1082 return s;
1083 }
1084
1085 int end;
1086
1087
1088
1089 for (end = s.length() - 1; end >= 0; end--) {
1090 int ch = s.charAt(end);
1091 if (ch != ' ' && ch != '\t' ) {
1092 break;
1093 }
1094 }
1095
1096
1097 if (end != s.length() - 1) {
1098 s = s.substring(0, end + 1);
1099 }
1100
1101
1102 if (s.length() + used <= FOLD_THRESHOLD) {
1103 return s;
1104 }
1105
1106
1107
1108
1109 StringBuffer newString = new StringBuffer(s.length() + 8);
1110
1111
1112
1113 while (used + s.length() > FOLD_THRESHOLD) {
1114 int breakPoint = -1;
1115 char breakChar = 0;
1116
1117
1118 for (int i = 0; i < s.length(); i++) {
1119
1120 if (used + i > FOLD_THRESHOLD) {
1121
1122
1123 if (breakPoint != -1) {
1124 break;
1125 }
1126 }
1127 char ch = s.charAt(i);
1128
1129
1130 if (ch == ' ' || ch == '\t') {
1131
1132 breakPoint = i;
1133
1134 breakChar = ch;
1135 i++;
1136 while (i < s.length()) {
1137 ch = s.charAt(i);
1138 if (ch != ' ' && ch != '\t') {
1139 break;
1140 }
1141 i++;
1142 }
1143 }
1144
1145 else if (ch == '\n') {
1146 newString.append('\\');
1147 newString.append('\n');
1148 }
1149 else if (ch == '\r') {
1150 newString.append('\\');
1151 newString.append('\n');
1152 i++;
1153
1154 if (i < s.length() && s.charAt(i) == '\n') {
1155 newString.append('\r');
1156 }
1157 }
1158
1159 }
1160
1161 if (breakPoint == -1) {
1162 newString.append(s);
1163 return newString.toString();
1164 }
1165 newString.append(s.substring(0, breakPoint));
1166 newString.append("\r\n");
1167 newString.append(breakChar);
1168
1169 s = s.substring(breakPoint + 1);
1170
1171 used = 1;
1172 }
1173
1174
1175 newString.append(s);
1176 return newString.toString();
1177 }
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188 public static String unfold(String s) {
1189
1190 if (!SessionUtil.getBooleanProperty(MIME_FOLDTEXT, true)) {
1191 return s;
1192 }
1193
1194
1195 if (s.indexOf('\n') < 0 && s.indexOf('\r') < 0) {
1196 return s;
1197 }
1198
1199
1200 int length = s.length();
1201
1202 StringBuffer newString = new StringBuffer(length);
1203
1204
1205 for (int i = 0; i < length; i++) {
1206 char ch = s.charAt(i);
1207
1208
1209
1210 if (ch == '\\') {
1211
1212 if (i == length - 1) {
1213 newString.append(ch);
1214 }
1215 else {
1216 int nextChar = s.charAt(i + 1);
1217
1218
1219 if (nextChar == '\n') {
1220 newString.append('\n');
1221 i++;
1222 }
1223 else if (nextChar == '\r') {
1224
1225 if (i == length - 2 || s.charAt(i + 2) != '\r') {
1226 newString.append('\r');
1227 i++;
1228 }
1229 else {
1230
1231 newString.append('\r');
1232 newString.append('\n');
1233 i += 2;
1234 }
1235 }
1236 else {
1237
1238 newString.append(ch);
1239 }
1240 }
1241 }
1242
1243 else if (ch == '\n' || ch == '\r') {
1244
1245 int lineBreak = i;
1246 boolean CRLF = false;
1247
1248 if (ch == '\r') {
1249
1250 if (i < length - 1 && s.charAt(i + 1) == '\n') {
1251 i++;
1252
1253 CRLF = true;
1254 }
1255 }
1256
1257
1258 int scan = i + 1;
1259
1260
1261
1262 if (scan < length && s.charAt(scan) == ' ') {
1263
1264 newString.append(' ');
1265
1266
1267 i = scan + 1;
1268 while (i < length && s.charAt(i) == ' ') {
1269 i++;
1270 }
1271
1272 i--;
1273 }
1274 else {
1275
1276 if (CRLF) {
1277 newString.append("\r\n");
1278 }
1279 else {
1280 newString.append(ch);
1281 }
1282 }
1283 }
1284 else {
1285
1286 newString.append(ch);
1287 }
1288 }
1289 return newString.toString();
1290 }
1291 }
1292
1293
1294
1295
1296
1297
1298
1299 class ContentCheckingOutputStream extends OutputStream {
1300 private int asciiChars = 0;
1301 private int nonAsciiChars = 0;
1302 private boolean containsLongLines = false;
1303 private boolean containsMalformedEOL = false;
1304 private int previousChar = 0;
1305 private int span = 0;
1306
1307 ContentCheckingOutputStream() {
1308 }
1309
1310 public void write(byte[] data) throws IOException {
1311 write(data, 0, data.length);
1312 }
1313
1314 public void write(byte[] data, int offset, int length) throws IOException {
1315 for (int i = 0; i < length; i++) {
1316 write(data[offset + i]);
1317 }
1318 }
1319
1320 public void write(int ch) {
1321
1322
1323 if (ch == '\n' || ch == '\r') {
1324
1325 if (ch == '\n') {
1326
1327 if (previousChar != '\r') {
1328 containsMalformedEOL = true;
1329 }
1330 }
1331
1332 span = 0;
1333 }
1334 else {
1335 span++;
1336
1337 if (span > 998) {
1338 containsLongLines = true;
1339 }
1340
1341
1342 if (!ASCIIUtil.isAscii(ch)) {
1343 nonAsciiChars++;
1344 }
1345 else {
1346 asciiChars++;
1347 }
1348 }
1349 previousChar = ch;
1350 }
1351
1352
1353 public String getBinaryTransferEncoding() {
1354 if (nonAsciiChars != 0 || containsLongLines || containsMalformedEOL) {
1355 return "base64";
1356 }
1357 else {
1358 return "7bit";
1359 }
1360 }
1361
1362 public String getTextTransferEncoding() {
1363
1364 if (nonAsciiChars == 0) {
1365
1366
1367 if (containsLongLines) {
1368 return "quoted-printable";
1369 }
1370 else {
1371
1372 return "7bit";
1373 }
1374 }
1375 else {
1376
1377 if (nonAsciiChars > asciiChars) {
1378 return "base64";
1379 }
1380 else {
1381
1382 return "quoted-printable";
1383 }
1384 }
1385 }
1386 }