/** * Encodes an array of bytes into an array of URL safe 7-bit characters. Unsafe characters are escaped. * * @param bytes * array of bytes to convert to URL safe characters * @return array of bytes containing URL safe characters */ @Override public byte[] encode(final byte[] bytes) { return encodeUrl(WWW_FORM_URL_SAFE, bytes); }
/** * Decodes an array of URL safe 7-bit characters into an array of original bytes. Escaped characters are converted * back to their original representation. * * @param bytes * array of URL safe characters * @return array of original bytes * @throws DecoderException * Thrown if URL decoding is unsuccessful */ @Override public byte[] decode(final byte[] bytes) throws DecoderException { return decodeUrl(bytes); }
@Override protected byte[] doEncoding(final byte[] bytes) { if (bytes == null) { return null; } final byte[] data = QuotedPrintableCodec.encodeQuotedPrintable(PRINTABLE_CHARS, bytes); if (this.encodeBlanks) { for (int i = 0; i < data.length; i++) { if (data[i] == BLANK) { data[i] = UNDERSCORE; } } } return data; }
@Test public void testSafeCharEncodeDecode() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "abc123_-.*"; final String encoded = urlCodec.encode(plain); assertEquals("Safe chars URL encoding test", plain, encoded); assertEquals("Safe chars URL decoding test", plain, urlCodec.decode(encoded)); this.validateState(urlCodec); }
@Test public void testDefaultEncoding() throws Exception { final String plain = "Hello there!"; final URLCodec urlCodec = new URLCodec("UnicodeBig"); urlCodec.encode(plain); // To work around a weird quirk in Java 1.2.2 final String encoded1 = urlCodec.encode(plain, "UnicodeBig"); final String encoded2 = urlCodec.encode(plain); assertEquals(encoded1, encoded2); this.validateState(urlCodec); } }
@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 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))); }
@Test public void testUTF8RoundTrip() throws Exception { final String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); final BCodec bcodec = new BCodec(CharEncoding.UTF_8); assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg)); assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg)); assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg))); assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg))); }
/** * 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 Base64 form using the default charset. Unsafe characters are escaped. * * @param value * string to convert to Base64 form * @return Base64 string * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ @Override public String encode(final String value) throws EncoderException { if (value == null) { return null; } return encode(value, this.getCharset()); }
/** * 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 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); }
/** * 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); }
private void assertExpectedDecoderException(final String s) throws Exception { final RFC1522TestCodec testcodec = new RFC1522TestCodec(); try { testcodec.decodeText(s); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { // Expected. } }
@Test public void testBasicEncodeDecode() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "Hello there!"; final String encoded = urlCodec.encode(plain); assertEquals("Basic URL encoding test", "Hello+there%21", encoded); assertEquals("Basic URL decoding test", plain, urlCodec.decode(encoded)); this.validateState(urlCodec); }
@Test public void testEncodeNull() throws Exception { final URLCodec urlCodec = new URLCodec(); final byte[] plain = null; final byte[] encoded = urlCodec.encode(plain); assertEquals("Encoding a null string should return null", null, encoded); this.validateState(urlCodec); }
/** * 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); }
@Test public void testEncodeDecodeNull() throws Exception { final URLCodec urlCodec = new URLCodec(); assertNull("Null string URL encoding test", urlCodec.encode((String)null)); assertNull("Null string URL decoding test", urlCodec.decode((String)null)); this.validateState(urlCodec); }
/** * 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 testUnsafeEncodeDecode() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "~!@#$%^&()+{}\"\\;:`,/[]"; final String encoded = urlCodec.encode(plain); assertEquals("Unsafe chars URL encoding test", "%7E%21%40%23%24%25%5E%26%28%29%2B%7B%7D%22%5C%3B%3A%60%2C%2F%5B%5D", encoded); assertEquals("Unsafe chars URL decoding test", plain, urlCodec.decode(encoded)); this.validateState(urlCodec); }