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