/** * 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()); }
/** * Encodes an object into its Base64 form using the default charset. Unsafe characters are escaped. * * @param value * object to convert to Base64 form * @return Base64 object * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ @Override public Object encode(final Object value) throws EncoderException { if (value == null) { return null; } else if (value instanceof String) { return encode((String) value); } else { throw new EncoderException("Objects of type " + value.getClass().getName() + " cannot be encoded using BCodec"); } }
/** * 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. */ public String encode(String value) throws EncoderException { if (value == null) { return null; } return encode(value, getDefaultCharset()); }
/** * Encodes an object into its Base64 form using the default charset. Unsafe characters are escaped. * * @param value * object to convert to Base64 form * @return Base64 object * * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ public Object encode(Object value) throws EncoderException { if (value == null) { return null; } else if (value instanceof String) { return encode((String) value); } else { throw new EncoderException("Objects of type " + value.getClass().getName() + " cannot be encoded using BCodec"); } }
@Test public void testEncodeObjects() throws Exception { final BCodec bcodec = new BCodec(); final String plain = "what not"; final String encoded = (String) bcodec.encode((Object) plain); assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded); final Object result = bcodec.encode((Object) null); assertEquals("Encoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); bcodec.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 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))); }
@Test public void testEncodeStringWithNull() throws Exception { final BCodec bcodec = new BCodec(); final String test = null; final String result = bcodec.encode(test, "charset"); assertEquals("Result should be null", null, result); }
@Test public void testBasicEncodeDecode() throws Exception { final BCodec bcodec = new BCodec(); final String plain = "Hello there"; final String encoded = bcodec.encode(plain); assertEquals("Basic B encoding test", "=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded); assertEquals("Basic B decoding test", plain, bcodec.decode(encoded)); }
@Test public void testEncodeDecodeNull() throws Exception { final BCodec bcodec = new BCodec(); assertNull("Null string B encoding test", bcodec.encode((String) null)); assertNull("Null string B decoding test", bcodec.decode((String) null)); }
/** * 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. */ public String encode(String value) throws EncoderException { if (value == null) { return null; } return encode(value, getDefaultCharset()); }
/** * 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()); }
/** * 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()); }
/** * 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. */ public String encode(String value) throws EncoderException { if (value == null) { return null; } return encode(value, getDefaultCharset()); }
/** * 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()); }
/** * 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. */ public String encode(String value) throws EncoderException { if (value == null) { return null; } return encode(value, getDefaultCharset()); }
/** * 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()); }
/** * 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. */ public String encode(String value) throws EncoderException { if (value == null) { return null; } return encode(value, getDefaultCharset()); }
/** * 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()); }
/** * 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. */ public String encode(String value) throws EncoderException { if (value == null) { return null; } return encode(value, getDefaultCharset()); }
/** * Used to encode a string as specified by RFC 2047 * @param value The string to encode * @param charset The character set to use for the encoding */ public static String encode(String value, String charset, Codec codec) { if (value == null) return null; try { switch(codec) { case Q: return (new QCodec(charset)).encode(value); case B: default: return (new BCodec(charset)).encode(value); } } catch (Exception e) { return value; } }