Skip to content

Commit 9250cf3

Browse files
committed
[CODEC-110] Add a String version of Base64.isArrayByteBase64(). https://issues.apache.org/jira/browse/CODEC-110
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1063100 13f79535-47bb-0310-9956-ffa450edef68
1 parent eabb909 commit 9250cf3

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,20 @@ public static boolean isBase64(byte octet) {
589589
return octet == PAD || (octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1);
590590
}
591591

592+
/**
593+
* Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the
594+
* method treats whitespace as valid.
595+
*
596+
* @param base64
597+
* String to test
598+
* @return <code>true</code> if all characters in the String are valid characters in the Base64 alphabet or if
599+
* the String is empty; <code>false</code>, otherwise
600+
* @since 1.5
601+
*/
602+
public static boolean isBase64(String base64) {
603+
return isArrayByteBase64(StringUtils.getBytesUtf8(base64));
604+
}
605+
592606
/**
593607
* Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the
594608
* method treats whitespace as valid.
@@ -606,19 +620,6 @@ public static boolean isArrayByteBase64(byte[] arrayOctet) {
606620
}
607621
return true;
608622
}
609-
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-
}
622623

623624
/**
624625
* Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ public void testIsStringBase64() {
6565
String invalidString = validString + ((char)0); // append null character
6666

6767
try {
68-
Base64.isStringBase64(nullString);
68+
Base64.isBase64(nullString);
6969
fail("Base64.isStringBase64() should not be null-safe.");
7070
} catch (NullPointerException npe) {
7171
assertNotNull("Base64.isStringBase64() should not be null-safe.", npe);
7272
}
7373

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));
74+
assertTrue("Base64.isStringBase64(empty-string) is true", Base64.isBase64(emptyString));
75+
assertTrue("Base64.isStringBase64(valid-string) is true", Base64.isBase64(validString));
76+
assertFalse("Base64.isStringBase64(invalid-string) is false", Base64.isBase64(invalidString));
7777
}
7878

7979
/**

0 commit comments

Comments
 (0)