/** * Encodes an object into its quoted-printable safe form. Unsafe characters are escaped. * * @param obj * string to convert to a quoted-printable form * @return quoted-printable object * @throws EncoderException * Thrown if quoted-printable encoding is not applicable to objects of this type or if encoding is * unsuccessful */ @Override public Object encode(final Object obj) throws EncoderException { if (obj == null) { return null; } else if (obj instanceof byte[]) { return encode((byte[]) obj); } else if (obj instanceof String) { return encode((String) obj); } else { throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be quoted-printable encoded"); } }
/** * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped. * <p> * Depending on the selection of the {@code strict} parameter, this function either implements the full ruleset * or only a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in * RFC 1521 and is suitable for encoding binary data and unformatted text. * * @param str * string to convert to quoted-printable form * @param charset * the charset for str * @return quoted-printable string * @since 1.7 */ public String encode(final String str, final Charset charset) { if (str == null) { return null; } return StringUtils.newStringUsAscii(this.encode(str.getBytes(charset))); }
/** * Encodes a string into its quoted-printable form using the default string charset. Unsafe characters are escaped. * <p> * Depending on the selection of the {@code strict} parameter, this function either implements the full ruleset * or only a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in * RFC 1521 and is suitable for encoding binary data and unformatted text. * * @param str * string to convert to quoted-printable form * @return quoted-printable string * @throws EncoderException * Thrown if quoted-printable encoding is unsuccessful * * @see #getCharset() */ @Override public String encode(final String str) throws EncoderException { return this.encode(str, getCharset()); }
/** * Encodes an object into its quoted-printable safe form. Unsafe characters are escaped. * * @param pObject * string to convert to a quoted-printable form * @return quoted-printable object * @throws EncoderException * Thrown if quoted-printable encoding is not applicable to objects of this type or if encoding is * unsuccessful */ public Object encode(Object pObject) throws EncoderException { if (pObject == null) { return null; } else if (pObject instanceof byte[]) { return encode((byte[]) pObject); } else if (pObject instanceof String) { return encode((String) pObject); } else { throw new EncoderException("Objects of type " + pObject.getClass().getName() + " cannot be quoted-printable encoded"); } }
/** * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped. * <p> * Depending on the selection of the {@code strict} parameter, this function either implements the full ruleset * or only a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in * RFC 1521 and is suitable for encoding binary data and unformatted text. * * @param str * string to convert to quoted-printable form * @param charset * the charset for str * @return quoted-printable string * @throws UnsupportedEncodingException * Thrown if the charset is not supported */ public String encode(final String str, final String charset) throws UnsupportedEncodingException { if (str == null) { return null; } return StringUtils.newStringUsAscii(encode(str.getBytes(charset))); } }
@Test public void testDefaultEncoding() throws Exception { final String plain = "Hello there!"; final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec("UnicodeBig"); qpcodec.encode(plain); // To work around a weird quirk in Java 1.2.2 final String encoded1 = qpcodec.encode(plain, "UnicodeBig"); final String encoded2 = qpcodec.encode(plain); assertEquals(encoded1, encoded2); }
@Test public void testUltimateSoftBreak() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true); String plain ="This is a example of a quoted-printable text file. There is no end to it\t"; String expected = "This is a example of a quoted-printable text file. There is no end to i=\r\nt=09"; assertEquals(expected, qpcodec.encode(plain)); plain ="This is a example of a quoted-printable text file. There is no end to it "; expected = "This is a example of a quoted-printable text file. There is no end to i=\r\nt=20"; assertEquals(expected, qpcodec.encode(plain)); // whitespace before soft break plain ="This is a example of a quoted-printable text file. There is no end to "; expected = "This is a example of a quoted-printable text file. There is no end to=20=\r\n =20"; assertEquals(expected, qpcodec.encode(plain)); // non-printable character before soft break plain ="This is a example of a quoted-printable text file. There is no end to= "; expected = "This is a example of a quoted-printable text file. There is no end to=3D=\r\n =20"; assertEquals(expected, qpcodec.encode(plain)); }
@Test public void testTrailingSpecial() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true); String plain ="This is a example of a quoted-printable text file. This might contain sp=cial chars."; String expected = "This is a example of a quoted-printable text file. This might contain sp=3D=\r\ncial chars."; assertEquals(expected, qpcodec.encode(plain)); plain ="This is a example of a quoted-printable text file. This might contain ta\tbs as well."; expected = "This is a example of a quoted-printable text file. This might contain ta=09=\r\nbs as well."; assertEquals(expected, qpcodec.encode(plain)); }
@Test public void testEncodeObjects() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String plain = "1+1 = 2"; String encoded = (String) qpcodec.encode((Object) plain); assertEquals("Basic quoted-printable encoding test", "1+1 =3D 2", encoded); final byte[] plainBA = plain.getBytes(Charsets.UTF_8); final byte[] encodedBA = (byte[]) qpcodec.encode((Object) plainBA); encoded = new String(encodedBA); assertEquals("Basic quoted-printable encoding test", "1+1 =3D 2", encoded); final Object result = qpcodec.encode((Object) null); assertEquals( "Encoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); qpcodec.encode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final EncoderException ee) { // Exception expected, test segment passes. } }
@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)); }
@Test public void testEncodeStringWithNull() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String test = null; final String result = qpcodec.encode( test, "charset" ); assertEquals("Result should be null", null, result); }
@Test public void testEncodeNull() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final byte[] plain = null; final byte[] encoded = qpcodec.encode(plain); assertEquals("Encoding a null string should return null", null, encoded); }
@Test public void testFinalBytes() throws Exception { // whitespace, but does not need to be encoded final String plain ="This is a example of a quoted=printable text file. There is no tt"; final String expected = "This is a example of a quoted=3Dprintable text file. There is no tt"; assertEquals(expected, new QuotedPrintableCodec(true).encode(plain)); } }
@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 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 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 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)); }