@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)); }
/** * Decodes an array of quoted-printable characters into an array of original bytes. Escaped characters are converted * back to their original representation. * <p> * This function fully implements the quoted-printable encoding specification (rule #1 through rule #5) as * defined in RFC 1521. * * @param bytes * array of quoted-printable characters * @return array of original bytes * @throws DecoderException * Thrown if quoted-printable decoding is unsuccessful */ @Override public byte[] decode(final byte[] bytes) throws DecoderException { return decodeQuotedPrintable(bytes); }
final int b = getUnsignedOctet(i, bytes); if (pos < SAFE_LENGTH) { pos += encodeByte(b, !printable.get(b), buffer); } else { encodeByte(b, !printable.get(b) || isWhitespace(b), buffer); int b = getUnsignedOctet(bytes.length - 3, bytes); boolean encode = !printable.get(b) || (isWhitespace(b) && pos > SAFE_LENGTH - 5); pos += encodeByte(b, encode, buffer); b = getUnsignedOctet(i, bytes); encode = !printable.get(b) || (i > bytes.length - 2 && isWhitespace(b)); encodeByte(b, encode, buffer); buffer.write(b); } else { encodeQuotedPrintable(b, buffer);
/** * 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()); }
/** * 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()); }
@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 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); }
/** * Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped. * <p> * This function implements 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 printable * bitset of characters deemed quoted-printable * @param bytes * array of bytes to be encoded * @return array of bytes containing quoted-printable data */ public static final byte[] encodeQuotedPrintable(final BitSet printable, final byte[] bytes) { return encodeQuotedPrintable(printable, bytes, false); }
@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)); }
/** * Encodes a string into its quoted-printable form using the default string charset. Unsafe characters are escaped. * * <p> * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in * RFC 1521 and is suitable for encoding binary data. * </p> * * @param pString * string to convert to quoted-printable form * @return quoted-printable string * * @throws EncoderException * Thrown if quoted-printable encoding is unsuccessful * * @see #getDefaultCharset() */ public String encode(String pString) throws EncoderException { if (pString == null) { return null; } try { return encode(pString, getDefaultCharset()); } catch (UnsupportedEncodingException e) { throw new EncoderException(e.getMessage()); } }
/** * 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()); } }
/** * 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"); } }
/** * 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"); } }
@Test(expected=UnsupportedCharsetException.class) public void testInvalidEncoding() { new QuotedPrintableCodec("NONSENSE"); }
@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 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 } }
/** * 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 array of bytes into an array of quoted-printable 7-bit characters. 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 bytes * array of bytes to be encoded * @return array of bytes containing quoted-printable data */ @Override public byte[] encode(final byte[] bytes) { return encodeQuotedPrintable(PRINTABLE_CHARS, bytes, strict); }
/** * 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()); }
/** * Encodes a string into its quoted-printable form using the default string charset. Unsafe characters are escaped. * * <p> * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in * RFC 1521 and is suitable for encoding binary data. * </p> * * @param pString * string to convert to quoted-printable form * @return quoted-printable string * * @throws EncoderException * Thrown if quoted-printable encoding is unsuccessful * * @see #getDefaultCharset() */ public String encode(String pString) throws EncoderException { if (pString == null) { return null; } try { return encode(pString, getDefaultCharset()); } catch (UnsupportedEncodingException e) { throw new EncoderException(e.getMessage()); } }