Skip to content

Commit b7832d4

Browse files
committed
Applying Sebb's patch from CODEC-71, improving the memory consumption of isArrayByteBase64(). I've also deprecated the discardWhitespace method as nothing uses it now
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@672993 13f79535-47bb-0310-9956-ffa450edef68
1 parent d09530a commit b7832d4

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,8 @@ public static boolean isBase64(byte octet) {
476476
* empty; false, otherwise
477477
*/
478478
public static boolean isArrayByteBase64(byte[] arrayOctet) {
479-
480-
arrayOctet = discardWhitespace(arrayOctet);
481-
482-
int length = arrayOctet.length;
483-
if (length == 0) {
484-
return true;
485-
}
486-
for (int i = 0; i < length; i++) {
487-
if (!isBase64(arrayOctet[i])) {
479+
for (int i = 0; i < arrayOctet.length; i++) {
480+
if (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) {
488481
return false;
489482
}
490483
}
@@ -630,6 +623,7 @@ public static byte[] decodeBase64(byte[] base64Data) {
630623
* @param data
631624
* The base-64 encoded data to discard the whitespace from.
632625
* @return The data, less whitespace (see RFC 2045).
626+
* @deprecated This method is no longer needed
633627
*/
634628
static byte[] discardWhitespace(byte[] data) {
635629
byte groomedData[] = new byte[data.length];
@@ -654,6 +648,25 @@ static byte[] discardWhitespace(byte[] data) {
654648
return packedData;
655649
}
656650

651+
652+
/**
653+
* Check if a byte value is whitespace or not.
654+
*
655+
* @param byteToCheck the byte to check
656+
* @return true if byte is whitespace, false otherwise
657+
*/
658+
private static boolean isWhiteSpace(byte byteToCheck){
659+
switch (byteToCheck) {
660+
case ' ' :
661+
case '\n' :
662+
case '\r' :
663+
case '\t' :
664+
return true;
665+
default :
666+
return false;
667+
}
668+
}
669+
657670
/**
658671
* Discards any characters outside of the base64 alphabet, per the requirements on page 25 of RFC 2045 - "Any
659672
* characters outside of the base64 alphabet are to be ignored in base64 encoded data."

0 commit comments

Comments
 (0)