1 16 17 package org.apache.commons.codec.net; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.UnsupportedEncodingException ; 21 import java.util.BitSet ; 22 import org.apache.commons.codec.BinaryDecoder; 23 import org.apache.commons.codec.BinaryEncoder; 24 import org.apache.commons.codec.DecoderException; 25 import org.apache.commons.codec.EncoderException; 26 import org.apache.commons.codec.StringDecoder; 27 import org.apache.commons.codec.StringEncoder; 28 29 59 public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder { 60 63 private String charset = StringEncodings.UTF8; 64 65 68 private static final BitSet PRINTABLE_CHARS = new BitSet (256); 69 70 private static byte ESCAPE_CHAR = '='; 71 72 private static byte TAB = 9; 73 74 private static byte SPACE = 32; 75 static { 77 for (int i = 33; i <= 60; i++) { 79 PRINTABLE_CHARS.set(i); 80 } 81 for (int i = 62; i <= 126; i++) { 82 PRINTABLE_CHARS.set(i); 83 } 84 PRINTABLE_CHARS.set(TAB); 85 PRINTABLE_CHARS.set(SPACE); 86 } 87 88 91 public QuotedPrintableCodec() { 92 super(); 93 } 94 95 101 public QuotedPrintableCodec(String charset) { 102 super(); 103 this.charset = charset; 104 } 105 106 114 private static final void encodeQuotedPrintable(int b, ByteArrayOutputStream buffer) { 115 buffer.write(ESCAPE_CHAR); 116 char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16)); 117 char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16)); 118 buffer.write(hex1); 119 buffer.write(hex2); 120 } 121 122 136 public static final byte[] encodeQuotedPrintable(BitSet printable, byte[] bytes) { 137 if (bytes == null) { 138 return null; 139 } 140 if (printable == null) { 141 printable = PRINTABLE_CHARS; 142 } 143 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 144 for (int i = 0; i < bytes.length; i++) { 145 int b = bytes[i]; 146 if (b < 0) { 147 b = 256 + b; 148 } 149 if (printable.get(b)) { 150 buffer.write(b); 151 } else { 152 encodeQuotedPrintable(b, buffer); 153 } 154 } 155 return buffer.toByteArray(); 156 } 157 158 173 public static final byte[] decodeQuotedPrintable(byte[] bytes) throws DecoderException { 174 if (bytes == null) { 175 return null; 176 } 177 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 178 for (int i = 0; i < bytes.length; i++) { 179 int b = bytes[i]; 180 if (b == ESCAPE_CHAR) { 181 try { 182 int u = Character.digit((char) bytes[++i], 16); 183 int l = Character.digit((char) bytes[++i], 16); 184 if (u == -1 || l == -1) { 185 throw new DecoderException("Invalid quoted-printable encoding"); 186 } 187 buffer.write((char) ((u << 4) + l)); 188 } catch (ArrayIndexOutOfBoundsException e) { 189 throw new DecoderException("Invalid quoted-printable encoding"); 190 } 191 } else { 192 buffer.write(b); 193 } 194 } 195 return buffer.toByteArray(); 196 } 197 198 210 public byte[] encode(byte[] bytes) { 211 return encodeQuotedPrintable(PRINTABLE_CHARS, bytes); 212 } 213 214 229 public byte[] decode(byte[] bytes) throws DecoderException { 230 return decodeQuotedPrintable(bytes); 231 } 232 233 250 public String encode(String pString) throws EncoderException { 251 if (pString == null) { 252 return null; 253 } 254 try { 255 return encode(pString, getDefaultCharset()); 256 } catch (UnsupportedEncodingException e) { 257 throw new EncoderException(e.getMessage()); 258 } 259 } 260 261 275 public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException { 276 if (pString == null) { 277 return null; 278 } 279 return new String (decode(pString.getBytes(StringEncodings.US_ASCII)), charset); 280 } 281 282 295 public String decode(String pString) throws DecoderException { 296 if (pString == null) { 297 return null; 298 } 299 try { 300 return decode(pString, getDefaultCharset()); 301 } catch (UnsupportedEncodingException e) { 302 throw new DecoderException(e.getMessage()); 303 } 304 } 305 306 316 public Object encode(Object pObject) throws EncoderException { 317 if (pObject == null) { 318 return null; 319 } else if (pObject instanceof byte[]) { 320 return encode((byte[]) pObject); 321 } else if (pObject instanceof String ) { 322 return encode((String ) pObject); 323 } else { 324 throw new EncoderException("Objects of type " 325 + pObject.getClass().getName() 326 + " cannot be quoted-printable encoded"); 327 } 328 } 329 330 341 public Object decode(Object pObject) throws DecoderException { 342 if (pObject == null) { 343 return null; 344 } else if (pObject instanceof byte[]) { 345 return decode((byte[]) pObject); 346 } else if (pObject instanceof String ) { 347 return decode((String ) pObject); 348 } else { 349 throw new DecoderException("Objects of type " 350 + pObject.getClass().getName() 351 + " cannot be quoted-printable decoded"); 352 } 353 } 354 355 360 public String getDefaultCharset() { 361 return this.charset; 362 } 363 364 381 public String encode(String pString, String charset) throws UnsupportedEncodingException { 382 if (pString == null) { 383 return null; 384 } 385 return new String (encode(pString.getBytes(charset)), StringEncodings.US_ASCII); 386 } 387 } | Popular Tags |