/** * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The * url-safe variation emits - and _ instead of + and / characters. * <b>Note: no padding is added.</b> * @param binaryData * binary data to encode * @return String containing Base64 characters * @since 1.4 */ public static String encodeBase64URLSafeString(final byte[] binaryData) { return StringUtils.newStringUsAscii(encodeBase64(binaryData, false, true)); }
/** * Encodes binary data using the base64 algorithm but does not chunk the output. * * NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to * single-line non-chunking (commons-codec-1.5). * * @param binaryData * binary data to encode * @return String containing Base64 characters. * @since 1.4 (NOTE: 1.4 chunked the output, whereas 1.5 does not). */ public static String encodeBase64String(final byte[] binaryData) { return StringUtils.newStringUsAscii(encodeBase64(binaryData, false)); }
/** * Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped. * * @param str * string to convert to a URL safe form * @param charset * the charset for str * @return URL safe string * @throws UnsupportedEncodingException * Thrown if 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))); }
/** * 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 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))); } }
/** * Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset. * <p> * This method constructs the "encoded-word" header common to all the RFC 1522 codecs and then invokes * {@link #doEncoding(byte [])} method of a concrete class to perform the specific encoding. * * @param text * a string to encode * @param charset * a charset to be used * @return RFC 1522 compliant "encoded-word" * @throws EncoderException * thrown if there is an error condition during the Encoding process. * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> */ protected String encodeText(final String text, final Charset charset) throws EncoderException { if (text == null) { return null; } final StringBuilder buffer = new StringBuilder(); buffer.append(PREFIX); buffer.append(charset); buffer.append(SEP); buffer.append(this.getEncoding()); buffer.append(SEP); final byte [] rawData = this.doEncoding(text.getBytes(charset)); buffer.append(StringUtils.newStringUsAscii(rawData)); buffer.append(POSTFIX); return buffer.toString(); }
private void testEncodeDecode(final String plainText) { final String encodedText = Base64.encodeBase64String(StringUtils.getBytesUtf8(plainText)); final String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText)); assertEquals(plainText, decodedText); }
private void testDecodeEncode(final String encodedText) { final String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText)); final String encodedText2 = Base64.encodeBase64String(StringUtils.getBytesUtf8(decodedText)); assertEquals(encodedText, encodedText2); }
/** * Tests RFC 4648 section 10 test vectors. * <ul> * <li>BASE64("") = ""</li> * <li>BASE64("f") = "Zg=="</li> * <li>BASE64("fo") = "Zm8="</li> * <li>BASE64("foo") = "Zm9v"</li> * <li>BASE64("foob") = "Zm9vYg=="</li> * <li>BASE64("fooba") = "Zm9vYmE="</li> * <li>BASE64("foobar") = "Zm9vYmFy"</li> * </ul> * * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/ * html/rfc4648</a> */ @Test public void testRfc4648Section10DecodeWithCrLf() { final String CRLF = StringUtils.newStringUsAscii(Base64.CHUNK_SEPARATOR); assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64("" + CRLF))); assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg==" + CRLF))); assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8=" + CRLF))); assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v" + CRLF))); assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg==" + CRLF))); assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE=" + CRLF))); assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy" + CRLF))); }
/** * Tests RFC 4648 section 10 test vectors. * <ul> * <li>BASE64("") = ""</li> * <li>BASE64("f") = "Zg=="</li> * <li>BASE64("fo") = "Zm8="</li> * <li>BASE64("foo") = "Zm9v"</li> * <li>BASE64("foob") = "Zm9vYg=="</li> * <li>BASE64("fooba") = "Zm9vYmE="</li> * <li>BASE64("foobar") = "Zm9vYmFy"</li> * </ul> * * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/ * html/rfc4648</a> */ @Test public void testRfc4648Section10Decode() { assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64(""))); assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg=="))); assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8="))); assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v"))); assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg=="))); assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE="))); assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy"))); }
@Test public void testNewStringUsAscii() throws UnsupportedEncodingException { final String charsetName = "US-ASCII"; testNewString(charsetName); final String expected = new String(BYTES_FIXTURE, charsetName); final String actual = StringUtils.newStringUsAscii(BYTES_FIXTURE); Assert.assertEquals(expected, actual); }
@Test public void testNewStringNullInput_CODEC229() { Assert.assertNull(StringUtils.newStringUtf8(null)); Assert.assertNull(StringUtils.newStringIso8859_1(null)); Assert.assertNull(StringUtils.newStringUsAscii(null)); Assert.assertNull(StringUtils.newStringUtf16(null)); Assert.assertNull(StringUtils.newStringUtf16Be(null)); Assert.assertNull(StringUtils.newStringUtf16Le(null)); }
/** * Encodes binary data using the base64 algorithm but does not chunk the output. * * NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to * single-line non-chunking (commons-codec-1.5). * * @param binaryData * binary data to encode * @return String containing Base64 characters. * @since 1.4 (NOTE: 1.4 chunked the output, whereas 1.5 does not). */ public static String encodeBase64String(final byte[] binaryData) { return StringUtils.newStringUsAscii(encodeBase64(binaryData, false)); }
/** * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The * url-safe variation emits - and _ instead of + and / characters. * <b>Note: no padding is added.</b> * @param binaryData * binary data to encode * @return String containing Base64 characters * @since 1.4 */ public static String encodeBase64URLSafeString(final byte[] binaryData) { return StringUtils.newStringUsAscii(encodeBase64(binaryData, false, true)); }
/** * Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped. * * @param str * string to convert to a URL safe form * @param charset * the charset for str * @return URL safe string * @throws UnsupportedEncodingException * Thrown if 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))); }
/** * Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped. * * @param str * string to convert to a URL safe form * @param charset * the charset for str * @return URL safe string * @throws UnsupportedEncodingException * Thrown if 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))); }
/** * Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped. * * @param str * string to convert to a URL safe form * @param charset * the charset for str * @return URL safe string * @throws UnsupportedEncodingException * Thrown if 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))); }
/** * Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped. * * @param str * string to convert to a URL safe form * @param charset * the charset for str * @return URL safe string * @throws UnsupportedEncodingException * Thrown if 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))); }
/** * Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped. * * @param str * string to convert to a URL safe form * @param charset * the charset for str * @return URL safe string * @throws UnsupportedEncodingException * Thrown if 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))); }
public String encodeUrl(String unescaped) { byte[] rawdata = URLCodec.encodeUrl(URI.allowed_query, org.apache.commons.codec.binary.StringUtils.getBytesUtf8(unescaped)); return org.apache.commons.codec.binary.StringUtils.newStringUsAscii(rawdata); }