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