You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2015/05/02 19:06:20 UTC

svn commit: r1677334 - /webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java

Author: veithen
Date: Sat May  2 17:06:19 2015
New Revision: 1677334

URL: http://svn.apache.org/r1677334
Log:
Duplicate code reduction.

Modified:
    webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java

Modified: webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java?rev=1677334&r1=1677333&r2=1677334&view=diff
==============================================================================
--- webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java (original)
+++ webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java Sat May  2 17:06:19 2015
@@ -31,25 +31,6 @@ import org.apache.axiom.util.activation.
  * Contains utility methods to work with base64 encoded data.
  */
 public class Base64Utils {
-    private static final char[] S_BASE64CHAR = { 'A', 'B', 'C', 'D', 'E', 'F',
-        'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
-        'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
-        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
-        't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
-        '6', '7', '8', '9', '+', '/' };
-
-    private static final char S_BASE64PAD = '=';
-    
-    private static final byte[] S_DECODETABLE = new byte[128];
-    
-    static {
-        for (int i = 0; i < S_DECODETABLE.length; i++)
-            S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
-        for (int i = 0; i < S_BASE64CHAR.length; i++)
-            // 0 to 63
-            S_DECODETABLE[S_BASE64CHAR[i]] = (byte) i;
-    }
-    
     private static int getEncodedSize(int unencodedSize) {
         return (unencodedSize+2) / 3 * 4;
     }
@@ -102,14 +83,14 @@ public class Base64Utils {
 
     private static int decode0(char[] ibuf, byte[] obuf, int wp) {
         int outlen = 3;
-        if (ibuf[3] == S_BASE64PAD)
+        if (ibuf[3] == Base64Constants.S_BASE64PAD)
             outlen = 2;
-        if (ibuf[2] == S_BASE64PAD)
+        if (ibuf[2] == Base64Constants.S_BASE64PAD)
             outlen = 1;
-        int b0 = S_DECODETABLE[ibuf[0]];
-        int b1 = S_DECODETABLE[ibuf[1]];
-        int b2 = S_DECODETABLE[ibuf[2]];
-        int b3 = S_DECODETABLE[ibuf[3]];
+        int b0 = Base64Constants.S_DECODETABLE[ibuf[0]];
+        int b1 = Base64Constants.S_DECODETABLE[ibuf[1]];
+        int b2 = Base64Constants.S_DECODETABLE[ibuf[2]];
+        int b3 = Base64Constants.S_DECODETABLE[ibuf[3]];
         switch (outlen) {
             case 1:
                 obuf[wp] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
@@ -138,8 +119,8 @@ public class Base64Utils {
         int obufcount = 0;
         for (int i = off; i < off + len; i++) {
             char ch = data[i];
-            if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
-                    && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
+            if (ch == Base64Constants.S_BASE64PAD || ch < Base64Constants.S_DECODETABLE.length
+                    && Base64Constants.S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                 ibuf[ibufcount++] = ch;
                 if (ibufcount == ibuf.length) {
                     ibufcount = 0;
@@ -164,8 +145,8 @@ public class Base64Utils {
         int obufcount = 0;
         for (int i = 0; i < data.length(); i++) {
             char ch = data.charAt(i);
-            if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
-                    && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
+            if (ch == Base64Constants.S_BASE64PAD || ch < Base64Constants.S_DECODETABLE.length
+                    && Base64Constants.S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                 ibuf[ibufcount++] = ch;
                 if (ibufcount == ibuf.length) {
                     ibufcount = 0;
@@ -187,8 +168,8 @@ public class Base64Utils {
         for (int i = 0; i < data.length(); i++) {
             char ch = data.charAt(i);
 
-            if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
-                    && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
+            if (ch == Base64Constants.S_BASE64PAD || ch < Base64Constants.S_DECODETABLE.length
+                    && Base64Constants.S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                 //valid character.Do nothing
             } else if (ch == '\r' || ch == '\n') {
                 //do nothing
@@ -210,8 +191,8 @@ public class Base64Utils {
         byte[] obuf = new byte[3];
         for (int i = off; i < off + len; i++) {
             char ch = data[i];
-            if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
-                    && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
+            if (ch == Base64Constants.S_BASE64PAD || ch < Base64Constants.S_DECODETABLE.length
+                    && Base64Constants.S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                 ibuf[ibufcount++] = ch;
                 if (ibufcount == ibuf.length) {
                     ibufcount = 0;
@@ -232,8 +213,8 @@ public class Base64Utils {
         byte[] obuf = new byte[3];
         for (int i = 0; i < data.length(); i++) {
             char ch = data.charAt(i);
-            if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
-                    && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
+            if (ch == Base64Constants.S_BASE64PAD || ch < Base64Constants.S_DECODETABLE.length
+                    && Base64Constants.S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                 ibuf[ibufcount++] = ch;
                 if (ibufcount == ibuf.length) {
                     ibufcount = 0;
@@ -265,25 +246,25 @@ public class Base64Utils {
             int i = ((data[rindex] & 0xff) << 16)
                     + ((data[rindex + 1] & 0xff) << 8)
                     + (data[rindex + 2] & 0xff);
-            out[windex++] = S_BASE64CHAR[i >> 18];
-            out[windex++] = S_BASE64CHAR[(i >> 12) & 0x3f];
-            out[windex++] = S_BASE64CHAR[(i >> 6) & 0x3f];
-            out[windex++] = S_BASE64CHAR[i & 0x3f];
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[i >> 18];
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[(i >> 12) & 0x3f];
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[(i >> 6) & 0x3f];
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[i & 0x3f];
             rindex += 3;
             rest -= 3;
         }
         if (rest == 1) {
             int i = data[rindex] & 0xff;
-            out[windex++] = S_BASE64CHAR[i >> 2];
-            out[windex++] = S_BASE64CHAR[(i << 4) & 0x3f];
-            out[windex++] = S_BASE64PAD;
-            out[windex++] = S_BASE64PAD;
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[i >> 2];
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[(i << 4) & 0x3f];
+            out[windex++] = Base64Constants.S_BASE64PAD;
+            out[windex++] = Base64Constants.S_BASE64PAD;
         } else if (rest == 2) {
             int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
-            out[windex++] = S_BASE64CHAR[i >> 10];
-            out[windex++] = S_BASE64CHAR[(i >> 4) & 0x3f];
-            out[windex++] = S_BASE64CHAR[(i << 2) & 0x3f];
-            out[windex++] = S_BASE64PAD;
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[i >> 10];
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[(i >> 4) & 0x3f];
+            out[windex++] = (char)Base64Constants.S_BASE64CHAR[(i << 2) & 0x3f];
+            out[windex++] = Base64Constants.S_BASE64PAD;
         }
         return new String(out, 0, windex);
     }
@@ -303,27 +284,27 @@ public class Base64Utils {
             int i = ((data[rindex] & 0xff) << 16)
                     + ((data[rindex + 1] & 0xff) << 8)
                     + (data[rindex + 2] & 0xff);
-            out[0] = S_BASE64CHAR[i >> 18];
-            out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
-            out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
-            out[3] = S_BASE64CHAR[i & 0x3f];
+            out[0] = (char)Base64Constants.S_BASE64CHAR[i >> 18];
+            out[1] = (char)Base64Constants.S_BASE64CHAR[(i >> 12) & 0x3f];
+            out[2] = (char)Base64Constants.S_BASE64CHAR[(i >> 6) & 0x3f];
+            out[3] = (char)Base64Constants.S_BASE64CHAR[i & 0x3f];
             buffer.append(out);
             rindex += 3;
             rest -= 3;
         }
         if (rest == 1) {
             int i = data[rindex] & 0xff;
-            out[0] = S_BASE64CHAR[i >> 2];
-            out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
-            out[2] = S_BASE64PAD;
-            out[3] = S_BASE64PAD;
+            out[0] = (char)Base64Constants.S_BASE64CHAR[i >> 2];
+            out[1] = (char)Base64Constants.S_BASE64CHAR[(i << 4) & 0x3f];
+            out[2] = Base64Constants.S_BASE64PAD;
+            out[3] = Base64Constants.S_BASE64PAD;
             buffer.append(out);
         } else if (rest == 2) {
             int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
-            out[0] = S_BASE64CHAR[i >> 10];
-            out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
-            out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
-            out[3] = S_BASE64PAD;
+            out[0] = (char)Base64Constants.S_BASE64CHAR[i >> 10];
+            out[1] = (char)Base64Constants.S_BASE64CHAR[(i >> 4) & 0x3f];
+            out[2] = (char)Base64Constants.S_BASE64CHAR[(i << 2) & 0x3f];
+            out[3] = (char)Base64Constants.S_BASE64PAD;
             buffer.append(out);
         }
     }
@@ -342,27 +323,27 @@ public class Base64Utils {
             int i = ((data[rindex] & 0xff) << 16)
                     + ((data[rindex + 1] & 0xff) << 8)
                     + (data[rindex + 2] & 0xff);
-            out[0] = (byte) S_BASE64CHAR[i >> 18];
-            out[1] = (byte) S_BASE64CHAR[(i >> 12) & 0x3f];
-            out[2] = (byte) S_BASE64CHAR[(i >> 6) & 0x3f];
-            out[3] = (byte) S_BASE64CHAR[i & 0x3f];
+            out[0] = Base64Constants.S_BASE64CHAR[i >> 18];
+            out[1] = Base64Constants.S_BASE64CHAR[(i >> 12) & 0x3f];
+            out[2] = Base64Constants.S_BASE64CHAR[(i >> 6) & 0x3f];
+            out[3] = Base64Constants.S_BASE64CHAR[i & 0x3f];
             ostream.write(out, 0, 4);
             rindex += 3;
             rest -= 3;
         }
         if (rest == 1) {
             int i = data[rindex] & 0xff;
-            out[0] = (byte) S_BASE64CHAR[i >> 2];
-            out[1] = (byte) S_BASE64CHAR[(i << 4) & 0x3f];
-            out[2] = (byte) S_BASE64PAD;
-            out[3] = (byte) S_BASE64PAD;
+            out[0] = Base64Constants.S_BASE64CHAR[i >> 2];
+            out[1] = Base64Constants.S_BASE64CHAR[(i << 4) & 0x3f];
+            out[2] = Base64Constants.S_BASE64PAD;
+            out[3] = Base64Constants.S_BASE64PAD;
             ostream.write(out, 0, 4);
         } else if (rest == 2) {
             int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
-            out[0] = (byte) S_BASE64CHAR[i >> 10];
-            out[1] = (byte) S_BASE64CHAR[(i >> 4) & 0x3f];
-            out[2] = (byte) S_BASE64CHAR[(i << 2) & 0x3f];
-            out[3] = (byte) S_BASE64PAD;
+            out[0] = Base64Constants.S_BASE64CHAR[i >> 10];
+            out[1] = Base64Constants.S_BASE64CHAR[(i >> 4) & 0x3f];
+            out[2] = Base64Constants.S_BASE64CHAR[(i << 2) & 0x3f];
+            out[3] = Base64Constants.S_BASE64PAD;
             ostream.write(out, 0, 4);
         }
     }
@@ -382,10 +363,10 @@ public class Base64Utils {
             int i = ((data[rindex] & 0xff) << 16)
                     + ((data[rindex + 1] & 0xff) << 8)
                     + (data[rindex + 2] & 0xff);
-            out[0] = S_BASE64CHAR[i >> 18];
-            out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
-            out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
-            out[3] = S_BASE64CHAR[i & 0x3f];
+            out[0] = (char)Base64Constants.S_BASE64CHAR[i >> 18];
+            out[1] = (char)Base64Constants.S_BASE64CHAR[(i >> 12) & 0x3f];
+            out[2] = (char)Base64Constants.S_BASE64CHAR[(i >> 6) & 0x3f];
+            out[3] = (char)Base64Constants.S_BASE64CHAR[i & 0x3f];
             writer.write(out, 0, 4);
             rindex += 3;
             rest -= 3;
@@ -395,17 +376,17 @@ public class Base64Utils {
         }
         if (rest == 1) {
             int i = data[rindex] & 0xff;
-            out[0] = S_BASE64CHAR[i >> 2];
-            out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
-            out[2] = S_BASE64PAD;
-            out[3] = S_BASE64PAD;
+            out[0] = (char)Base64Constants.S_BASE64CHAR[i >> 2];
+            out[1] = (char)Base64Constants.S_BASE64CHAR[(i << 4) & 0x3f];
+            out[2] = (char)Base64Constants.S_BASE64PAD;
+            out[3] = (char)Base64Constants.S_BASE64PAD;
             writer.write(out, 0, 4);
         } else if (rest == 2) {
             int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
-            out[0] = S_BASE64CHAR[i >> 10];
-            out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
-            out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
-            out[3] = S_BASE64PAD;
+            out[0] = (char)Base64Constants.S_BASE64CHAR[i >> 10];
+            out[1] = (char)Base64Constants.S_BASE64CHAR[(i >> 4) & 0x3f];
+            out[2] = (char)Base64Constants.S_BASE64CHAR[(i << 2) & 0x3f];
+            out[3] = (char)Base64Constants.S_BASE64PAD;
             writer.write(out, 0, 4);
         }
     }