001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
026     * @author Brian Wing Shun Chan
027     */
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 (ObjectOutputStream os = new ObjectOutputStream(ubaos)) {
101                            os.writeObject(o);
102                    }
103                    catch (Exception e) {
104                            _log.error(e, e);
105                    }
106    
107                    return encode(ubaos.unsafeGetByteArray(), 0, ubaos.size());
108            }
109    
110            public static Object stringToObject(String s) {
111                    return _stringToObject(s, null, false);
112            }
113    
114            public static Object stringToObject(String s, ClassLoader classLoader) {
115                    return _stringToObject(s, classLoader, false);
116            }
117    
118            public static Object stringToObjectSilent(String s) {
119                    return _stringToObject(s, null, true);
120            }
121    
122            public static Object stringToObjectSilent(
123                    String s, ClassLoader classLoader) {
124    
125                    return _stringToObject(s, classLoader, true);
126            }
127    
128            public static String toURLSafe(String base64) {
129                    return StringUtil.replace(
130                            base64,
131                            new String[] {
132                                    StringPool.PLUS, StringPool.EQUAL, StringPool.SLASH
133                            },
134                            new String[] {
135                                    StringPool.MINUS, StringPool.STAR, StringPool.UNDERLINE
136                            });
137            }
138    
139            protected static char[] encodeBlock(byte[] raw, int offset, int lastIndex) {
140                    int block = 0;
141                    int slack = lastIndex - offset - 1;
142                    int end = slack < 2 ? slack : 2;
143    
144                    for (int i = 0; i <= end; i++) {
145                            byte b = raw[offset + i];
146    
147                            int neuter = b >= 0 ? ((int) (b)) : b + 256;
148                            block += neuter << 8 * (2 - i);
149                    }
150    
151                    char[] base64 = new char[4];
152    
153                    for (int i = 0; i < 4; i++) {
154                            int sixbit = block >>> 6 * (3 - i) & 0x3f;
155                            base64[i] = getChar(sixbit);
156                    }
157    
158                    if (slack < 1) {
159                            base64[2] = CharPool.EQUAL;
160                    }
161    
162                    if (slack < 2) {
163                            base64[3] = CharPool.EQUAL;
164                    }
165    
166                    return base64;
167            }
168    
169            protected static char getChar(int sixbit) {
170                    if ((sixbit >= 0) && (sixbit <= 25)) {
171                            return (char)(65 + sixbit);
172                    }
173    
174                    if ((sixbit >= 26) && (sixbit <= 51)) {
175                            return (char)(97 + (sixbit - 26));
176                    }
177    
178                    if ((sixbit >= 52) && (sixbit <= 61)) {
179                            return (char)(48 + (sixbit - 52));
180                    }
181    
182                    if (sixbit == 62) {
183                            return CharPool.PLUS;
184                    }
185    
186                    return sixbit != 63 ? CharPool.QUESTION : CharPool.SLASH;
187            }
188    
189            protected static int getValue(char c) {
190                    if ((c >= CharPool.UPPER_CASE_A) && (c <= CharPool.UPPER_CASE_Z)) {
191                            return c - 65;
192                    }
193    
194                    if ((c >= CharPool.LOWER_CASE_A) && (c <= CharPool.LOWER_CASE_Z)) {
195                            return (c - 97) + 26;
196                    }
197    
198                    if ((c >= CharPool.NUMBER_0) && (c <= CharPool.NUMBER_9)) {
199                            return (c - 48) + 52;
200                    }
201    
202                    if (c == CharPool.PLUS) {
203                            return 62;
204                    }
205    
206                    if (c == CharPool.SLASH) {
207                            return 63;
208                    }
209    
210                    return c != CharPool.EQUAL ? -1 : 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 ObjectInputStream(ubais);
230                            }
231                            else {
232                                    is = new ClassLoaderObjectInputStream(ubais, classLoader);
233                            }
234    
235                            return is.readObject();
236                    }
237                    catch (Exception e) {
238                            if (!silent) {
239                                    _log.error(e, e);
240                            }
241                    }
242    
243                    return null;
244            }
245    
246            private static Log _log = LogFactoryUtil.getLog(Base64.class);
247    
248    }