1 21 22 27 28 package com.sun.mail.imap.protocol; 29 30 import java.io.*; 31 32 33 96 97 public class BASE64MailboxEncoder { 98 protected byte[] buffer = new byte[4]; 99 protected int bufsize = 0; 100 protected boolean started = false; 101 protected Writer out = null; 102 103 104 public static String encode(String original) { 105 BASE64MailboxEncoder base64stream = null; 106 char origchars[] = original.toCharArray(); 107 int length = origchars.length; 108 boolean changedString = false; 109 CharArrayWriter writer = new CharArrayWriter(length); 110 111 for(int index = 0; index < length; index++) { 113 char current = origchars[index]; 114 115 if (current >= 0x20 && current <= 0x7e) { 118 if (base64stream != null) { 119 base64stream.flush(); 120 } 121 122 if (current == '&') { 123 changedString = true; 124 writer.write('&'); 125 writer.write('-'); 126 } else { 127 writer.write(current); 128 } 129 } else { 130 131 136 if (base64stream == null) { 137 base64stream = new BASE64MailboxEncoder(writer); 138 changedString = true; 139 } 140 141 base64stream.write(current); 142 } 143 } 144 145 146 if (base64stream != null) { 147 base64stream.flush(); 148 } 149 150 if (changedString) { 151 return writer.toString(); 152 } else { 153 return original; 154 } 155 } 156 157 158 161 public BASE64MailboxEncoder(Writer what) { 162 out = what; 163 } 164 165 public void write(int c) { 166 try { 167 if (!started) { 169 started = true; 170 out.write('&'); 171 } 172 173 buffer[bufsize++] = (byte) (c >> 8); 175 buffer[bufsize++] = (byte) (c & 0xff); 176 177 if (bufsize >= 3) { 178 encode(); 179 bufsize -= 3; 180 } 181 } catch (IOException e) { 182 } 184 } 185 186 187 public void flush() { 188 try { 189 if (bufsize > 0) { 191 encode(); 192 bufsize = 0; 193 } 194 195 if (started) { 197 out.write('-'); 198 started = false; 199 } 200 } catch (IOException e) { 201 } 203 } 204 205 206 protected void encode() throws IOException { 207 byte a, b, c; 208 if (bufsize == 1) { 209 a = buffer[0]; 210 b = 0; 211 c = 0; 212 out.write(pem_array[(a >>> 2) & 0x3F]); 213 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]); 214 } else if (bufsize == 2) { 216 a = buffer[0]; 217 b = buffer[1]; 218 c = 0; 219 out.write(pem_array[(a >>> 2) & 0x3F]); 220 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]); 221 out.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]); 222 } else { 224 a = buffer[0]; 225 b = buffer[1]; 226 c = buffer[2]; 227 out.write(pem_array[(a >>> 2) & 0x3F]); 228 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]); 229 out.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]); 230 out.write(pem_array[c & 0x3F]); 231 232 if (bufsize == 4) 234 buffer[0] = buffer[3]; 235 } 236 } 237 238 private final static char pem_array[] = { 239 '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','+',',' }; 248 } 249 | Popular Tags |