Author: veithen
Date: Fri May 14 19:43:02 2010
New Revision: 944423

URL: http://svn.apache.org/viewvc?rev=944423&view=rev
Log:
Merged all base64 encoding/decoding stuff into the org.apache.axiom.util.base64 
package. This also eliminates some package cycles.

Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/utils/DataHandlerUtils.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/Base64.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/Base64Test.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReaderTest.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/utils/DataHandlerUtils.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/utils/DataHandlerUtils.java?rev=944423&r1=944422&r2=944423&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/utils/DataHandlerUtils.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/utils/DataHandlerUtils.java
 Fri May 14 19:43:02 2010
@@ -20,16 +20,16 @@
 package org.apache.axiom.attachments.utils;
 
 
-import org.apache.axiom.attachments.ByteArrayDataSource;
-import org.apache.axiom.om.util.Base64;
-
 import javax.activation.DataHandler;
 
+import org.apache.axiom.attachments.ByteArrayDataSource;
+import org.apache.axiom.util.base64.Base64Utils;
+
 public class DataHandlerUtils {
 
     public static Object getDataHandlerFromText(String value, String mimeType) 
{
         ByteArrayDataSource dataSource;
-        byte[] data = Base64.decode(value);
+        byte[] data = Base64Utils.decode(value);
         if (mimeType != null) {
             dataSource = new ByteArrayDataSource(data, mimeType);
         } else {

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/Base64.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/Base64.java?rev=944423&r1=944422&r2=944423&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/Base64.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/Base64.java
 Fri May 14 19:43:02 2010
@@ -19,330 +19,8 @@
 
 package org.apache.axiom.om.util;
 
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.Writer;
+import org.apache.axiom.util.base64.Base64Utils;
 
-public class Base64 {
-    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', '+', '/' };
+public class Base64 extends Base64Utils {
 
-    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 decode0(char[] ibuf, byte[] obuf, int wp) {
-        int outlen = 3;
-        if (ibuf[3] == S_BASE64PAD)
-            outlen = 2;
-        if (ibuf[2] == 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]];
-        switch (outlen) {
-            case 1:
-                obuf[wp] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
-                return 1;
-            case 2:
-                obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
-                obuf[wp] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
-                return 2;
-            case 3:
-                obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
-                obuf[wp++] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
-                obuf[wp] = (byte) (b2 << 6 & 0xc0 | b3 & 0x3f);
-                return 3;
-            default:
-                throw new RuntimeException("internalError00");
-        }
-    }
-
-    /**
-     *
-     */
-    public static byte[] decode(char[] data, int off, int len) {
-        char[] ibuf = new char[4];
-        int ibufcount = 0;
-        byte[] obuf = new byte[len / 4 * 3 + 3];
-        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) {
-                ibuf[ibufcount++] = ch;
-                if (ibufcount == ibuf.length) {
-                    ibufcount = 0;
-                    obufcount += decode0(ibuf, obuf, obufcount);
-                }
-            }
-        }
-        if (obufcount == obuf.length)
-            return obuf;
-        byte[] ret = new byte[obufcount];
-        System.arraycopy(obuf, 0, ret, 0, obufcount);
-        return ret;
-    }
-
-    /**
-     *
-     */
-    public static byte[] decode(String data) {
-        char[] ibuf = new char[4];
-        int ibufcount = 0;
-        byte[] obuf = new byte[data.length() / 4 * 3 + 3];
-        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) {
-                ibuf[ibufcount++] = ch;
-                if (ibufcount == ibuf.length) {
-                    ibufcount = 0;
-                    obufcount += decode0(ibuf, obuf, obufcount);
-                }
-            }
-        }
-        if (obufcount == obuf.length)
-            return obuf;
-        byte[] ret = new byte[obufcount];
-        System.arraycopy(obuf, 0, ret, 0, obufcount);
-        return ret;
-    }
-
-    /**
-     * checks input string for invalid Base64 characters
-     *
-     * @param data
-     * @return true, if String contains only valid Base64 characters. false, 
otherwise
-     */
-    public static boolean isValidBase64Encoding(String data) {
-        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) {
-                //valid character.Do nothing
-            } else if (ch == '\r' || ch == '\n') {
-                //do nothing
-            } else {
-                return false;
-            }
-        }//iterate over all characters in the string
-        return true;
-    }
-
-
-    /**
-     *
-     */
-    public static void decode(char[] data, int off, int len,
-                              OutputStream ostream) throws IOException {
-        char[] ibuf = new char[4];
-        int ibufcount = 0;
-        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) {
-                ibuf[ibufcount++] = ch;
-                if (ibufcount == ibuf.length) {
-                    ibufcount = 0;
-                    int obufcount = decode0(ibuf, obuf, 0);
-                    ostream.write(obuf, 0, obufcount);
-                }
-            }
-        }
-    }
-
-    /**
-     *
-     */
-    public static void decode(String data, OutputStream ostream)
-            throws IOException {
-        char[] ibuf = new char[4];
-        int ibufcount = 0;
-        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) {
-                ibuf[ibufcount++] = ch;
-                if (ibufcount == ibuf.length) {
-                    ibufcount = 0;
-                    int obufcount = decode0(ibuf, obuf, 0);
-                    ostream.write(obuf, 0, obufcount);
-                }
-            }
-        }
-    }
-
-    /** Returns base64 representation of specified byte array. */
-    public static String encode(byte[] data) {
-        return encode(data, 0, data.length);
-    }
-
-    /** Returns base64 representation of specified byte array. */
-    public static String encode(byte[] data, int off, int len) {
-        if (len <= 0)
-            return "";
-        char[] out = new char[len / 3 * 4 + 4];
-        int rindex = off;
-        int windex = 0;
-        int rest = len - off;
-        while (rest >= 3) {
-            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];
-            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;
-        } 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;
-        }
-        return new String(out, 0, windex);
-    }
-
-    /** Outputs base64 representation of the specified byte array to the 
specified String Buffer */
-    public static void encode(byte[] data, int off, int len, StringBuffer 
buffer) {
-        if (len <= 0) {
-            return;
-        }
-
-        char[] out = new char[4];
-        int rindex = off;
-        int rest = len - off;
-        while (rest >= 3) {
-            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];
-            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;
-            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;
-            buffer.append(out);
-        }
-    }
-
-    /** Outputs base64 representation of the specified byte array to a byte 
stream. */
-    public static void encode(byte[] data, int off, int len,
-                              OutputStream ostream) throws IOException {
-        if (len <= 0)
-            return;
-        byte[] out = new byte[4];
-        int rindex = off;
-        int rest = len - off;
-        while (rest >= 3) {
-            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];
-            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;
-            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;
-            ostream.write(out, 0, 4);
-        }
-    }
-
-    /** Outputs base64 representation of the specified byte array to a 
character stream. */
-    public static void encode(byte[] data, int off, int len, Writer writer)
-            throws IOException {
-        if (len <= 0)
-            return;
-        char[] out = new char[4];
-        int rindex = off;
-        int rest = len - off;
-        int output = 0;
-        while (rest >= 3) {
-            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];
-            writer.write(out, 0, 4);
-            rindex += 3;
-            rest -= 3;
-            output += 4;
-            if (output % 76 == 0)
-                writer.write("\n");
-        }
-        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;
-            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;
-            writer.write(out, 0, 4);
-        }
-    }
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java?rev=944423&r1=944422&r2=944423&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
 Fri May 14 19:43:02 2010
@@ -19,20 +19,21 @@
 
 package org.apache.axiom.om.util;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+
 import org.apache.axiom.attachments.impl.BufferUtils;
 import org.apache.axiom.attachments.lifecycle.LifecycleManager;
 import org.apache.axiom.attachments.lifecycle.impl.FileAccessor;
 import org.apache.axiom.attachments.lifecycle.impl.LifecycleManagerImpl;
 import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMText;
-
-import javax.activation.DataHandler;
-import javax.mail.MessagingException;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import org.apache.axiom.util.base64.Base64Utils;
 
 public class TextHelper {
     
@@ -84,7 +85,7 @@ public class TextHelper {
                 }
                 len += read;
             } while (len < data.length);
-            Base64.encode(data, 0, len, buffer);
+            Base64Utils.encode(data, 0, len, buffer);
         } while (!eos);
     }
     
@@ -169,7 +170,7 @@ public class TextHelper {
             }
         }
         if (omText == null) {
-            omText = factory.createOMText(Base64.encode(b, off, length));
+            omText = factory.createOMText(Base64Utils.encode(b, off, length));
             omText.setOptimize(isOptimize);
         }
         return omText;

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java?rev=944423&r1=944422&r2=944423&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java
 Fri May 14 19:43:02 2010
@@ -20,6 +20,8 @@
 package org.apache.axiom.util.base64;
 
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Writer;
 
 import javax.activation.DataHandler;
 
@@ -29,6 +31,25 @@ 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;
     }
@@ -59,4 +80,307 @@ public class Base64Utils {
         out.complete();
         return buffer.toString();
     }
+
+    private static int decode0(char[] ibuf, byte[] obuf, int wp) {
+        int outlen = 3;
+        if (ibuf[3] == S_BASE64PAD)
+            outlen = 2;
+        if (ibuf[2] == 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]];
+        switch (outlen) {
+            case 1:
+                obuf[wp] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
+                return 1;
+            case 2:
+                obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
+                obuf[wp] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
+                return 2;
+            case 3:
+                obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
+                obuf[wp++] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
+                obuf[wp] = (byte) (b2 << 6 & 0xc0 | b3 & 0x3f);
+                return 3;
+            default:
+                throw new RuntimeException("internalError00");
+        }
+    }
+
+    /**
+     *
+     */
+    public static byte[] decode(char[] data, int off, int len) {
+        char[] ibuf = new char[4];
+        int ibufcount = 0;
+        byte[] obuf = new byte[len / 4 * 3 + 3];
+        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) {
+                ibuf[ibufcount++] = ch;
+                if (ibufcount == ibuf.length) {
+                    ibufcount = 0;
+                    obufcount += decode0(ibuf, obuf, obufcount);
+                }
+            }
+        }
+        if (obufcount == obuf.length)
+            return obuf;
+        byte[] ret = new byte[obufcount];
+        System.arraycopy(obuf, 0, ret, 0, obufcount);
+        return ret;
+    }
+
+    /**
+     *
+     */
+    public static byte[] decode(String data) {
+        char[] ibuf = new char[4];
+        int ibufcount = 0;
+        byte[] obuf = new byte[data.length() / 4 * 3 + 3];
+        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) {
+                ibuf[ibufcount++] = ch;
+                if (ibufcount == ibuf.length) {
+                    ibufcount = 0;
+                    obufcount += decode0(ibuf, obuf, obufcount);
+                }
+            }
+        }
+        if (obufcount == obuf.length)
+            return obuf;
+        byte[] ret = new byte[obufcount];
+        System.arraycopy(obuf, 0, ret, 0, obufcount);
+        return ret;
+    }
+
+    /**
+     * checks input string for invalid Base64 characters
+     *
+     * @param data
+     * @return true, if String contains only valid Base64 characters. false, 
otherwise
+     */
+    public static boolean isValidBase64Encoding(String data) {
+        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) {
+                //valid character.Do nothing
+            } else if (ch == '\r' || ch == '\n') {
+                //do nothing
+            } else {
+                return false;
+            }
+        }//iterate over all characters in the string
+        return true;
+    }
+
+
+    /**
+     *
+     */
+    public static void decode(char[] data, int off, int len,
+                              OutputStream ostream) throws IOException {
+        char[] ibuf = new char[4];
+        int ibufcount = 0;
+        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) {
+                ibuf[ibufcount++] = ch;
+                if (ibufcount == ibuf.length) {
+                    ibufcount = 0;
+                    int obufcount = decode0(ibuf, obuf, 0);
+                    ostream.write(obuf, 0, obufcount);
+                }
+            }
+        }
+    }
+
+    /**
+     *
+     */
+    public static void decode(String data, OutputStream ostream)
+            throws IOException {
+        char[] ibuf = new char[4];
+        int ibufcount = 0;
+        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) {
+                ibuf[ibufcount++] = ch;
+                if (ibufcount == ibuf.length) {
+                    ibufcount = 0;
+                    int obufcount = decode0(ibuf, obuf, 0);
+                    ostream.write(obuf, 0, obufcount);
+                }
+            }
+        }
+    }
+
+    /** Returns base64 representation of specified byte array. */
+    public static String encode(byte[] data) {
+        return encode(data, 0, data.length);
+    }
+
+    /** Returns base64 representation of specified byte array. */
+    public static String encode(byte[] data, int off, int len) {
+        if (len <= 0)
+            return "";
+        char[] out = new char[len / 3 * 4 + 4];
+        int rindex = off;
+        int windex = 0;
+        int rest = len - off;
+        while (rest >= 3) {
+            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];
+            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;
+        } 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;
+        }
+        return new String(out, 0, windex);
+    }
+
+    /** Outputs base64 representation of the specified byte array to the 
specified String Buffer */
+    public static void encode(byte[] data, int off, int len, StringBuffer 
buffer) {
+        if (len <= 0) {
+            return;
+        }
+
+        char[] out = new char[4];
+        int rindex = off;
+        int rest = len - off;
+        while (rest >= 3) {
+            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];
+            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;
+            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;
+            buffer.append(out);
+        }
+    }
+
+    /** Outputs base64 representation of the specified byte array to a byte 
stream. */
+    public static void encode(byte[] data, int off, int len,
+                              OutputStream ostream) throws IOException {
+        if (len <= 0)
+            return;
+        byte[] out = new byte[4];
+        int rindex = off;
+        int rest = len - off;
+        while (rest >= 3) {
+            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];
+            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;
+            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;
+            ostream.write(out, 0, 4);
+        }
+    }
+
+    /** Outputs base64 representation of the specified byte array to a 
character stream. */
+    public static void encode(byte[] data, int off, int len, Writer writer)
+            throws IOException {
+        if (len <= 0)
+            return;
+        char[] out = new char[4];
+        int rindex = off;
+        int rest = len - off;
+        int output = 0;
+        while (rest >= 3) {
+            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];
+            writer.write(out, 0, 4);
+            rindex += 3;
+            rest -= 3;
+            output += 4;
+            if (output % 76 == 0)
+                writer.write("\n");
+        }
+        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;
+            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;
+            writer.write(out, 0, 4);
+        }
+    }
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java?rev=944423&r1=944422&r2=944423&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
 Fri May 14 19:43:02 2010
@@ -27,7 +27,7 @@ import javax.xml.stream.XMLStreamReader;
 import org.apache.axiom.attachments.ByteArrayDataSource;
 import org.apache.axiom.ext.stax.datahandler.DataHandlerReader;
 import org.apache.axiom.om.OMAttachmentAccessor;
-import org.apache.axiom.om.util.Base64;
+import org.apache.axiom.util.base64.Base64Utils;
 import org.apache.axiom.util.stax.wrapper.XMLStreamReaderContainer;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -113,7 +113,7 @@ public class XMLStreamReaderUtils {
                 base64 = buff.toString();
             }
         }
-        return new DataHandler(new ByteArrayDataSource(Base64.decode(base64)));
+        return new DataHandler(new 
ByteArrayDataSource(Base64Utils.decode(base64)));
     }
     
     /**

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/Base64Test.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/Base64Test.java?rev=944423&r1=944422&r2=944423&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/Base64Test.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/Base64Test.java
 Fri May 14 19:43:02 2010
@@ -19,13 +19,15 @@
 
 package org.apache.axiom.om.util;
 
-import junit.framework.TestCase;
-
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 
+import junit.framework.TestCase;
+
+import org.apache.axiom.util.base64.Base64Utils;
+
 public class Base64Test extends TestCase {
 
     Object expectedObject;
@@ -43,8 +45,8 @@ public class Base64Test extends TestCase
         ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
         ObjectOutputStream objectOutStream = new 
ObjectOutputStream(byteStream);
         objectOutStream.writeObject(expectedObject);
-        expectedBase64 = Base64.encode(byteStream.toByteArray());
-        byte[] tempa = Base64.decode(expectedBase64);
+        expectedBase64 = Base64Utils.encode(byteStream.toByteArray());
+        byte[] tempa = Base64Utils.decode(expectedBase64);
         ObjectInputStream objectInStream = new ObjectInputStream(
                 new ByteArrayInputStream(tempa));
         actualObject = objectInStream.readObject();

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReaderTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReaderTest.java?rev=944423&r1=944422&r2=944423&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReaderTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReaderTest.java
 Fri May 14 19:43:02 2010
@@ -28,8 +28,8 @@ import org.apache.axiom.attachments.Atta
 import org.apache.axiom.om.AbstractTestCase;
 import org.apache.axiom.om.TestConstants;
 import org.apache.axiom.om.impl.builder.OMAttachmentAccessorMimePartProvider;
-import org.apache.axiom.om.util.Base64;
 import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.util.base64.Base64Utils;
 import org.apache.axiom.util.stax.XMLStreamReaderComparator;
 
 public class XOPDecodingStreamReaderTest extends AbstractTestCase {
@@ -58,7 +58,7 @@ public class XOPDecodingStreamReaderTest
             reader.next();
         }
         String base64 = reader.getElementText();
-        byte[] data = Base64.decode(base64);
+        byte[] data = Base64Utils.decode(base64);
         // The data is actually a JPEG image. Try to decode it to check that 
the data is not
         // corrupted.
         ImageIO.read(new ByteArrayInputStream(data));


Reply via email to