1 21 22 27 28 package com.sun.mail.imap.protocol; 29 30 import java.text.StringCharacterIterator ; 31 import java.text.CharacterIterator ; 32 33 41 42 public class BASE64MailboxDecoder { 43 44 public static String decode(String original) { 45 if (original == null || original.length() == 0) 46 return original; 47 48 boolean changedString = false; 49 int copyTo = 0; 50 char[] chars = new char[original.length()]; 52 StringCharacterIterator iter = new StringCharacterIterator (original); 53 54 for(char c = iter.first(); c != CharacterIterator.DONE; 55 c = iter.next()) { 56 57 if (c == '&') { 58 changedString = true; 59 copyTo = base64decode(chars, copyTo, iter); 60 } else { 61 chars[copyTo++] = c; 62 } 63 } 64 65 if (changedString) { 67 return new String (chars, 0, copyTo); 68 } else { 69 return original; 70 } 71 } 72 73 74 protected static int base64decode(char[] buffer, int offset, 75 CharacterIterator iter) { 76 boolean firsttime = true; 77 int leftover = -1; 78 char testing = 0; 79 80 while(true) { 81 byte orig_0 = (byte) iter.next(); 83 if (orig_0 == -1) break; if (orig_0 == '-') { 85 if (firsttime) { 86 buffer[offset++] = '&'; 88 } 89 break; 91 } 92 firsttime = false; 93 94 byte orig_1 = (byte) iter.next(); 96 if (orig_1 == -1 || orig_1 == '-') 97 break; 99 byte a, b, current; 100 a = pem_convert_array[orig_0 & 0xff]; 101 b = pem_convert_array[orig_1 & 0xff]; 102 current = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)); 104 105 if (leftover != -1) { 107 buffer[offset++] = (char)(leftover << 8 | (current & 0xff)); 108 leftover = -1; 109 } else { 110 leftover = current & 0xff; 111 } 112 113 byte orig_2 = (byte) iter.next(); 114 if (orig_2 == '=') { continue; 116 } else if (orig_2 == -1 || orig_2 == '-') { 117 break; } 119 120 a = b; 122 b = pem_convert_array[orig_2 & 0xff]; 123 current = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf)); 124 125 if (leftover != -1) { 127 buffer[offset++] = (char)(leftover << 8 | (current & 0xff)); 128 leftover = -1; 129 } else { 130 leftover = current & 0xff; 131 } 132 133 byte orig_3 = (byte) iter.next(); 134 if (orig_3 == '=') { continue; 136 } else if (orig_3 == -1 || orig_3 == '-') { 137 break; } 139 140 a = b; 142 b = pem_convert_array[orig_3 & 0xff]; 143 current = (byte)(((a << 6) & 0xc0) | (b & 0x3f)); 144 145 if (leftover != -1) { 147 testing = (char)((int)leftover << 8 | ((int) current & 0xff)); 148 buffer[offset++] = (char)(leftover << 8 | (current & 0xff)); 149 leftover = -1; 150 } else { 151 leftover = current & 0xff; 152 } 153 } 154 155 return offset; 156 } 157 158 163 164 protected final static char pem_array[] = { 165 'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X', 'Y','Z','a','b','c','d','e','f', 'g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v', 'w','x','y','z','0','1','2','3', '4','5','6','7','8','9','+',',' }; 174 175 protected final static byte pem_convert_array[] = new byte[256]; 176 177 static { 178 for (int i = 0; i < 255; i++) 179 pem_convert_array[i] = -1; 180 for(int i = 0; i < pem_array.length; i++) 181 pem_convert_array[pem_array[i]] = (byte) i; 182 } 183 } 184 | Popular Tags |