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