KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > QuotedPrintableEncoder


1 /*
2  * QuotedPrintableEncoder.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: QuotedPrintableEncoder.java,v 1.1.1.1 2002/09/24 16:09:55 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import org.armedbear.j.FastStringBuffer;
26 import org.armedbear.j.Log;
27
28 public final class QuotedPrintableEncoder
29 {
30     public static String JavaDoc encode(String JavaDoc input, String JavaDoc characterEncoding,
31         String JavaDoc separator)
32     {
33         if (input.length() == 0)
34             return input;
35         byte[] bytes;
36         try {
37             bytes = input.getBytes(characterEncoding);
38         }
39         catch (UnsupportedEncodingException JavaDoc e) {
40             Log.error(e);
41             bytes = input.getBytes();
42         }
43         FastStringBuffer sb = new FastStringBuffer();
44         int outputLength = 0;
45         for (int i = 0; i < bytes.length; i++) {
46             byte b = bytes[i];
47             if ((b < ' ' && b != '\t') || b == 127 || b == '=') {
48                 sb.append(encode(b));
49                 outputLength += 3;
50             } else if (outputLength == 0 && b == 'F') {
51                 if (i+4 < bytes.length &&
52                     bytes[i+1] == 'r' &&
53                     bytes[i+2] == 'o' &&
54                     bytes[i+3] == 'm' &&
55                     bytes[i+4] == ' ') {
56                     // Need to encode the 'F'.
57
sb.append(encode(b));
58                     outputLength += 3;
59                 } else {
60                     // 'F' but not "From ". Don't encode it.
61
sb.append((char) (b < 0 ? b+256 : b));
62                     ++outputLength;
63                 }
64             } else if (outputLength == 0 && b == '.') {
65                 sb.append(encode(b));
66                 outputLength += 3;
67             } else {
68                 sb.append((char) (b < 0 ? b+256 : b));
69                 ++outputLength;
70             }
71             if (outputLength >= 73) {
72                 // Soft line break.
73
sb.append('=');
74                 sb.append(separator);
75                 outputLength = 0;
76             }
77         }
78         // Check for whitespace at end of line.
79
if (sb.length() > 0) {
80             char c = sb.charAt(sb.length()-1);
81             if (c == ' ' || c == '\t') {
82                 sb.append('=');
83                 sb.append(separator);
84             }
85         }
86         return sb.toString();
87     }
88
89     private static String JavaDoc encode(byte b)
90     {
91         int n = b;
92         if (n < 0)
93             n += 256;
94         FastStringBuffer sb = new FastStringBuffer('=');
95         sb.append(Integer.toString(n, 16));
96         return sb.toString().toUpperCase();
97     }
98 }
99
Popular Tags