/*
 * Copyright 2012 Splunk, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"): you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

/* adapted from wikipedia and wikihow */

package com.splunk;

public class Base64 {

    private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/";

    public static byte[] zeroPad(int length, byte[] bytes) {
        byte[] padded = new byte[length];
        System.arraycopy(bytes, 0, padded, 0, bytes.length);
        return padded;
    }

    public static String encode(String string) {
        String encoded = "";
        byte[] stringArray;
        try {
            stringArray = string.getBytes("UTF-8");
        } catch (Exception ignored) {
            stringArray = string.getBytes();
        }
        int paddingCount = (3 - (stringArray.length % 3)) % 3;
        stringArray = zeroPad(stringArray.length + paddingCount, stringArray);
        for (int i = 0; i < stringArray.length; i += 3) {
            int j = ((stringArray[i] & 0xff) << 16) +
                    ((stringArray[i + 1] & 0xff) << 8) +
                    (stringArray[i + 2] & 0xff);
            encoded = encoded + base64code.charAt((j >> 18) & 0x3f) +
                    base64code.charAt((j >> 12) & 0x3f) +
                    base64code.charAt((j >> 6) & 0x3f) +
                    base64code.charAt(j & 0x3f);
        }
        return encoded;
    }
}
@@ -594,68 +594,72 @@
 */%0A%0A
-/* adapted from wikipedia and wikihow */%0A%0Apackage com.splunk
+package com.splunk;%0A%0Aimport java.io.UnsupportedEncodingException
 ;%0A%0Ap
@@ -724,16 +724,28 @@
 64code =
+%0A           
  %22ABCDEF
@@ -768,25 +768,8 @@
 WXYZ
-%22%0A            + %22
 abcd
@@ -790,21 +790,16 @@
 stuvwxyz
-%22 + %22
 01234567
@@ -804,13 +804,8 @@
 6789
-%22 + %22
 +/%22;
@@ -1093,27 +1093,30 @@
  byte%5B%5D 
-stringArray
+byteArray = %7B%7D
 ;%0A      
@@ -1127,38 +1127,36 @@
 y %7B%0A            
-string
+byte
 Array = string.g
@@ -1194,84 +1194,59 @@
 ch (
-Exception ignored) %7B%0A            stringArray = string.getBytes();%0A       
+UnsupportedEncodingException e) %7B assert false;
  %7D%0A
+%0A
     
@@ -1274,22 +1274,20 @@
 = (3 - (
-string
+byte
 Array.le
@@ -1310,22 +1310,20 @@
         
-string
+byte
 Array = 
@@ -1330,22 +1330,20 @@
 zeroPad(
-string
+byte
 Array.le
@@ -1363,22 +1363,20 @@
 gCount, 
-string
+byte
 Array);%0A
@@ -1403,22 +1403,20 @@
  0; i %3C 
-string
+byte
 Array.le
@@ -1453,22 +1453,20 @@
 t j = ((
-string
+byte
 Array%5Bi%5D
@@ -1505,22 +1505,20 @@
       ((
-string
+byte
 Array%5Bi 
@@ -1559,22 +1559,20 @@
        (
-string
+byte
 Array%5Bi 
@@ -1616,16 +1616,36 @@
 ncoded +
+%0A                   
  base64c

tweak base 64