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