1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
26  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  
30  import java.io.IOException;
31  import java.io.ObjectInputStream;
32  import java.io.ObjectOutputStream;
33  
34  /**
35   * <a href="Base64.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class Base64 {
40  
41      protected static char getChar(int sixbit) {
42          if (sixbit >= 0 && sixbit <= 25) {
43              return (char)(65 + sixbit);
44          }
45  
46          if (sixbit >= 26 && sixbit <= 51) {
47              return (char)(97 + (sixbit - 26));
48          }
49  
50          if (sixbit >= 52 && sixbit <= 61) {
51              return (char)(48 + (sixbit - 52));
52          }
53  
54          if (sixbit == 62) {
55              return CharPool.PLUS;
56          }
57  
58          return sixbit != 63 ? CharPool.QUESTION : CharPool.SLASH;
59      }
60  
61      protected static int getValue(char c) {
62          if ((c >= CharPool.UPPER_CASE_A) && (c <= CharPool.UPPER_CASE_Z)) {
63              return c - 65;
64          }
65  
66          if ((c >= CharPool.LOWER_CASE_A) && (c <= CharPool.LOWER_CASE_Z)) {
67              return (c - 97) + 26;
68          }
69  
70          if (c >= CharPool.NUMBER_0 && c <= CharPool.NUMBER_9) {
71              return (c - 48) + 52;
72          }
73  
74          if (c == CharPool.PLUS) {
75              return 62;
76          }
77  
78          if (c == CharPool.SLASH) {
79              return 63;
80          }
81  
82          return c != CharPool.EQUAL ? -1 : 0;
83      }
84  
85      public static String encode(byte raw[]) {
86          StringBuilder encoded = new StringBuilder();
87  
88          for (int i = 0; i < raw.length; i += 3) {
89              encoded.append(encodeBlock(raw, i));
90          }
91  
92          return encoded.toString();
93      }
94  
95      protected static char[] encodeBlock(byte raw[], int offset) {
96          int block = 0;
97          int slack = raw.length - offset - 1;
98          int end = slack < 2 ? slack : 2;
99  
100         for (int i = 0; i <= end; i++) {
101             byte b = raw[offset + i];
102 
103             int neuter = b >= 0 ? ((int) (b)) : b + 256;
104             block += neuter << 8 * (2 - i);
105         }
106 
107         char base64[] = new char[4];
108 
109         for (int i = 0; i < 4; i++) {
110             int sixbit = block >>> 6 * (3 - i) & 0x3f;
111             base64[i] = getChar(sixbit);
112         }
113 
114         if (slack < 1) {
115             base64[2] = CharPool.EQUAL;
116         }
117 
118         if (slack < 2) {
119             base64[3] = CharPool.EQUAL;
120         }
121 
122         return base64;
123     }
124 
125     public static byte[] decode(String base64) {
126         if (Validator.isNull(base64)) {
127             return new byte[0];
128         }
129 
130         int pad = 0;
131 
132         for (int i = base64.length() - 1; base64.charAt(i) == CharPool.EQUAL;
133                 i--) {
134 
135             pad++;
136         }
137 
138         int length = (base64.length() * 6) / 8 - pad;
139         byte raw[] = new byte[length];
140         int rawindex = 0;
141 
142         for (int i = 0; i < base64.length(); i += 4) {
143             int block = (getValue(base64.charAt(i)) << 18) +
144                         (getValue(base64.charAt(i + 1)) << 12) +
145                         (getValue(base64.charAt(i + 2)) << 6) +
146                         getValue(base64.charAt(i + 3));
147 
148             for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
149                 raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
150             }
151 
152             rawindex += 3;
153         }
154 
155         return raw;
156     }
157 
158     public static String objectToString(Object o) {
159         if (o == null) {
160             return null;
161         }
162 
163         UnsyncByteArrayOutputStream ubaos = new UnsyncByteArrayOutputStream(
164             32000);
165 
166         try {
167             ObjectOutputStream os = new ObjectOutputStream(ubaos);
168 
169             os.flush();
170             os.writeObject(o);
171             os.flush();
172         }
173         catch (IOException e) {
174             _log.error(e, e);
175         }
176 
177         return encode(ubaos.toByteArray());
178     }
179 
180     public static Object stringToObject(String s) {
181         return stringToObject(s, null);
182     }
183 
184     public static Object stringToObject(String s, ClassLoader classLoader) {
185         if (s == null) {
186             return null;
187         }
188 
189         byte bytes[] = decode(s);
190 
191         UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
192             bytes);
193 
194         try {
195             ObjectInputStream is = null;
196 
197             if (classLoader == null) {
198                 is = new ObjectInputStream(ubais);
199             }
200             else {
201                 is = new ClassLoaderObjectInputStream(ubais, classLoader);
202             }
203 
204             return is.readObject();
205         }
206         catch (Exception e) {
207             _log.error(e, e);
208         }
209 
210         return null;
211     }
212 
213     private static Log _log = LogFactoryUtil.getLog(Base64.class);
214 
215 }