File: MimeUtility.java

package info (click to toggle)
libgnumail-java 1.0-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,620 kB
  • ctags: 2,193
  • sloc: java: 17,470; sh: 9,912; makefile: 432; xml: 159
file content (1154 lines) | stat: -rw-r--r-- 34,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
/*
 * MimeUtility.java
 * Copyright (C) 2002, 2004 The Free Software Foundation
 * 
 * This file is part of GNU JavaMail, a library.
 * 
 * GNU JavaMail is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * GNU JavaMail is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * As a special exception, if you link this library with other files to
 * produce an executable, this library does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * This exception does not however invalidate any other reasons why the
 * executable file might be covered by the GNU General Public License.
 */

package javax.mail.internet;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.Session;

import gnu.inet.util.LineInputStream;
import gnu.mail.util.Base64InputStream;
import gnu.mail.util.Base64OutputStream;
import gnu.mail.util.BOutputStream;
import gnu.mail.util.QInputStream;
import gnu.mail.util.QOutputStream;
import gnu.mail.util.QPInputStream;
import gnu.mail.util.QPOutputStream;
import gnu.mail.util.UUDecoderStream;
import gnu.mail.util.UUEncoderStream;

/**
 * This is a utility class that provides various MIME related functionality.
 * <p>
 * There are a set of methods to encode and decode MIME headers as per 
 * RFC 2047. A brief description on handling such headers is given below:
 * <p>
 * RFC 822 mail headers must contain only US-ASCII characters. Headers that
 * contain non US-ASCII characters must be encoded so that they contain only
 * US-ASCII characters. Basically, this process involves using either BASE64 
 * or QP to encode certain characters. RFC 2047 describes this in detail.
 * <p>
 * In Java, Strings contain (16 bit) Unicode characters. ASCII is a subset of
 * Unicode (and occupies the range 0 - 127). A String that contains only ASCII
 * characters is already mail-safe. If the String contains non US-ASCII
 * characters, it must be encoded. An additional complexity in this step is that
 * since Unicode is not yet a widely used charset, one might want to first
 * charset-encode the String into another charset and then do the
 * transfer-encoding.
 * <p>
 * Note that to get the actual bytes of a mail-safe String (say, for sending 
 * over SMTP), one must do
 * <pre>

        byte[] bytes = string.getBytes("iso-8859-1");

   <pre>
 * The <code>setHeader()</code> and <code>addHeader()</code> methods on
 * MimeMessage and MimeBodyPart assume that the given header values are 
 * Unicode strings that contain only US-ASCII characters. Hence the callers 
 * of those methods must insure that the values they pass do not contain non 
 * US-ASCII characters. The methods in this class help do this.
 * <p>
 * The <code>getHeader()</code> family of methods on MimeMessage and 
 * MimeBodyPart return the raw header value. These might be encoded as per 
 * RFC 2047, and if so, must be decoded into Unicode Strings.
 * The methods in this class help to do this.
 *
 * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
 * @version 1.3
 */
public class MimeUtility
{

  /*
   * Uninstantiable.
   */
  private MimeUtility()
  {
  }
  
  /**
   * Get the content-transfer-encoding that should be applied to the input
   * stream of this datasource, to make it mailsafe.
   * <p>
   * The algorithm used here is:
   * <ul>
   * <li>If the primary type of this datasource is "text" and if all the bytes
   * in its input stream are US-ASCII, then the encoding is "7bit". If more
   * than half of the bytes are non-US-ASCII, then the encoding is "base64".
   * If less than half of the bytes are non-US-ASCII, then the encoding is
   * "quoted-printable".
   * <li>If the primary type of this datasource is not "text", then if all the
   * bytes of its input stream are US-ASCII, the encoding is "7bit". If
   * there is even one non-US-ASCII character, the encoding is "base64".
   * @param ds DataSource
   * @return the encoding.
   * This is either "7bit", "quoted-printable" or "base64"
   */
  public static String getEncoding(DataSource ds)
  {
    String encoding = "base64";
    InputStream is = null;
    try
    {
      is = ds.getInputStream();
      ContentType ct = new ContentType(ds.getContentType());
      boolean text = ct.match("text/*");
      switch (asciiStatus(is, ALL, text))
      {
        case ALL_ASCII:
          encoding = "7bit";
          break;
        case MAJORITY_ASCII:
          if (text)
            encoding = "quoted-printable";
          break;
      }
    }
    catch (Exception e)
    {
      
    }
    try
    {
      is.close();
    }
    catch (IOException e)
    {
    }
    return encoding;
  }
  
  /**
   * Same as getEncoding(DataSource) except that instead of reading the data
   * from an InputStream it uses the writeTo method to examine the data.
   * This is more efficient in the common case of a DataHandler created 
   * with an object and a MIME type (for example, a "text/plain" String)
   * because all the I/O is done in this thread.
   * In the case requiring an InputStream the DataHandler uses a thread,
   * a pair of pipe streams, and the writeTo method to produce the data.
   */
  public static String getEncoding(DataHandler dh)
  {
    String encoding = "base64";
    if (dh.getName()!=null)
      return getEncoding(dh.getDataSource());
    try
    {
      ContentType ct = new ContentType(dh.getContentType());
      boolean text = ct.match("text/*");
      
      AsciiOutputStream aos = new AsciiOutputStream(!text,
          encodeeolStrict() && !text);
      try
      {
        dh.writeTo(aos);
      }
      catch (IOException e)
      {
      }
      switch (aos.status())
      {
        case ALL_ASCII:
          encoding = "7bit";
          break;
        case MAJORITY_ASCII:
          if (text)
            encoding = "quoted-printable";
          break;
      }
    }
    catch (Exception e)
    {
    }
    return encoding;
  }

  /**
   * Decode the given input stream.
   * The Input stream returned is the decoded input stream.
   * All the encodings defined in RFC 2045 are supported here.
   * They include "base64", "quoted-printable", "7bit", "8bit", and
   * "binary". In addition, "uuencode" is also supported.
   * @param is input stream
   * @param encoding the encoding of the stream.
   * @return decoded input stream.
   */
  public static InputStream decode(InputStream is, String encoding)
    throws MessagingException
  {
    if (encoding.equalsIgnoreCase("base64"))
      return new Base64InputStream(is);
    if (encoding.equalsIgnoreCase("quoted-printable"))
      return new QPInputStream(is);
    if (encoding.equalsIgnoreCase("uuencode") || 
        encoding.equalsIgnoreCase("x-uuencode"))
      return new UUDecoderStream(is);
    if (encoding.equalsIgnoreCase("binary") ||
        encoding.equalsIgnoreCase("7bit") ||
        encoding.equalsIgnoreCase("8bit"))
      return is;
    throw new MessagingException("Unknown encoding: "+encoding);
  }

  /**
   * Wrap an encoder around the given output stream.
   * All the encodings defined in RFC 2045 are supported here.
   * They include "base64", "quoted-printable", "7bit", "8bit" and "binary".
   * In addition, "uuencode" is also supported.
   * @param os output stream
   * @param encoding the encoding of the stream.
   * @return output stream that applies the specified encoding.
   */
  public static OutputStream encode(OutputStream os, String encoding)
    throws MessagingException
  {
    if (encoding==null)
      return os;
    if (encoding.equalsIgnoreCase("base64"))
      return new Base64OutputStream(os);
    if (encoding.equalsIgnoreCase("quoted-printable"))
      return new QPOutputStream(os);
    if (encoding.equalsIgnoreCase("uuencode") ||
        encoding.equalsIgnoreCase("x-uuencode"))
      return new UUEncoderStream(os);
    if (encoding.equalsIgnoreCase("binary") || 
        encoding.equalsIgnoreCase("7bit") || 
        encoding.equalsIgnoreCase("8bit"))
      return os;
    else
      throw new MessagingException("Unknown encoding: "+encoding);
  }

  /**
   * Wrap an encoder around the given output stream.
   * All the encodings defined in RFC 2045 are supported here.
   * They include "base64", "quoted-printable", "7bit", "8bit" and "binary".
   * In addition, "uuencode" is also supported. The <code>filename</code>
   * parameter is used with the "uuencode" encoding and is included in the 
   * encoded output.
   * @param os output stream
   * @param encoding the encoding of the stream.
   * @param filename name for the file being encoded (only used with uuencode)
   * @return output stream that applies the specified encoding.
   */
  public static OutputStream encode(OutputStream os, String encoding,
      String filename)
    throws MessagingException
  {
    if (encoding==null)
      return os;
    if (encoding.equalsIgnoreCase("base64"))
      return new Base64OutputStream(os);
    if (encoding.equalsIgnoreCase("quoted-printable"))
      return new QPOutputStream(os);
    if (encoding.equalsIgnoreCase("uuencode") ||
        encoding.equalsIgnoreCase("x-uuencode"))
      return new UUEncoderStream(os, filename);
    if (encoding.equalsIgnoreCase("binary") || 
        encoding.equalsIgnoreCase("7bit") || 
        encoding.equalsIgnoreCase("8bit"))
      return os;
    else
      throw new MessagingException("Unknown encoding: "+encoding);
  }

  /**
   * Encode a RFC 822 "text" token into mail-safe form as per RFC 2047.
   * <p>
   * The given Unicode string is examined for non US-ASCII characters. If the
   * string contains only US-ASCII characters, it is returned as-is. If the
   * string contains non US-ASCII characters, it is first character-encoded
   * using the platform's default charset, then transfer-encoded using either
   * the B or Q encoding. The resulting bytes are then returned as a Unicode
   * string containing only ASCII characters.
   * <p>
   * Note that this method should be used to encode only "unstructured" 
   * RFC 822 headers.
   * <p>
   * Example of usage:
   * <pre>
    MimePart part = ...
    String rawvalue = "FooBar Mailer, Japanese version 1.1"
    try {
      // If we know for sure that rawvalue contains only US-ASCII
      // characters, we can skip the encoding part
      part.setHeader("X-mailer", MimeUtility.encodeText(rawvalue));
    } catch (UnsupportedEncodingException e) {
      // encoding failure
    } catch (MessagingException me) {
      // setHeader() failure
    }
    </pre>
   * @param text unicode string
   * @return Unicode string containing only US-ASCII characters
   * @param UnsupportedEncodingException if the encoding fails
   */
  public static String encodeText(String text)
    throws UnsupportedEncodingException
  {
    return encodeText(text, null, null);
  }

  /**
   * Encode a RFC 822 "text" token into mail-safe form as per RFC 2047.
   * <p>
   * The given Unicode string is examined for non US-ASCII characters. If the
   * string contains only US-ASCII characters, it is returned as-is. If the
   * string contains non US-ASCII characters, it is first character-encoded
   * using the platform's default charset, then transfer-encoded using either
   * the B or Q encoding. The resulting bytes are then returned as a Unicode
   * string containing only ASCII characters.
   * <p>
   * Note that this method should be used to encode only "unstructured" 
   * RFC 822 headers.
   * <p>
   * @param text the header value
   * @param charset the charset. If this parameter is null, the platform's
   * default chatset is used.
   * @param encoding the encoding to be used. 
   * Currently supported values are "B" and "Q".
   * If this parameter is null, then the "Q" encoding is used if most of the
   * characters to be encoded are in the ASCII charset, otherwise "B"
   * encoding is used.
   * @return Unicode string containing only US-ASCII characters
   */
  public static String encodeText(String text, String charset, String encoding)
    throws UnsupportedEncodingException
  {
    return encodeWord(text, charset, encoding, false);
  }

  /**
   * Decode "unstructured" headers, that is, headers that are defined as '*text'
   * as per RFC 822.
   * <p>
   * The string is decoded using the algorithm specified in RFC 2047, Section
   * 6.1.1. If the charset-conversion fails for any sequence, an
   * UnsupportedEncodingException is thrown. If the String is not an RFC 2047
   * style encoded header, it is returned as-is
   * <p>
   * Example of usage:
   * <pre>
    MimePart part = ...
    String rawvalue = null;
    String  value = null;
    try {
      if ((rawvalue = part.getHeader("X-mailer")[0]) != null)
        value = MimeUtility.decodeText(rawvalue);
    } catch (UnsupportedEncodingException e) {
        // Don't care
        value = rawvalue;
    } catch (MessagingException me) { }
    return value;
    <pre>
   * @param etext the possibly encoded value
   * @exception UnsupportedEncodingException if the charset conversion failed.
   */
  public static String decodeText(String etext)
    throws UnsupportedEncodingException
  {
    String delimiters = "\t\n\r ";
    if (etext.indexOf("=?")<0)
      return etext;
    StringTokenizer st = new StringTokenizer(etext, delimiters, true);
    StringBuffer buffer = new StringBuffer();
    StringBuffer extra = new StringBuffer();
    boolean decoded = false;
    while (st.hasMoreTokens()) 
    {
      String token = st.nextToken();
      char c = token.charAt(0);
      if (delimiters.indexOf(c)>-1)
        extra.append(c);
      else
      {
        try
        {
          token = decodeWord(token);
          if (!decoded && extra.length()>0)
            buffer.append(extra);
          decoded = true;
        }
        catch (ParseException e)
        {
          if (!decodetextStrict())
            token = decodeInnerText(token);
          if (extra.length()>0)
            buffer.append(extra);
          decoded = false;
        }
        buffer.append(token);
        extra.setLength(0);
      }
    }
    return buffer.toString();
  }

  /**
   * Encode a RFC 822 "word" token into mail-safe form as per RFC 2047.
   * <p>
   * The given Unicode string is examined for non US-ASCII characters.
   * If the string contains only US-ASCII characters, it is returned as-is.
   * If the string contains non US-ASCII characters, it is first 
   * character-encoded using the platform's default charset, then 
   * transfer-encoded using either the B or Q encoding.
   * The resulting bytes are then returned as a Unicode string containing 
   * only ASCII characters.
   * <p>
   * This method is meant to be used when creating RFC 822 "phrases". The
   * InternetAddress class, for example, uses this to encode it's 'phrase'
   * component.
   * @param text unicode string
   * @return Unicode string containing only US-ASCII characters.
   * @exception UnsupportedEncodingException if the encoding fails
   */
  public static String encodeWord(String text)
    throws UnsupportedEncodingException
  {
    return encodeWord(text, null, null);
  }

  /**
   * Encode a RFC 822 "word" token into mail-safe form as per RFC 2047.
   * <p>
   * The given Unicode string is examined for non US-ASCII characters.
   * If the string contains only US-ASCII characters, it is returned as-is.
   * If the string contains non US-ASCII characters, it is first 
   * character-encoded using the platform's default charset, then 
   * transfer-encoded using either the B or Q encoding.
   * The resulting bytes are then returned as a Unicode string containing 
   * only ASCII characters.
   * <p>
   * @param text unicode string
   * @param charset the MIME charset
   * @param encoding the encoding to be used.
   * Currently supported values are "B" and "Q".
   * If this parameter is null, then the "Q" encoding is used if most of the
   * characters to be encoded are in the ASCII charset, otherwise "B"
   * encoding is used.
   * @return Unicode string containing only US-ASCII characters
   * @exception UnsupportedEncodingException if the encoding fails
   */
  public static String encodeWord(String text, String charset, String encoding)
    throws UnsupportedEncodingException
  {
    return encodeWord(text, charset, encoding, true);
  }

  private static String encodeWord(String text, String charset, 
      String encoding, boolean word)
    throws UnsupportedEncodingException
  {
    if (asciiStatus(text.getBytes())==ALL_ASCII)
      return text;
    String javaCharset;
    if (charset==null)
    {
      javaCharset = getDefaultJavaCharset();
      charset = mimeCharset(javaCharset);
    }
    else
      javaCharset = javaCharset(charset);
    if (encoding==null)
    {
      byte[] bytes = text.getBytes(javaCharset);
      if (asciiStatus(bytes)!=MINORITY_ASCII)
        encoding = "Q";
      else
        encoding = "B";
    }
    boolean bEncoding;
    if (encoding.equalsIgnoreCase("B"))
      bEncoding = true;
    else if (encoding.equalsIgnoreCase("Q"))
      bEncoding = false;
    else
      throw new UnsupportedEncodingException("Unknown transfer encoding: "+
          encoding);
    
    StringBuffer encodingBuffer = new StringBuffer();
    encodingBuffer.append("=?");
    encodingBuffer.append(charset);
    encodingBuffer.append("?");
    encodingBuffer.append(encoding);
    encodingBuffer.append("?");
    
    StringBuffer buffer = new StringBuffer();
    encodeBuffer(buffer,
        text, 
        javaCharset, 
        bEncoding, 
        68 - charset.length(), 
        encodingBuffer.toString(), 
        true,
        word);
    return buffer.toString();
  }

  private static void encodeBuffer(StringBuffer buffer,
      String text, 
      String charset, 
      boolean bEncoding, 
      int max, 
      String encoding,
      boolean keepTogether, 
      boolean word)
    throws UnsupportedEncodingException
  {
    byte[] bytes = text.getBytes(charset);
    int elen;
    if (bEncoding)
      elen = BOutputStream.encodedLength(bytes);
    else
      elen = QOutputStream.encodedLength(bytes, word);
    int len = text.length();
    if (elen>max && len>1)
    {
      encodeBuffer(buffer,
          text.substring(0, len/2), 
          charset, 
          bEncoding, 
          max, 
          encoding, 
          keepTogether, 
          word);
      encodeBuffer(buffer,
          text.substring(len/2, len),
          charset,
          bEncoding,
          max,
          encoding,
          false,
          word);
    }
    else
    {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      OutputStream os = null;
      if (bEncoding)
        os = new BOutputStream(bos);
      else
        os = new QOutputStream(bos, word);
      try
      {
        os.write(bytes);
        os.close();
      }
      catch (IOException e)
      {
      }
      bytes = bos.toByteArray();
      if (!keepTogether)
        buffer.append("\r\n ");
      buffer.append(encoding);
      for (int i = 0; i<bytes.length; i++)
        buffer.append((char)bytes[i]);
      
      buffer.append("?=");
    }
  }

  /**
   * The string is parsed using the rules in RFC 2047 for parsing an
   * "encoded-word".
   * If the parse fails, a ParseException is thrown. Otherwise, it is 
   * transfer-decoded, and then charset-converted into Unicode. If the
   * charset-conversion fails, an UnsupportedEncodingException is thrown.
   * @param eword the possibly encoded value
   * @exception ParseException if the string is not an encoded-word as per 
   * RFC 2047.
   * @exception UnsupportedEncodingException if the charset conversion
   * failed.
   */
  public static String decodeWord(String text)
    throws ParseException, UnsupportedEncodingException
  {
    if (!text.startsWith("=?"))
      throw new ParseException();
    int start = 2;
    int end = text.indexOf('?', start);
    if (end<0)
      throw new ParseException();
    String charset = javaCharset(text.substring(start, end));
    start = end + 1;
    end = text.indexOf('?', start);
    if (end<0)
      throw new ParseException();
    String encoding = text.substring(start, end);
    start = end + 1;
    end = text.indexOf("?=", start);
    if (end<0)
      throw new ParseException();
    text = text.substring(start, end);
    try
    {
      // The characters in the remaining string must all be 7-bit clean.
      // Therefore it is safe just to copy them verbatim into a byte array.
      char[] chars = text.toCharArray();
      int len = chars.length;
      byte[] bytes = new byte[len];
      for (int i = 0; i<len; i++)
        bytes[i] = (byte)chars[i];

      ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
      InputStream is;
      if (encoding.equalsIgnoreCase("B"))
        is = new Base64InputStream(bis);
      else
      if (encoding.equalsIgnoreCase("Q"))
        is = new QInputStream(bis);
      else
        throw new UnsupportedEncodingException("Unknown encoding: "+encoding);
      len = bis.available();
      bytes = new byte[len];
      len = is.read(bytes, 0, len);
      String ret = new String(bytes, 0, len, charset);
      if (text.length() > end + 2)
      {
        String extra = text.substring(end + 2);
        if (!decodetextStrict())
          extra = decodeInnerText(extra);
        ret = ret + extra;
      }
      return ret;
    }
    catch (IOException e)
    {
      throw new ParseException();
    }
    catch (IllegalArgumentException e)
    {
      throw new UnsupportedEncodingException();
    }
  }

  /**
   * Indicates that we should consider a lone CR or LF in a body part
   * that's not a MIME text type to indicate that the body part
   * needs to be encoded.
   * @since JavaMail 1.3
   */
  private static boolean encodeeolStrict()
  {
    try
    {
      String encodeeolStrict =
        System.getProperty("mail.mime.encodeeol.strict", "false");
      return Boolean.valueOf(encodeeolStrict).booleanValue();
    }
    catch (SecurityException e)
    {
      return false;
    }
  }

  /**
   * Indicates if text in the middle of words should be decoded.
   * @since JavaMail 1.3
   */
  private static boolean decodetextStrict()
  {
    try
    {
      String decodetextStrict =
        System.getProperty("mail.mime.decodetext.strict", "true");
      return Boolean.valueOf(decodetextStrict).booleanValue();
    }
    catch (SecurityException e)
    {
      return true;
    }
  }

  /**
   * Decodes text in the middle of the specified text.
   * @since JavaMail 1.3
   */
  private static String decodeInnerText(String text)
    throws UnsupportedEncodingException
  {
    final String LD = "=?", RD = "?=";
    int pos = 0;
    StringBuffer buffer = new StringBuffer();
    for (int start = text.indexOf(LD, pos); start != -1;
        start = text.indexOf(LD, pos))
    {
      int end = text.indexOf(RD, start + 2);
      if (end == -1)
        break;
      buffer.append(text.substring(pos, start));
      pos = end + 2;
      String encoded = text.substring(start, pos);
      try
      {
        buffer.append(decodeWord(encoded));
      }
      catch (ParseException e)
      {
        buffer.append(encoded);
      }
    }
    if (buffer.length() > 0)
    {
      if (pos < text.length())
        buffer.append(text.substring(pos));
      return buffer.toString();
    }
    else
      return text;
  }

  /**
   * A utility method to quote a word, if the word contains any characters 
   * from the specified 'specials' list.
   * <p>
   * The HeaderTokenizer class defines two special sets of delimiters - 
   * MIME and RFC 822.
   * <p>
   * This method is typically used during the generation of RFC 822 and MIME
   * header fields.
   * @param word word to be quoted
   * @param specials the set of special characters
   * @return the possibly quoted word
   */
  public static String quote(String text, String specials)
  {
    int len = text.length();
    boolean needsQuotes = false;
    for (int i = 0; i<len; i++)
    {
      char c = text.charAt(i);
      if (c=='\n' || c=='\r' || c=='"' || c=='\\')
      {
        StringBuffer buffer = new StringBuffer(len+3);
        buffer.append('"');
        for (int j = 0; j<len; j++)
        {
          char c2 = text.charAt(j);
          if (c2=='"' || c2=='\\' || c2=='\r' || c2=='\n')
            buffer.append('\\');
          buffer.append(c2);
        }

        buffer.append('"');
        return buffer.toString();
      }
      if (c<' ' || c>'\177' || specials.indexOf(c)>0)
        needsQuotes = true;
    }

    if (needsQuotes)
    {
      StringBuffer buffer = new StringBuffer(len+2);
      buffer.append('"');
      buffer.append(text);
      buffer.append('"');
      return buffer.toString();
    }
    else
      return text;
  }

  // -- Java and MIME charset conversions --

  /*
   * Map of MIME charset names to Java charset names.
   */
  private static HashMap mimeCharsets;

  /*
   * Map of Java charset names to MIME charset names.
   */
  private static HashMap javaCharsets;

  /*
   * Load the charset conversion tables.
   */
  static 
  {
    String mappings = "/META-INF/javamail.charset.map";
    InputStream in = (MimeUtility.class).getResourceAsStream(mappings);
    if (in!=null)
    {
      mimeCharsets = new HashMap(10);
      javaCharsets = new HashMap(20);
      LineInputStream lin = new LineInputStream(in);
      parse(mimeCharsets, lin);
      parse(javaCharsets, lin);
    }
  }

  /*
   * Parse a charset map stream.
   */
  private static void parse(HashMap mappings, LineInputStream lin)
  {
    try
    {
      while (true)
      {
        String line = lin.readLine();
        if (line==null || (line.startsWith("--") && line.endsWith("--")))
          return;
        
        if (line.trim().length()!=0 && !line.startsWith("#"))
        {
          StringTokenizer st = new StringTokenizer(line, "\t ");
          try
          {
            String key = st.nextToken();
            String value = st.nextToken();
            mappings.put(key.toLowerCase(), value);
          }
          catch (NoSuchElementException e2)
          {
          }
        }
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Convert a MIME charset name into a valid Java charset name.
   * @param charset the MIME charset name
   * @return the Java charset equivalent.
   * If a suitable mapping is not available, the passed in charset is 
   * itself returned.
   */
  public static String javaCharset(String charset)
  {
    if (mimeCharsets==null || charset==null)
      return charset;
    String jc = (String)mimeCharsets.get(charset.toLowerCase());
    return (jc!=null) ? jc : charset;
  }

  /**
   * Convert a java charset into its MIME charset name.
   * <p>
   * Note that a future version of JDK (post 1.2) might provide this
   * functionality, in which case, we may deprecate this method then.
   * @param charset the JDK charset
   * @return the MIME/IANA equivalent.
   * If a mapping is not possible, the passed in charset itself is returned.
   */
  public static String mimeCharset(String charset)
  {
    if (javaCharsets==null || charset==null)
      return charset;
    String mc = (String)javaCharsets.get(charset.toLowerCase());
    return (mc!=null) ? mc : charset;
  }

  // -- Java default charset --
  
  /*
   * Local cache for the system default Java charset.
   * @see #getDefaultJavaCharset
   */
  private static String defaultJavaCharset;

  /**
   * Get the default charset corresponding to the system's current default
   * locale.
   * @return the default charset of the system's default locale,
   * as a Java charset. (NOT a MIME charset)
   */
  public static String getDefaultJavaCharset()
  {
    if (defaultJavaCharset==null)
    {
      try
      {
        // Use mail.mime.charset as of JavaMail 1.3
        defaultJavaCharset = System.getProperty("mail.mime.charset");
        if (defaultJavaCharset == null)
          defaultJavaCharset = System.getProperty("file.encoding", "8859_1");
      }
      catch (SecurityException e)
      {
        // InputStreamReader has access to the platform default encoding.
        // We create a dummy input stream to feed it with, just to get
        // this encoding value.
        InputStreamReader isr = 
          new InputStreamReader(new InputStream()
              {
                public int read()
                {
                  return 0;
                }
              }
          );
        defaultJavaCharset = isr.getEncoding();
        
        // If all else fails use 8859_1
        if (defaultJavaCharset==null)
          defaultJavaCharset = "8859_1";
      }
    }
    return defaultJavaCharset;
  }

  // -- Calculating multipart boundaries --
  
  private static int part = 0;

  /*
   * Returns a suitably unique boundary value.
   */
  static String getUniqueBoundaryValue()
  {
    StringBuffer buffer = new StringBuffer();
    buffer.append("----=_Part_");
    buffer.append(part++);
    buffer.append("_");
    buffer.append(buffer.hashCode());
    buffer.append('.');
    buffer.append(System.currentTimeMillis());
    return buffer.toString();
  }

  /*
   * Returns a suitably unique Message-ID value.
   */
  static String getUniqueMessageIDValue(Session session)
  {
    InternetAddress localAddress = InternetAddress.getLocalAddress(session);
    String address = (localAddress!=null) ? localAddress.getAddress() :
      "javamailuser@localhost";

    StringBuffer buffer = new StringBuffer();
    buffer.append(buffer.hashCode());
    buffer.append('.');
    buffer.append(System.currentTimeMillis());
    buffer.append('.');
    buffer.append("JavaMail.");
    buffer.append(address);
    return buffer.toString();
  }

  // These methods provide checks on whether collections of bytes contain
  // all-ASCII, majority-ASCII, or minority-ASCII bytes.
  
  // Constants
  static final int ALL = -1;
  static final int ALL_ASCII = 1;
  static final int MAJORITY_ASCII = 2;
  static final int MINORITY_ASCII = 3;

  static int asciiStatus(byte[] bytes)
  {
    int asciiCount = 0;
    int nonAsciiCount = 0;
    for (int i = 0; i<bytes.length; i++)
		{
      if (isAscii((int)bytes[i]))
        asciiCount++;
      else
        nonAsciiCount++;
		}

    if (nonAsciiCount==0)
      return ALL_ASCII;
    return (asciiCount<=nonAsciiCount) ? MINORITY_ASCII : MAJORITY_ASCII;
  }

  static int asciiStatus(InputStream is, int len, boolean text)
  {
    int asciiCount = 0;
    int nonAsciiCount = 0;
    int blockLen = 4096;
    int lineLen = 0;
    boolean islong = false;
    byte[] bytes = null;
    if (len!=0)
    {
      blockLen = (len!=ALL) ? Math.min(len, 4096) : 4096;
      bytes = new byte[blockLen];
    }
    while (len!=0) 
    {
      int readLen;
      try
      {
        readLen = is.read(bytes, 0, blockLen);
        if (readLen<0)
          break;
        for (int i = 0; i<readLen; i++)
        {
          int c = bytes[i] & 0xff;
          if (c==13 || c==10)
            lineLen = 0;
          else
          {
            lineLen++;
            if (lineLen>998)
              islong = true;
          }
          if (isAscii(c))
            asciiCount++;
          else
          {
            if (text)
              return MINORITY_ASCII;
            nonAsciiCount++;
          }
        }

      }
      catch (IOException e)
      {
        break;
      }
      if (len!=-1)
        len -= readLen;
    }
    if (len==0 && text)
      return MINORITY_ASCII;
    if (nonAsciiCount==0)
      return !islong ? ALL_ASCII : MAJORITY_ASCII;
    return asciiCount<=nonAsciiCount ? MINORITY_ASCII : MAJORITY_ASCII;
  }

  private static final boolean isAscii(int c)
  {
		if (c<0)
			c += 0xff;
    return (c<128 && c>31) || c==13 || c==10 || c==9;
  }

  /*
   * This is used by the getEncoding(DataHandler) method to ascertain which
   * encoding scheme to use. It embodies the same algorithm as the
   * asciiStatus methods above.
   */
  static class AsciiOutputStream extends OutputStream
  {

    static final int LF = 0x0a;
    static final int CR = 0x0d;
    
    private boolean strict;
    private boolean eolStrict;
    private int asciiCount = 0;
    private int nonAsciiCount = 0;
    private int ret;
    private int len;
    private int last = -1;
    private boolean islong = false;
    private boolean eolCheckFailed = false;
    
    public AsciiOutputStream(boolean strict, boolean eolStrict)
    {
      this.strict = strict;
      this.eolStrict = eolStrict;
    }
    
    public void write(int c)
      throws IOException
    {
      check(c);
    }
    
    public void write(byte[] bytes)
      throws IOException
    {
      write(bytes, 0, bytes.length);
    }
    
    public void write(byte[] bytes, int offset, int length)
      throws IOException
    {
      length += offset;
      for (int i = offset; i<length; i++)
        check(bytes[i]);
      
    }
    
    private final void check(int c)
      throws IOException
    {
      c &= 0xff;
      if (eolStrict)
      {
        if (last == CR && c != LF || last != CR && c == LF)
          eolCheckFailed = true;
      }
      if (c == CR || c == LF)
        len = 0;
      else
      {
        len++;
        if (len > 998)
          islong = true;
      }
      if (c > 127)
      {
        nonAsciiCount++;
        if (strict)
        {
          ret = MINORITY_ASCII;
          throw new EOFException();
        }
      }
      else
        asciiCount++;
      last = c;
    }
    
    int status()
    {
      if (ret != 0)
        return ret;
      if (eolCheckFailed)
        return MINORITY_ASCII;
      if (nonAsciiCount == 0)
        return !islong ? ALL_ASCII : MAJORITY_ASCII;
      return (asciiCount <= nonAsciiCount) ? MAJORITY_ASCII : MINORITY_ASCII;
    }
    
  }

}