/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * quoted-printable object to convert into its original form * @return original object * @throws DecoderException * Thrown if the argument is not a <code>String</code>. Thrown if a failure condition is encountered * during the decode process. */ @Override public Object decode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param pObject * quoted-printable object to convert into its original form * * @return original object * * @throws DecoderException * A decoder exception is thrown if a failure condition is encountered during the decode process. */ public Object decode(Object pObject) throws DecoderException { if (pObject == null) { return null; } else if (pObject instanceof String) { return decode((String) pObject); } else { throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be decoded using Q codec"); } }
@Test public void testDecodeObjects() throws Exception { final QCodec qcodec = new QCodec(); final String decoded = "=?UTF-8?Q?1+1 =3D 2?="; final String plain = (String) qcodec.decode((Object) decoded); assertEquals("Basic Q decoding test", "1+1 = 2", plain); final Object result = qcodec.decode((Object) null); assertEquals( "Decoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); qcodec.decode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final DecoderException ee) { // Exception expected, test segment passes. } }
@Test public void testDecodeStringWithNull() throws Exception { final QCodec qcodec = new QCodec(); final String test = null; final String result = qcodec.decode( test ); assertEquals("Result should be null", null, result); }
@Test public void testUnsafeEncodeDecode() throws Exception { final QCodec qcodec = new QCodec(); final String plain = "?_=\r\n"; final String encoded = qcodec.encode(plain); assertEquals("Unsafe chars Q encoding test", "=?UTF-8?Q?=3F=5F=3D=0D=0A?=", encoded); assertEquals("Unsafe chars Q decoding test", plain, qcodec.decode(encoded)); }
@Test public void testEncodeDecodeNull() throws Exception { final QCodec qcodec = new QCodec(); assertNull("Null string Q encoding test", qcodec.encode((String)null)); assertNull("Null string Q decoding test", qcodec.decode((String)null)); }
@Test public void testBasicEncodeDecode() throws Exception { final QCodec qcodec = new QCodec(); final String plain = "= Hello there =\r\n"; final String encoded = qcodec.encode(plain); assertEquals("Basic Q encoding test", "=?UTF-8?Q?=3D Hello there =3D=0D=0A?=", encoded); assertEquals("Basic Q decoding test", plain, qcodec.decode(encoded)); }
@Test public void testEncodeDecodeBlanks() throws Exception { final String plain = "Mind those pesky blanks"; final String encoded1 = "=?UTF-8?Q?Mind those pesky blanks?="; final String encoded2 = "=?UTF-8?Q?Mind_those_pesky_blanks?="; final QCodec qcodec = new QCodec(); qcodec.setEncodeBlanks(false); String s = qcodec.encode(plain); assertEquals("Blanks encoding with the Q codec test", encoded1, s); qcodec.setEncodeBlanks(true); s = qcodec.encode(plain); assertEquals("Blanks encoding with the Q codec test", encoded2, s); s = qcodec.decode(encoded1); assertEquals("Blanks decoding with the Q codec test", plain, s); s = qcodec.decode(encoded2); assertEquals("Blanks decoding with the Q codec test", plain, s); }
@Test public void testUTF8RoundTrip() throws Exception { final String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); final QCodec qcodec = new QCodec(CharEncoding.UTF_8); assertEquals( "=?UTF-8?Q?=D0=92=D1=81=D0=B5=D0=BC=5F=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82?=", qcodec.encode(ru_msg) ); assertEquals("=?UTF-8?Q?Gr=C3=BCezi=5Fz=C3=A4m=C3=A4?=", qcodec.encode(ch_msg)); assertEquals(ru_msg, qcodec.decode(qcodec.encode(ru_msg))); assertEquals(ch_msg, qcodec.decode(qcodec.encode(ch_msg))); }
//String s must start with "=?" and end with "?=" QCodec q = new QCodec(); q.decode(s);
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * quoted-printable object to convert into its original form * @return original object * @throws DecoderException * Thrown if the argument is not a <code>String</code>. Thrown if a failure condition is encountered * during the decode process. */ @Override public Object decode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param pObject * quoted-printable object to convert into its original form * * @return original object * * @throws DecoderException * A decoder exception is thrown if a failure condition is encountered during the decode process. */ public Object decode(Object pObject) throws DecoderException { if (pObject == null) { return null; } else if (pObject instanceof String) { return decode((String) pObject); } else { throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * quoted-printable object to convert into its original form * @return original object * @throws DecoderException * Thrown if the argument is not a <code>String</code>. Thrown if a failure condition is encountered * during the decode process. */ @Override public Object decode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * quoted-printable object to convert into its original form * @return original object * @throws DecoderException * Thrown if the argument is not a <code>String</code>. Thrown if a failure condition is encountered * during the decode process. */ @Override public Object decode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param pObject * quoted-printable object to convert into its original form * * @return original object * * @throws DecoderException * A decoder exception is thrown if a failure condition is encountered during the decode process. */ public Object decode(Object pObject) throws DecoderException { if (pObject == null) { return null; } else if (pObject instanceof String) { return decode((String) pObject); } else { throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param pObject * quoted-printable object to convert into its original form * * @return original object * * @throws DecoderException * A decoder exception is thrown if a failure condition is encountered during the decode process. */ public Object decode(Object pObject) throws DecoderException { if (pObject == null) { return null; } else if (pObject instanceof String) { return decode((String) pObject); } else { throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param pObject * quoted-printable object to convert into its original form * * @return original object * * @throws DecoderException * A decoder exception is thrown if a failure condition is encountered during the decode process. */ public Object decode(Object pObject) throws DecoderException { if (pObject == null) { return null; } else if (pObject instanceof String) { return decode((String) pObject); } else { throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param pObject * quoted-printable object to convert into its original form * * @return original object * * @throws DecoderException * A decoder exception is thrown if a failure condition is encountered during the decode process. */ public Object decode(Object pObject) throws DecoderException { if (pObject == null) { return null; } else if (pObject instanceof String) { return decode((String) pObject); } else { throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * Used to decode a string as specified by RFC 2047 * * @param value The encoded string */ public static String decode(String value) { if (value == null) return null; try { // try BCodec first return (new BCodec()).decode(value); } catch (DecoderException de) { // try QCodec next try { return (new QCodec()).decode(value); } catch (Exception ex) { return value; } } catch (Exception e) { return value; } }
/** * Used to decode a string as specified by RFC 2047 * @param value The encoded string */ public static String decode(String value) { if (value == null) return null; try { // try BCodec first return (new BCodec()).decode(value); } catch (DecoderException de) { // try QCodec next try { return (new QCodec()).decode(value); } catch (Exception ex) { return value; } } catch (Exception e) { return value; } }