Skip to content

Commit 5223a63

Browse files
author
Julius Davies
committed
CODEC-110 - Add a String version of Base64.isArrayByteBase64()
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1063091 13f79535-47bb-0310-9956-ffa450edef68
1 parent d8acbb0 commit 5223a63

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/java/org/apache/commons/codec/binary/Base64.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,19 @@ public static boolean isArrayByteBase64(byte[] arrayOctet) {
607607
return true;
608608
}
609609

610+
/**
611+
* Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the
612+
* method treats whitespace as valid.
613+
*
614+
* @param base64
615+
* String of (presumably) base64 characters to test
616+
* @return <code>true</code> if all characters in the String are valid characters in the Base64 alphabet or if
617+
* the String is empty; false, otherwise
618+
*/
619+
public static boolean isStringBase64(String base64) {
620+
return isArrayByteBase64(StringUtils.getBytesUtf8(base64));
621+
}
622+
610623
/**
611624
* Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
612625
*

src/test/org/apache/commons/codec/binary/Base64Test.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ public Random getRandom() {
5555
return this._random;
5656
}
5757

58+
/**
59+
* Test the isStringBase64 method.
60+
*/
61+
public void testIsStringBase64() {
62+
String nullString = null;
63+
String emptyString = "";
64+
String validString = "abc===defg\n\r123456\r789\r\rABC\n\nDEF==GHI\r\nJKL==============";
65+
String invalidString = validString + ((char)0); // append null character
66+
67+
try {
68+
Base64.isStringBase64(nullString);
69+
fail("Base64.isStringBase64() should not be null-safe.");
70+
} catch (NullPointerException npe) {
71+
assertNotNull("Base64.isStringBase64() should not be null-safe.", npe);
72+
}
73+
74+
assertTrue("Base64.isStringBase64(empty-string) is true", Base64.isStringBase64(emptyString));
75+
assertTrue("Base64.isStringBase64(valid-string) is true", Base64.isStringBase64(validString));
76+
assertFalse("Base64.isStringBase64(invalid-string) is false", Base64.isStringBase64(invalidString));
77+
}
78+
5879
/**
5980
* Test the Base64 implementation
6081
*/

0 commit comments

Comments
 (0)