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