1 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 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 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 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 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 return -1; 80 } 81 } 82 | Popular Tags |