/** * 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> or <code>byte[]</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 byte[]) { return decode((byte[]) obj); } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be quoted-printable decoded"); } }
/** * Decodes a quoted-printable string into its original form using the default string charset. Escaped characters are * converted back to their original representation. * * @param str * quoted-printable string to convert into its original form * @return original string * @throws DecoderException * Thrown if quoted-printable decoding is unsuccessful. Thrown if charset is not supported. * @see #getCharset() */ @Override public String decode(final String str) throws DecoderException { return this.decode(str, this.getCharset()); }
/** * Decodes a quoted-printable string into its original form using the specified string charset. Escaped characters * are converted back to their original representation. * * @param pString * quoted-printable string to convert into its original form * @param charset * the original string charset * @return original string * @throws DecoderException * Thrown if quoted-printable decoding is unsuccessful * @throws UnsupportedEncodingException * Thrown if charset is not supported */ public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException { if (pString == null) { return null; } return new String(decode(pString.getBytes(StringEncodings.US_ASCII)), charset); }
/** * Decodes a quoted-printable string into its original form using the specified string charset. Escaped characters * are converted back to their original representation. * * @param str * quoted-printable string to convert into its original form * @param charset * the original string charset * @return original string * @throws DecoderException * Thrown if quoted-printable decoding is unsuccessful * @since 1.7 */ public String decode(final String str, final Charset charset) throws DecoderException { if (str == null) { return null; } return new String(this.decode(StringUtils.getBytesUsAscii(str)), charset); }
/** * Decodes a quoted-printable string into its original form using the specified string charset. Escaped characters * are converted back to their original representation. * * @param str * quoted-printable string to convert into its original form * @param charset * the original string charset * @return original string * @throws DecoderException * Thrown if quoted-printable decoding is unsuccessful * @throws UnsupportedEncodingException * Thrown if charset is not supported */ public String decode(final String str, final String charset) throws DecoderException, UnsupportedEncodingException { if (str == null) { return null; } return new String(decode(StringUtils.getBytesUsAscii(str)), charset); }
/** * 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 * Thrown if quoted-printable decoding is not applicable to objects of this type if decoding is * unsuccessful */ public Object decode(Object pObject) throws DecoderException { if (pObject == null) { return null; } else if (pObject instanceof byte[]) { return decode((byte[]) pObject); } else if (pObject instanceof String) { return decode((String) pObject); } else { throw new DecoderException("Objects of type " + pObject.getClass().getName() + " cannot be quoted-printable decoded"); } }
/** * Decodes a quoted-printable string into its original form using the default string charset. Escaped characters are * converted back to their original representation. * * @param pString * quoted-printable string to convert into its original form * @return original string * @throws DecoderException * Thrown if quoted-printable decoding is unsuccessful * @throws UnsupportedEncodingException * Thrown if charset is not supported * @see #getDefaultCharset() */ public String decode(String pString) throws DecoderException { if (pString == null) { return null; } try { return decode(pString, getDefaultCharset()); } catch (UnsupportedEncodingException e) { throw new DecoderException(e.getMessage()); } }
@Test public void testDecodeObjects() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String plain = "1+1 =3D 2"; String decoded = (String) qpcodec.decode((Object) plain); assertEquals("Basic quoted-printable decoding test", "1+1 = 2", decoded); final byte[] plainBA = plain.getBytes(Charsets.UTF_8); final byte[] decodedBA = (byte[]) qpcodec.decode((Object) plainBA); decoded = new String(decodedBA); assertEquals("Basic quoted-printable decoding test", "1+1 = 2", decoded); final Object result = qpcodec.decode((Object) null); assertEquals( "Decoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); qpcodec.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 testDecodeInvalid() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); try { qpcodec.decode("="); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { // Expected. Move on } try { qpcodec.decode("=A"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { // Expected. Move on } try { qpcodec.decode("=WW"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { // Expected. Move on } }
@Test public void testSoftLineBreakDecode() throws Exception { final String qpdata = "If you believe that truth=3Dbeauty, then surely=20=\r\nmathematics is the most beautiful branch of philosophy."; final String expected = "If you believe that truth=beauty, then surely mathematics is the most beautiful branch of philosophy."; final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); assertEquals(expected, qpcodec.decode(qpdata)); final String encoded = qpcodec.encode(expected); assertEquals(expected, qpcodec.decode(encoded)); }
@Test public void testSkipNotEncodedCRLF() throws Exception { final String qpdata = "CRLF in an\n encoded text should be=20=\r\n\rskipped in the\r decoding."; final String expected = "CRLF in an encoded text should be skipped in the decoding."; final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true); assertEquals(expected, qpcodec.decode(qpdata)); final String encoded = qpcodec.encode(expected); assertEquals(expected, qpcodec.decode(encoded)); }
@Test public void testDecodeStringWithNull() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String test = null; final String result = qpcodec.decode( test, "charset" ); assertEquals("Result should be null", null, result); }
@Test public void testEncodeUrlWithNullBitSet() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String plain = "1+1 = 2"; final String encoded = new String(QuotedPrintableCodec. encodeQuotedPrintable(null, plain.getBytes(Charsets.UTF_8))); assertEquals("Basic quoted-printable encoding test", "1+1 =3D 2", encoded); assertEquals("Basic quoted-printable decoding test", plain, qpcodec.decode(encoded)); }
@Test public void testUnsafeEncodeDecode() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String plain = "=\r\n"; final String encoded = qpcodec.encode(plain); assertEquals("Unsafe chars quoted-printable encoding test", "=3D=0D=0A", encoded); assertEquals("Unsafe chars quoted-printable decoding test", plain, qpcodec.decode(encoded)); }
@Test public void testEncodeDecodeNull() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); assertNull("Null string quoted-printable encoding test", qpcodec.encode((String)null)); assertNull("Null string quoted-printable decoding test", qpcodec.decode((String)null)); }
@Test public void testSafeCharEncodeDecode() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String plain = "abc123_-.*~!@#$%^&()+{}\"\\;:`,/[]"; final String encoded = qpcodec.encode(plain); assertEquals("Safe chars quoted-printable encoding test", plain, encoded); assertEquals("Safe chars quoted-printable decoding test", plain, qpcodec.decode(encoded)); }
@Test public void testBasicEncodeDecode() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String plain = "= Hello there =\r\n"; final String encoded = qpcodec.encode(plain); assertEquals("Basic quoted-printable encoding test", "=3D Hello there =3D=0D=0A", encoded); assertEquals("Basic quoted-printable decoding test", plain, qpcodec.decode(encoded)); }
@Test public void testSoftLineBreakEncode() throws Exception { final String qpdata = "If you believe that truth=3Dbeauty, then surely mathematics is the most b=\r\neautiful branch of philosophy."; final String expected = "If you believe that truth=beauty, then surely mathematics is the most beautiful branch of philosophy."; final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true); assertEquals(qpdata, qpcodec.encode(expected)); final String decoded = qpcodec.decode(qpdata); assertEquals(qpdata, qpcodec.encode(decoded)); }
@Test public void testUTF8RoundTrip() throws Exception { final String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); assertEquals( "=D0=92=D1=81=D0=B5=D0=BC_=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82", qpcodec.encode(ru_msg, CharEncoding.UTF_8) ); assertEquals("Gr=C3=BCezi_z=C3=A4m=C3=A4", qpcodec.encode(ch_msg, CharEncoding.UTF_8)); assertEquals(ru_msg, qpcodec.decode(qpcodec.encode(ru_msg, CharEncoding.UTF_8), CharEncoding.UTF_8)); assertEquals(ch_msg, qpcodec.decode(qpcodec.encode(ch_msg, CharEncoding.UTF_8), CharEncoding.UTF_8)); }
private static String decodeRfc1521(final String decodeText) { final QuotedPrintableCodec codec = new QuotedPrintableCodec(); try { return codec.decode(decodeText); } catch (final DecoderException e) { return "Error: RFC 1521 MIME value cannot be decoded..."; } }