File tree Expand file tree Collapse file tree
java/org/apache/commons/codec/binary
test/org/apache/commons/codec/binary Expand file tree Collapse file tree Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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 \r 123456\r 789\r \r ABC\n \n DEF==GHI\r \n JKL==============" ;
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 */
You can’t perform that action at this time.
0 commit comments