001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021
022 import java.io.ObjectInputStream;
023 import java.io.ObjectOutputStream;
024
025
028 public class Base64 {
029
030 public static byte[] decode(String base64) {
031 if (Validator.isNull(base64)) {
032 return new byte[0];
033 }
034
035 int pad = 0;
036
037 for (int i = base64.length() - 1; base64.charAt(i) == CharPool.EQUAL;
038 i--) {
039
040 pad++;
041 }
042
043 int length = (base64.length() * 6) / 8 - pad;
044 byte raw[] = new byte[length];
045 int rawindex = 0;
046
047 for (int i = 0; i < base64.length(); i += 4) {
048 int block = getValue(base64.charAt(i)) << 18;
049
050 block += getValue(base64.charAt(i + 1)) << 12;
051 block += getValue(base64.charAt(i + 2)) << 6;
052 block += getValue(base64.charAt(i + 3));
053
054 for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
055 raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
056 }
057
058 rawindex += 3;
059 }
060
061 return raw;
062 }
063
064 public static String encode(byte[] raw) {
065 return encode(raw, 0, raw.length);
066 }
067
068 public static String encode(byte[] raw, int offset, int length) {
069 int lastIndex = Math.min(raw.length, offset + length);
070
071 StringBuilder sb = new StringBuilder(
072 ((lastIndex - offset) / 3 + 1) * 4);
073
074 for (int i = offset; i < lastIndex; i += 3) {
075 sb.append(encodeBlock(raw, i, lastIndex));
076 }
077
078 return sb.toString();
079 }
080
081 public static String fromURLSafe(String base64) {
082 return StringUtil.replace(
083 base64,
084 new String[] {
085 StringPool.MINUS, StringPool.STAR, StringPool.UNDERLINE
086 },
087 new String[] {
088 StringPool.PLUS, StringPool.EQUAL, StringPool.SLASH
089 });
090 }
091
092 public static String objectToString(Object o) {
093 if (o == null) {
094 return null;
095 }
096
097 UnsyncByteArrayOutputStream ubaos = new UnsyncByteArrayOutputStream(
098 32000);
099
100 try {
101 ObjectOutputStream os = new ObjectOutputStream(ubaos);
102
103 os.flush();
104 os.writeObject(o);
105 os.flush();
106 }
107 catch (Exception e) {
108 _log.error(e, e);
109 }
110
111 return encode(ubaos.unsafeGetByteArray(), 0, ubaos.size());
112 }
113
114 public static Object stringToObject(String s) {
115 return _stringToObject(s, null, false);
116 }
117
118 public static Object stringToObject(String s, ClassLoader classLoader) {
119 return _stringToObject(s, classLoader, false);
120 }
121
122 public static Object stringToObjectSilent(String s) {
123 return _stringToObject(s, null, true);
124 }
125
126 public static Object stringToObjectSilent(
127 String s, ClassLoader classLoader) {
128
129 return _stringToObject(s, classLoader, true);
130 }
131
132 public static String toURLSafe(String base64) {
133 return StringUtil.replace(
134 base64,
135 new String[] {
136 StringPool.PLUS, StringPool.EQUAL, StringPool.SLASH
137 },
138 new String[] {
139 StringPool.MINUS, StringPool.STAR, StringPool.UNDERLINE
140 });
141 }
142
143 protected static char[] encodeBlock(byte[] raw, int offset, int lastIndex) {
144 int block = 0;
145 int slack = lastIndex - offset - 1;
146 int end = slack < 2 ? slack : 2;
147
148 for (int i = 0; i <= end; i++) {
149 byte b = raw[offset + i];
150
151 int neuter = b >= 0 ? ((int) (b)) : b + 256;
152 block += neuter << 8 * (2 - i);
153 }
154
155 char base64[] = new char[4];
156
157 for (int i = 0; i < 4; i++) {
158 int sixbit = block >>> 6 * (3 - i) & 0x3f;
159 base64[i] = getChar(sixbit);
160 }
161
162 if (slack < 1) {
163 base64[2] = CharPool.EQUAL;
164 }
165
166 if (slack < 2) {
167 base64[3] = CharPool.EQUAL;
168 }
169
170 return base64;
171 }
172
173 protected static char getChar(int sixbit) {
174 if ((sixbit >= 0) && (sixbit <= 25)) {
175 return (char)(65 + sixbit);
176 }
177
178 if ((sixbit >= 26) && (sixbit <= 51)) {
179 return (char)(97 + (sixbit - 26));
180 }
181
182 if ((sixbit >= 52) && (sixbit <= 61)) {
183 return (char)(48 + (sixbit - 52));
184 }
185
186 if (sixbit == 62) {
187 return CharPool.PLUS;
188 }
189
190 return sixbit != 63 ? CharPool.QUESTION : CharPool.SLASH;
191 }
192
193 protected static int getValue(char c) {
194 if ((c >= CharPool.UPPER_CASE_A) && (c <= CharPool.UPPER_CASE_Z)) {
195 return c - 65;
196 }
197
198 if ((c >= CharPool.LOWER_CASE_A) && (c <= CharPool.LOWER_CASE_Z)) {
199 return (c - 97) + 26;
200 }
201
202 if ((c >= CharPool.NUMBER_0) && (c <= CharPool.NUMBER_9)) {
203 return (c - 48) + 52;
204 }
205
206 if (c == CharPool.PLUS) {
207 return 62;
208 }
209
210 if (c == CharPool.SLASH) {
211 return 63;
212 }
213
214 return c != CharPool.EQUAL ? -1 : 0;
215 }
216
217 private static Object _stringToObject(
218 String s, ClassLoader classLoader, boolean silent) {
219
220 if (s == null) {
221 return null;
222 }
223
224 byte bytes[] = decode(s);
225
226 UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
227 bytes);
228
229 try {
230 ObjectInputStream is = null;
231
232 if (classLoader == null) {
233 is = new ObjectInputStream(ubais);
234 }
235 else {
236 is = new ClassLoaderObjectInputStream(ubais, classLoader);
237 }
238
239 return is.readObject();
240 }
241 catch (Exception e) {
242 if (!silent) {
243 _log.error(e, e);
244 }
245 }
246
247 return null;
248 }
249
250 private static Log _log = LogFactoryUtil.getLog(Base64.class);
251
252 }