@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)); }
/** * Decodes a quoted-printable string into its original form. 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 * A decoder exception is thrown if a failure condition is encountered during the decode process. */ @Override public String decode(final String str) throws DecoderException { if (str == null) { return null; } try { return decodeText(str); } catch (final UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } }
/** * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped. * * @param str * string to convert to quoted-printable form * @param charset * the charset for str * @return quoted-printable string * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. * @since 1.7 */ public String encode(final String str, final Charset charset) throws EncoderException { if (str == null) { return null; } return encodeText(str, charset); }
/** * Encodes a string into its quoted-printable form using the default charset. Unsafe characters are escaped. * * @param str * string to convert to quoted-printable form * @return quoted-printable string * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ @Override public String encode(final String str) throws EncoderException { if (str == null) { return null; } return encode(str, getCharset()); }
/** * Encodes a string into its quoted-printable form using the default charset. Unsafe characters are escaped. * * @param pString * string to convert to quoted-printable form * @return quoted-printable string * * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ public String encode(String pString) throws EncoderException { if (pString == null) { return null; } return encode(pString, getDefaultCharset()); }
@Test public void testLetUsMakeCloverHappy() throws Exception { final QCodec qcodec = new QCodec(); qcodec.setEncodeBlanks(true); assertTrue(qcodec.isEncodeBlanks()); qcodec.setEncodeBlanks(false); assertFalse(qcodec.isEncodeBlanks()); }
/** * Encodes an object into its quoted-printable form using the default charset. Unsafe characters are escaped. * * @param obj * object to convert to quoted-printable form * @return quoted-printable object * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ @Override public Object encode(final Object obj) throws EncoderException { if (obj == null) { return null; } else if (obj instanceof String) { return encode((String) obj); } else { throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be encoded 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"); } }
@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 testNullInput() throws Exception { final QCodec qcodec = new QCodec(); assertNull(qcodec.doDecoding(null)); assertNull(qcodec.doEncoding(null)); }
@Test(expected=UnsupportedCharsetException.class) public void testInvalidEncoding() { new QCodec("NONSENSE"); }
/** * Encodes an object into its quoted-printable form using the default charset. Unsafe characters are escaped. * * @param pObject * object to convert to quoted-printable form * @return quoted-printable object * * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ public Object encode(Object pObject) throws EncoderException { if (pObject == null) { return null; } else if (pObject instanceof String) { return encode((String) pObject); } else { throw new EncoderException("Objects of type " + pObject.getClass().getName() + " cannot be encoded using Q codec"); } }
@Test public void testEncodeObjects() throws Exception { final QCodec qcodec = new QCodec(); final String plain = "1+1 = 2"; final String encoded = (String) qcodec.encode((Object) plain); assertEquals("Basic Q encoding test", "=?UTF-8?Q?1+1 =3D 2?=", encoded); final Object result = qcodec.encode((Object) null); assertEquals( "Encoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); qcodec.encode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final EncoderException ee) { // Exception expected, test segment passes. } }
/** * Encodes a string into its quoted-printable form using the default charset. Unsafe characters are escaped. * * @param str * string to convert to quoted-printable form * @return quoted-printable string * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ @Override public String encode(final String str) throws EncoderException { if (str == null) { return null; } return encode(str, getCharset()); }
/** * 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"); } }
/** * Encodes a string into its quoted-printable form using the default charset. Unsafe characters are escaped. * * @param pString * string to convert to quoted-printable form * @return quoted-printable string * * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ public String encode(String pString) throws EncoderException { if (pString == null) { return null; } return encode(pString, getDefaultCharset()); }
@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. } }