1
14
15 package com.liferay.portal.kernel.util;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25
26
31 public class Base64 {
32
33 protected static char getChar(int sixbit) {
34 if (sixbit >= 0 && sixbit <= 25) {
35 return (char)(65 + sixbit);
36 }
37
38 if (sixbit >= 26 && sixbit <= 51) {
39 return (char)(97 + (sixbit - 26));
40 }
41
42 if (sixbit >= 52 && sixbit <= 61) {
43 return (char)(48 + (sixbit - 52));
44 }
45
46 if (sixbit == 62) {
47 return CharPool.PLUS;
48 }
49
50 return sixbit != 63 ? CharPool.QUESTION : CharPool.SLASH;
51 }
52
53 protected static int getValue(char c) {
54 if ((c >= CharPool.UPPER_CASE_A) && (c <= CharPool.UPPER_CASE_Z)) {
55 return c - 65;
56 }
57
58 if ((c >= CharPool.LOWER_CASE_A) && (c <= CharPool.LOWER_CASE_Z)) {
59 return (c - 97) + 26;
60 }
61
62 if (c >= CharPool.NUMBER_0 && c <= CharPool.NUMBER_9) {
63 return (c - 48) + 52;
64 }
65
66 if (c == CharPool.PLUS) {
67 return 62;
68 }
69
70 if (c == CharPool.SLASH) {
71 return 63;
72 }
73
74 return c != CharPool.EQUAL ? -1 : 0;
75 }
76
77 public static String encode(byte raw[]) {
78 return encode(raw, 0, raw.length);
79 }
80
81 public static String encode(byte raw[], int offset, int length) {
82 int lastIndex = Math.min(raw.length, offset + length);
83
84 StringBuilder sb = new StringBuilder(
85 ((lastIndex - offset) / 3 + 1) * 4);
86
87 for (int i = offset; i < lastIndex; i += 3) {
88 sb.append(encodeBlock(raw, i, lastIndex));
89 }
90
91 return sb.toString();
92 }
93
94 protected static char[] encodeBlock(byte raw[], int offset, int lastIndex) {
95 int block = 0;
96 int slack = lastIndex - offset - 1;
97 int end = slack < 2 ? slack : 2;
98
99 for (int i = 0; i <= end; i++) {
100 byte b = raw[offset + i];
101
102 int neuter = b >= 0 ? ((int) (b)) : b + 256;
103 block += neuter << 8 * (2 - i);
104 }
105
106 char base64[] = new char[4];
107
108 for (int i = 0; i < 4; i++) {
109 int sixbit = block >>> 6 * (3 - i) & 0x3f;
110 base64[i] = getChar(sixbit);
111 }
112
113 if (slack < 1) {
114 base64[2] = CharPool.EQUAL;
115 }
116
117 if (slack < 2) {
118 base64[3] = CharPool.EQUAL;
119 }
120
121 return base64;
122 }
123
124 public static byte[] decode(String base64) {
125 if (Validator.isNull(base64)) {
126 return new byte[0];
127 }
128
129 int pad = 0;
130
131 for (int i = base64.length() - 1; base64.charAt(i) == CharPool.EQUAL;
132 i--) {
133
134 pad++;
135 }
136
137 int length = (base64.length() * 6) / 8 - pad;
138 byte raw[] = new byte[length];
139 int rawindex = 0;
140
141 for (int i = 0; i < base64.length(); i += 4) {
142 int block = (getValue(base64.charAt(i)) << 18) +
143 (getValue(base64.charAt(i + 1)) << 12) +
144 (getValue(base64.charAt(i + 2)) << 6) +
145 getValue(base64.charAt(i + 3));
146
147 for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
148 raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
149 }
150
151 rawindex += 3;
152 }
153
154 return raw;
155 }
156
157 public static String objectToString(Object o) {
158 if (o == null) {
159 return null;
160 }
161
162 UnsyncByteArrayOutputStream ubaos = new UnsyncByteArrayOutputStream(
163 32000);
164
165 try {
166 ObjectOutputStream os = new ObjectOutputStream(ubaos);
167
168 os.flush();
169 os.writeObject(o);
170 os.flush();
171 }
172 catch (IOException e) {
173 _log.error(e, e);
174 }
175
176 return encode(ubaos.unsafeGetByteArray(), 0, ubaos.size());
177 }
178
179 public static Object stringToObject(String s) {
180 return stringToObject(s, null);
181 }
182
183 public static Object stringToObject(String s, ClassLoader classLoader) {
184 if (s == null) {
185 return null;
186 }
187
188 byte bytes[] = decode(s);
189
190 UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
191 bytes);
192
193 try {
194 ObjectInputStream is = null;
195
196 if (classLoader == null) {
197 is = new ObjectInputStream(ubais);
198 }
199 else {
200 is = new ClassLoaderObjectInputStream(ubais, classLoader);
201 }
202
203 return is.readObject();
204 }
205 catch (Exception e) {
206 _log.error(e, e);
207 }
208
209 return null;
210 }
211
212 private static Log _log = LogFactoryUtil.getLog(Base64.class);
213
214 }