mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-18 05:23:59 +00:00
Added support for base64Encode
This commit is contained in:
@@ -302,9 +302,21 @@ public abstract class Strman {
|
||||
* @return decoded data
|
||||
*/
|
||||
public static String base64Decode(final String value) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
return new String(Base64.getDecoder().decode(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes data with MIME base64.
|
||||
*
|
||||
* @param value The data to encode
|
||||
* @return The encoded String
|
||||
*/
|
||||
public static String base64Encode(final String value) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
return Base64.getEncoder().encodeToString(value.getBytes());
|
||||
}
|
||||
|
||||
private static long countSubstr(String value, String subStr, boolean allowOverlapping, long count) {
|
||||
int position = value.indexOf(subStr);
|
||||
if (position == -1) {
|
||||
|
||||
@@ -253,6 +253,15 @@ public class StrmanTest {
|
||||
assertThat(base64Decode("YmFy"), equalTo("bar"));
|
||||
assertThat(base64Decode("YsOhciE="), equalTo("bár!"));
|
||||
assertThat(base64Decode("5ryi"), equalTo("漢"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void base64Encode_shouldEncodeAString() throws Exception {
|
||||
assertThat(base64Encode("strman"), equalTo("c3RybWFu"));
|
||||
assertThat(base64Encode("foo"), equalTo("Zm9v"));
|
||||
assertThat(base64Encode("bar"), equalTo("YmFy"));
|
||||
assertThat(base64Encode("bár!"), equalTo("YsOhciE="));
|
||||
assertThat(base64Encode("漢"), equalTo("5ryi"));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user