extensions/net.sf.basedb.labenv/trunk/src/net/sf/basedb/labenv/converter/Base64Coder.java

Code
Comments
Other
Rev Date Author Line
2303 02 Apr 14 olle 1 /**************************************************************************
2303 02 Apr 14 olle 2 *
2303 02 Apr 14 olle 3 * A Base64 Encoder/Decoder.
2303 02 Apr 14 olle 4 *
2303 02 Apr 14 olle 5 * This class is used to encode and decode data in Base64 format
2303 02 Apr 14 olle 6 * as described in RFC 1521.
2303 02 Apr 14 olle 7 *
2303 02 Apr 14 olle 8 * <p>
2303 02 Apr 14 olle 9 * Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland.<br>
2303 02 Apr 14 olle 10 * License: This is "Open Source" software and released under the <a href="http://www.gnu.org/licenses/gpl.html" target="_top">GNU/GPL</a> license.
2303 02 Apr 14 olle 11 * It is provided "as is" without warranty of any kind. Please contact the author for other licensing arrangements.<br>
2303 02 Apr 14 olle 12 * Home page: <a href="http://www.source-code.biz" target="_top">www.source-code.biz</a><br>
2303 02 Apr 14 olle 13 *
2303 02 Apr 14 olle 14 * <p>
2303 02 Apr 14 olle 15 * Version history:<br>
2303 02 Apr 14 olle 16 * 2003-07-22 Christian d'Heureuse (chdh): Module created.
2303 02 Apr 14 olle 17 *
2303 02 Apr 14 olle 18 **************************************************************************/
2303 02 Apr 14 olle 19
2303 02 Apr 14 olle 20 package net.sf.basedb.labenv.converter;
2303 02 Apr 14 olle 21
2303 02 Apr 14 olle 22 public class Base64Coder {
2303 02 Apr 14 olle 23
2303 02 Apr 14 olle 24 // Mapping table from 6-bit nibbles to Base64 characters.
2303 02 Apr 14 olle 25 private static char[]    map1 = new char[64];
2303 02 Apr 14 olle 26    static {
2303 02 Apr 14 olle 27       int i=0;
2303 02 Apr 14 olle 28       for (char c='A'; c<='Z'; c++) map1[i++] = c;
2303 02 Apr 14 olle 29       for (char c='a'; c<='z'; c++) map1[i++] = c;
2303 02 Apr 14 olle 30       for (char c='0'; c<='9'; c++) map1[i++] = c;
2303 02 Apr 14 olle 31       map1[i++] = '+'; map1[i++] = '/'; }
2303 02 Apr 14 olle 32
2303 02 Apr 14 olle 33 // Mapping table from Base64 characters to 6-bit nibbles.
2303 02 Apr 14 olle 34 private static byte[]    map2 = new byte[128];
2303 02 Apr 14 olle 35    static {
2303 02 Apr 14 olle 36       for (int i=0; i<map2.length; i++) map2[i] = -1;
2303 02 Apr 14 olle 37       for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
2303 02 Apr 14 olle 38
2303 02 Apr 14 olle 39 /**
2303 02 Apr 14 olle 40 * Encodes a string into Base64 format.
2303 02 Apr 14 olle 41 * No blanks or line breaks are inserted.
2303 02 Apr 14 olle 42 * @param s  a String to be encoded.
2303 02 Apr 14 olle 43 * @return   A String with the Base64 encoded data.
2303 02 Apr 14 olle 44 */
2303 02 Apr 14 olle 45 public static String encode (String s) {
2303 02 Apr 14 olle 46    return new String(encode(s.getBytes())); }
2303 02 Apr 14 olle 47
2303 02 Apr 14 olle 48 /**
2303 02 Apr 14 olle 49 * Encodes a byte array into Base64 format.
2303 02 Apr 14 olle 50 * No blanks or line breaks are inserted.
2303 02 Apr 14 olle 51 * @param in  an array containing the data bytes to be encoded.
2303 02 Apr 14 olle 52 * @return    A character array with the Base64 encoded data.
2303 02 Apr 14 olle 53 */
2303 02 Apr 14 olle 54 public static char[] encode (byte[] in) {
2303 02 Apr 14 olle 55    int iLen = in.length;
2303 02 Apr 14 olle 56    int oDataLen = (iLen*4+2)/3;       // output length without padding
2303 02 Apr 14 olle 57    int oLen = ((iLen+2)/3)*4;         // output length including padding
2303 02 Apr 14 olle 58    char[] out = new char[oLen];
2303 02 Apr 14 olle 59    int ip = 0;
2303 02 Apr 14 olle 60    int op = 0;
2303 02 Apr 14 olle 61    while (ip < iLen) {
2303 02 Apr 14 olle 62       int i0 = in[ip++] & 0xff;
2303 02 Apr 14 olle 63       int i1 = ip < iLen ? in[ip++] & 0xff : 0;
2303 02 Apr 14 olle 64       int i2 = ip < iLen ? in[ip++] & 0xff : 0;
2303 02 Apr 14 olle 65       int o0 = i0 >>> 2;
2303 02 Apr 14 olle 66       int o1 = ((i0 &   3) << 4) | (i1 >>> 4);
2303 02 Apr 14 olle 67       int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
2303 02 Apr 14 olle 68       int o3 = i2 & 0x3F;
2303 02 Apr 14 olle 69       out[op++] = map1[o0];
2303 02 Apr 14 olle 70       out[op++] = map1[o1];
2303 02 Apr 14 olle 71       out[op] = op < oDataLen ? map1[o2] : '='; op++;
2303 02 Apr 14 olle 72       out[op] = op < oDataLen ? map1[o3] : '='; op++; }
2303 02 Apr 14 olle 73    return out; }
2303 02 Apr 14 olle 74
2303 02 Apr 14 olle 75 /**
2303 02 Apr 14 olle 76 * Decodes a Base64 string.
2303 02 Apr 14 olle 77 * @param s  a Base64 String to be decoded.
2303 02 Apr 14 olle 78 * @return   A String containing the decoded data.
2303 02 Apr 14 olle 79 * @throws   IllegalArgumentException if the input is not valid Base64 encoded data.
2303 02 Apr 14 olle 80 */
2303 02 Apr 14 olle 81 public static String decode (String s) {
2303 02 Apr 14 olle 82    return new String(decode(s.toCharArray())); }
2303 02 Apr 14 olle 83
2303 02 Apr 14 olle 84 /**
2303 02 Apr 14 olle 85 * Decodes Base64 data.
2303 02 Apr 14 olle 86 * No blanks or line breaks are allowed within the Base64 encoded data.
2303 02 Apr 14 olle 87 * @param in  a character array containing the Base64 encoded data.
2303 02 Apr 14 olle 88 * @return    An array containing the decoded data bytes.
2303 02 Apr 14 olle 89 * @throws    IllegalArgumentException if the input is not valid Base64 encoded data.
2303 02 Apr 14 olle 90 */
2303 02 Apr 14 olle 91 public static byte[] decode (char[] in) {
2303 02 Apr 14 olle 92    int iLen = in.length;
2303 02 Apr 14 olle 93    if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
2303 02 Apr 14 olle 94    while (iLen > 0 && in[iLen-1] == '=') iLen--;
2303 02 Apr 14 olle 95    int oLen = (iLen*3) / 4;
2303 02 Apr 14 olle 96    byte[] out = new byte[oLen];
2303 02 Apr 14 olle 97    int ip = 0;
2303 02 Apr 14 olle 98    int op = 0;
2303 02 Apr 14 olle 99    while (ip < iLen) {
2303 02 Apr 14 olle 100       int i0 = in[ip++];
2303 02 Apr 14 olle 101       int i1 = in[ip++];
2303 02 Apr 14 olle 102       int i2 = ip < iLen ? in[ip++] : 'A';
2303 02 Apr 14 olle 103       int i3 = ip < iLen ? in[ip++] : 'A';
2303 02 Apr 14 olle 104       if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
2303 02 Apr 14 olle 105          throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
2303 02 Apr 14 olle 106       int b0 = map2[i0];
2303 02 Apr 14 olle 107       int b1 = map2[i1];
2303 02 Apr 14 olle 108       int b2 = map2[i2];
2303 02 Apr 14 olle 109       int b3 = map2[i3];
2303 02 Apr 14 olle 110       if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
2303 02 Apr 14 olle 111          throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
2303 02 Apr 14 olle 112       int o0 = ( b0       <<2) | (b1>>>4);
2303 02 Apr 14 olle 113       int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
2303 02 Apr 14 olle 114       int o2 = ((b2 &   3)<<6) |  b3;
2303 02 Apr 14 olle 115       out[op++] = (byte)o0;
2303 02 Apr 14 olle 116       if (op<oLen) out[op++] = (byte)o1;
2303 02 Apr 14 olle 117       if (op<oLen) out[op++] = (byte)o2; }
2303 02 Apr 14 olle 118    return out; }
2303 02 Apr 14 olle 119
2303 02 Apr 14 olle 120 }