There seem to be 2 issues. 
  1. Only 255 byte values are used when creating base64Alphabet, but there are 256 possible values
  2. octect (shouldn't that be octet ?) isn't shifted despite the fact that Java uses unsigned bytes.  It is used unchanged as on offset into an array (zero based).

As a result I can produce test cases that cause isBase64() to throw IndexOutOfBounds exceptions.

While at it, I suggest one line be added to discardWhitespace() that will improve performance under some circumstances (i.e., when there is no whitespace in the input).

Patch file is attached.Patch file is based on latest version in CVS of Base64.java file: 1.23

= Ezra E.

P.S., I can provide a jUnit TestCase if that's useful.

 

--- Base64.java.cvs.1.23        2005-01-24 16:59:46.211939200 -0800
+++ Base64.java.changed 2005-01-24 17:03:42.782110400 -0800
@@ -55,7 +55,7 @@
     /**
      * The base length.
      */
-    static final int BASELENGTH = 255;
+    static final int BASELENGTH = 256;
 
     /**
      * Lookup length.
@@ -162,7 +162,7 @@
     private static boolean isBase64(byte octect) {
         if (octect == PAD) {
             return true;
-        } else if (octect < 0 || base64Alphabet[octect] == -1) {
+        } else if (octect < 0 || base64Alphabet[(octect > 0) ? octect : 256 + 
octect] == -1) {
             return false;
         } else {
             return true;
@@ -482,6 +482,9 @@
             }
         }
 
+       // Check if we're done and save resources if we are.
+       if (bytesCopied == data.length) return groomedData;
+       
         byte packedData[] = new byte[bytesCopied];
 
         System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to