KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * QuotedPrintableDecoder.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: QuotedPrintableDecoder.java,v 1.1.1.1 2002/09/24 16:10:03 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 org.armedbear.j.ByteBuffer;
25
26 public final class QuotedPrintableDecoder
27 {
28     public static byte[] decode(String JavaDoc encoded)
29     {
30         ByteBuffer bb = new ByteBuffer();
31         int limit = encoded.length();
32         int i = 0;
33         while (i < limit) {
34             char c = encoded.charAt(i);
35             if (c == '=') {
36                 if (i+1 < limit && encoded.charAt(i+1) == '\n') {
37                     // Soft line break.
38
i += 2;
39                     continue;
40                 }
41                 if (i+2 < limit) {
42                     char c1 = encoded.charAt(i+1);
43                     char c2 = encoded.charAt(i+2);
44                     if (c1 == '\r' && c2 == '\n') {
45                         // Soft line break.
46
i += 3;
47                         continue;
48                     }
49                     int hi = decodeHex(c1);
50                     if (hi >= 0) {
51                         int lo = decodeHex(c2);
52                         if (lo >= 0) {
53                             byte b = (byte) ((hi << 4) + lo);
54                             bb.append(b);
55                             i += 3;
56                             continue;
57                         }
58                     }
59                 }
60             }
61             // Not encoded.
62
bb.append((byte)c);
63             ++i;
64         }
65         byte[] toBeReturned = new byte[bb.length()];
66         System.arraycopy(bb.getBytes(), 0, toBeReturned, 0, bb.length());
67         return toBeReturned;
68     }
69
70     private static int decodeHex(char c)
71     {
72         if (c >= '0' && c <= '9')
73             return c - '0';
74         else if (c >= 'A' && c <= 'F')
75             return c - 'A' + 10;
76         else if (c >= 'a' && c <= 'f')
77             return c - 'a' + 10;
78         // Error!
79
return -1;
80     }
81 }
82
Popular Tags