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