Guest User

Untitled

a guest
May 22nd, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. package femmod;
  2.  
  3. public class Base64Coder {
  4.  
  5.     // Mapping table from 6-bit nibbles to Base64 characters.
  6.     private static char[] map1 = new char[64];
  7.        static {
  8.           int i=0;
  9.           for (char c='A'; c<='Z'; c++) map1[i++] = c;
  10.           for (char c='a'; c<='z'; c++) map1[i++] = c;
  11.           for (char c='0'; c<='9'; c++) map1[i++] = c;
  12.           map1[i++] = '+';
  13.           //noinspection UnusedAssignment
  14.           map1[i++] = '/';
  15.        }
  16.  
  17.     // Mapping table from Base64 characters to 6-bit nibbles.
  18.     private static byte[]    map2 = new byte[128];
  19.        static {
  20.           for (int i=0; i<map2.length; i++) map2[i] = -1;
  21.           for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
  22.  
  23.     /**
  24.     * Encodes a string into Base64 format.
  25.     * No blanks or line breaks are inserted.
  26.     * @param s  a String to be encoded.
  27.     * @return   A String with the Base64 encoded data.
  28.     */
  29.     public static String encodeString (String s) {
  30.        return new String(encode(s.getBytes())); }
  31.  
  32.     /**
  33.     * Encodes a byte array into Base64 format.
  34.     * No blanks or line breaks are inserted.
  35.     * @param in  an array containing the data bytes to be encoded.
  36.     * @return    A character array with the Base64 encoded data.
  37.     */
  38.     public static char[] encode (byte[] in) {
  39.        return encode(in,in.length); }
  40.  
  41.     /**
  42.     * Encodes a byte array into Base64 format.
  43.     * No blanks or line breaks are inserted.
  44.     * @param in   an array containing the data bytes to be encoded.
  45.     * @param iLen number of bytes to process in <code>in</code>.
  46.     * @return     A character array with the Base64 encoded data.
  47.     */
  48.     public static char[] encode (byte[] in, int iLen) {
  49.        int oDataLen = (iLen*4+2)/3;       // output length without padding
  50.        int oLen = ((iLen+2)/3)*4;         // output length including padding
  51.        char[] out = new char[oLen];
  52.        int ip = 0;
  53.        int op = 0;
  54.        while (ip < iLen) {
  55.           int i0 = in[ip++] & 0xff;
  56.           int i1 = ip < iLen ? in[ip++] & 0xff : 0;
  57.           int i2 = ip < iLen ? in[ip++] & 0xff : 0;
  58.           int o0 = i0 >>> 2;
  59.           int o1 = ((i0 &   3) << 4) | (i1 >>> 4);
  60.           int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
  61.           int o3 = i2 & 0x3F;
  62.           out[op++] = map1[o0];
  63.           out[op++] = map1[o1];
  64.           out[op] = op < oDataLen ? map1[o2] : '='; op++;
  65.           out[op] = op < oDataLen ? map1[o3] : '='; op++; }
  66.        return out; }
  67.  
  68.     /**
  69.     * Decodes a string from Base64 format.
  70.     * @param s  a Base64 String to be decoded.
  71.     * @return   A String containing the decoded data.
  72.     * @throws   IllegalArgumentException if the input is not valid Base64 encoded data.
  73.     */
  74.     public static String decodeString (String s) {
  75.        return new String(decode(s)); }
  76.  
  77.     /**
  78.     * Decodes a byte array from Base64 format.
  79.     * @param s  a Base64 String to be decoded.
  80.     * @return   An array containing the decoded data bytes.
  81.     * @throws   IllegalArgumentException if the input is not valid Base64 encoded data.
  82.     */
  83.     public static byte[] decode (String s) {
  84.        return decode(s.toCharArray()); }
  85.  
  86.     /**
  87.     * Decodes a byte array from Base64 format.
  88.     * No blanks or line breaks are allowed within the Base64 encoded data.
  89.     * @param in  a character array containing the Base64 encoded data.
  90.     * @return    An array containing the decoded data bytes.
  91.     * @throws    IllegalArgumentException if the input is not valid Base64 encoded data.
  92.     */
  93.     public static byte[] decode (char[] in) {
  94.        int iLen = in.length;
  95.        if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
  96.        while (iLen > 0 && in[iLen-1] == '=') iLen--;
  97.        int oLen = (iLen*3) / 4;
  98.        byte[] out = new byte[oLen];
  99.        int ip = 0;
  100.        int op = 0;
  101.        while (ip < iLen) {
  102.           int i0 = in[ip++];
  103.           int i1 = in[ip++];
  104.           int i2 = ip < iLen ? in[ip++] : 'A';
  105.           int i3 = ip < iLen ? in[ip++] : 'A';
  106.           if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
  107.              throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
  108.           int b0 = map2[i0];
  109.           int b1 = map2[i1];
  110.           int b2 = map2[i2];
  111.           int b3 = map2[i3];
  112.           if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
  113.              throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
  114.           int o0 = ( b0       <<2) | (b1>>>4);
  115.           int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
  116.           int o2 = ((b2 &   3)<<6) |  b3;
  117.           out[op++] = (byte)o0;
  118.           if (op<oLen) out[op++] = (byte)o1;
  119.           if (op<oLen) out[op++] = (byte)o2; }
  120.        return out; }
  121.  
  122.  
  123.  
  124.     } // end class Base64Coder
Advertisement
Add Comment
Please, Sign In to add comment