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 char[] {CharPool.MINUS, CharPool.STAR, CharPool.UNDERLINE},
086 new char[] {CharPool.PLUS, CharPool.EQUAL, CharPool.SLASH});
087 }
088
089 public static String objectToString(Object o) {
090 if (o == null) {
091 return null;
092 }
093
094 UnsyncByteArrayOutputStream ubaos = new UnsyncByteArrayOutputStream(
095 32000);
096
097 try (ObjectOutputStream os = new ObjectOutputStream(ubaos)) {
098 os.writeObject(o);
099 }
100 catch (Exception e) {
101 _log.error(e, e);
102 }
103
104 return encode(ubaos.unsafeGetByteArray(), 0, ubaos.size());
105 }
106
107 public static Object stringToObject(String s) {
108 return _stringToObject(s, null, false);
109 }
110
111 public static Object stringToObject(String s, ClassLoader classLoader) {
112 return _stringToObject(s, classLoader, false);
113 }
114
115 public static Object stringToObjectSilent(String s) {
116 return _stringToObject(s, null, true);
117 }
118
119 public static Object stringToObjectSilent(
120 String s, ClassLoader classLoader) {
121
122 return _stringToObject(s, classLoader, true);
123 }
124
125 public static String toURLSafe(String base64) {
126 return StringUtil.replace(
127 base64, new char[] {CharPool.PLUS, CharPool.EQUAL, CharPool.SLASH},
128 new char[] {CharPool.MINUS, CharPool.STAR, CharPool.UNDERLINE});
129 }
130
131 protected static char[] encodeBlock(byte[] raw, int offset, int lastIndex) {
132 int block = 0;
133 int slack = lastIndex - offset - 1;
134 int end = slack < 2 ? slack : 2;
135
136 for (int i = 0; i <= end; i++) {
137 byte b = raw[offset + i];
138
139 int neuter = b >= 0 ? ((int) (b)) : b + 256;
140 block += neuter << 8 * (2 - i);
141 }
142
143 char[] base64 = new char[4];
144
145 for (int i = 0; i < 4; i++) {
146 int sixbit = block >>> 6 * (3 - i) & 0x3f;
147 base64[i] = getChar(sixbit);
148 }
149
150 if (slack < 1) {
151 base64[2] = CharPool.EQUAL;
152 }
153
154 if (slack < 2) {
155 base64[3] = CharPool.EQUAL;
156 }
157
158 return base64;
159 }
160
161 protected static char getChar(int sixbit) {
162 if ((sixbit >= 0) && (sixbit <= 25)) {
163 return (char)(65 + sixbit);
164 }
165
166 if ((sixbit >= 26) && (sixbit <= 51)) {
167 return (char)(97 + (sixbit - 26));
168 }
169
170 if ((sixbit >= 52) && (sixbit <= 61)) {
171 return (char)(48 + (sixbit - 52));
172 }
173
174 if (sixbit == 62) {
175 return CharPool.PLUS;
176 }
177
178 if (sixbit != 63) {
179 return CharPool.QUESTION;
180 }
181
182 return CharPool.SLASH;
183 }
184
185 protected static int getValue(char c) {
186 if ((c >= CharPool.UPPER_CASE_A) && (c <= CharPool.UPPER_CASE_Z)) {
187 return c - 65;
188 }
189
190 if ((c >= CharPool.LOWER_CASE_A) && (c <= CharPool.LOWER_CASE_Z)) {
191 return (c - 97) + 26;
192 }
193
194 if ((c >= CharPool.NUMBER_0) && (c <= CharPool.NUMBER_9)) {
195 return (c - 48) + 52;
196 }
197
198 if (c == CharPool.PLUS) {
199 return 62;
200 }
201
202 if (c == CharPool.SLASH) {
203 return 63;
204 }
205
206 if (c != CharPool.EQUAL) {
207 return -1;
208 }
209
210 return 0;
211 }
212
213 private static Object _stringToObject(
214 String s, ClassLoader classLoader, boolean silent) {
215
216 if (s == null) {
217 return null;
218 }
219
220 byte[] bytes = decode(s);
221
222 UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
223 bytes);
224
225 try {
226 ObjectInputStream is = null;
227
228 if (classLoader == null) {
229 is = new ProtectedObjectInputStream(ubais);
230 }
231 else {
232 is = new ProtectedClassLoaderObjectInputStream(
233 ubais, classLoader);
234 }
235
236 return is.readObject();
237 }
238 catch (Exception e) {
239 if (!silent) {
240 _log.error(e, e);
241 }
242 }
243
244 return null;
245 }
246
247 private static final Log _log = LogFactoryUtil.getLog(Base64.class);
248
249 }