Author: julius Date: Tue Jan 25 01:16:49 2011 New Revision: 1063091 URL: http://svn.apache.org/viewvc?rev=1063091&view=rev Log: CODEC-110 - Add a String version of Base64.isArrayByteBase64()
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java?rev=1063091&r1=1063090&r2=1063091&view=diff ============================================================================== --- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java (original) +++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java Tue Jan 25 01:16:49 2011 @@ -608,6 +608,19 @@ public class Base64 implements BinaryEnc } /** + * Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the + * method treats whitespace as valid. + * + * @param base64 + * String of (presumably) base64 characters to test + * @return <code>true</code> if all characters in the String are valid characters in the Base64 alphabet or if + * the String is empty; false, otherwise + */ + public static boolean isStringBase64(String base64) { + return isArrayByteBase64(StringUtils.getBytesUtf8(base64)); + } + + /** * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. * * @param arrayOctet Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java?rev=1063091&r1=1063090&r2=1063091&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java (original) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java Tue Jan 25 01:16:49 2011 @@ -56,6 +56,27 @@ public class Base64Test extends TestCase } /** + * Test the isStringBase64 method. + */ + public void testIsStringBase64() { + String nullString = null; + String emptyString = ""; + String validString = "abc===defg\n\r123456\r789\r\rABC\n\nDEF==GHI\r\nJKL=============="; + String invalidString = validString + ((char)0); // append null character + + try { + Base64.isStringBase64(nullString); + fail("Base64.isStringBase64() should not be null-safe."); + } catch (NullPointerException npe) { + assertNotNull("Base64.isStringBase64() should not be null-safe.", npe); + } + + assertTrue("Base64.isStringBase64(empty-string) is true", Base64.isStringBase64(emptyString)); + assertTrue("Base64.isStringBase64(valid-string) is true", Base64.isStringBase64(validString)); + assertFalse("Base64.isStringBase64(invalid-string) is false", Base64.isStringBase64(invalidString)); + } + + /** * Test the Base64 implementation */ public void testBase64() {