001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.IOException;
023    import java.io.ObjectInputStream;
024    import java.io.ObjectOutputStream;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class Base64 {
030    
031            protected static char getChar(int sixbit) {
032                    if (sixbit >= 0 && sixbit <= 25) {
033                            return (char)(65 + sixbit);
034                    }
035    
036                    if (sixbit >= 26 && sixbit <= 51) {
037                            return (char)(97 + (sixbit - 26));
038                    }
039    
040                    if (sixbit >= 52 && sixbit <= 61) {
041                            return (char)(48 + (sixbit - 52));
042                    }
043    
044                    if (sixbit == 62) {
045                            return CharPool.PLUS;
046                    }
047    
048                    return sixbit != 63 ? CharPool.QUESTION : CharPool.SLASH;
049            }
050    
051            protected static int getValue(char c) {
052                    if ((c >= CharPool.UPPER_CASE_A) && (c <= CharPool.UPPER_CASE_Z)) {
053                            return c - 65;
054                    }
055    
056                    if ((c >= CharPool.LOWER_CASE_A) && (c <= CharPool.LOWER_CASE_Z)) {
057                            return (c - 97) + 26;
058                    }
059    
060                    if (c >= CharPool.NUMBER_0 && c <= CharPool.NUMBER_9) {
061                            return (c - 48) + 52;
062                    }
063    
064                    if (c == CharPool.PLUS) {
065                            return 62;
066                    }
067    
068                    if (c == CharPool.SLASH) {
069                            return 63;
070                    }
071    
072                    return c != CharPool.EQUAL ? -1 : 0;
073            }
074    
075            public static String encode(byte raw[]) {
076                    return encode(raw, 0, raw.length);
077            }
078    
079            public static String encode(byte raw[], int offset, int length) {
080                    int lastIndex = Math.min(raw.length, offset + length);
081    
082                    StringBuilder sb = new StringBuilder(
083                            ((lastIndex - offset) / 3 + 1) * 4);
084    
085                    for (int i = offset; i < lastIndex; i += 3) {
086                            sb.append(encodeBlock(raw, i, lastIndex));
087                    }
088    
089                    return sb.toString();
090            }
091    
092            protected static char[] encodeBlock(byte raw[], int offset, int lastIndex) {
093                    int block = 0;
094                    int slack = lastIndex - offset - 1;
095                    int end = slack < 2 ? slack : 2;
096    
097                    for (int i = 0; i <= end; i++) {
098                            byte b = raw[offset + i];
099    
100                            int neuter = b >= 0 ? ((int) (b)) : b + 256;
101                            block += neuter << 8 * (2 - i);
102                    }
103    
104                    char base64[] = new char[4];
105    
106                    for (int i = 0; i < 4; i++) {
107                            int sixbit = block >>> 6 * (3 - i) & 0x3f;
108                            base64[i] = getChar(sixbit);
109                    }
110    
111                    if (slack < 1) {
112                            base64[2] = CharPool.EQUAL;
113                    }
114    
115                    if (slack < 2) {
116                            base64[3] = CharPool.EQUAL;
117                    }
118    
119                    return base64;
120            }
121    
122            public static byte[] decode(String base64) {
123                    if (Validator.isNull(base64)) {
124                            return new byte[0];
125                    }
126    
127                    int pad = 0;
128    
129                    for (int i = base64.length() - 1; base64.charAt(i) == CharPool.EQUAL;
130                                    i--) {
131    
132                            pad++;
133                    }
134    
135                    int length = (base64.length() * 6) / 8 - pad;
136                    byte raw[] = new byte[length];
137                    int rawindex = 0;
138    
139                    for (int i = 0; i < base64.length(); i += 4) {
140                            int block = (getValue(base64.charAt(i)) << 18) +
141                                                    (getValue(base64.charAt(i + 1)) << 12) +
142                                                    (getValue(base64.charAt(i + 2)) << 6) +
143                                                    getValue(base64.charAt(i + 3));
144    
145                            for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
146                                    raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
147                            }
148    
149                            rawindex += 3;
150                    }
151    
152                    return raw;
153            }
154    
155            public static String objectToString(Object o) {
156                    if (o == null) {
157                            return null;
158                    }
159    
160                    UnsyncByteArrayOutputStream ubaos = new UnsyncByteArrayOutputStream(
161                            32000);
162    
163                    try {
164                            ObjectOutputStream os = new ObjectOutputStream(ubaos);
165    
166                            os.flush();
167                            os.writeObject(o);
168                            os.flush();
169                    }
170                    catch (IOException e) {
171                            _log.error(e, e);
172                    }
173    
174                    return encode(ubaos.unsafeGetByteArray(), 0, ubaos.size());
175            }
176    
177            public static Object stringToObject(String s) {
178                    return stringToObject(s, null);
179            }
180    
181            public static Object stringToObject(String s, ClassLoader classLoader) {
182                    if (s == null) {
183                            return null;
184                    }
185    
186                    byte bytes[] = decode(s);
187    
188                    UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
189                            bytes);
190    
191                    try {
192                            ObjectInputStream is = null;
193    
194                            if (classLoader == null) {
195                                    is = new ObjectInputStream(ubais);
196                            }
197                            else {
198                                    is = new ClassLoaderObjectInputStream(ubais, classLoader);
199                            }
200    
201                            return is.readObject();
202                    }
203                    catch (Exception e) {
204                            _log.error(e, e);
205                    }
206    
207                    return null;
208            }
209    
210            private static Log _log = LogFactoryUtil.getLog(Base64.class);
211    
212    }