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         int pad = 0;
126 
127         for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) {
128             pad++;
129         }
130 
131         int length = (base64.length() * 6) / 8 - pad;
132         byte raw[] = new byte[length];
133         int rawindex = 0;
134 
135         for (int i = 0; i < base64.length(); i += 4) {
136             int block = (getValue(base64.charAt(i)) << 18) +
137                         (getValue(base64.charAt(i + 1)) << 12) +
138                         (getValue(base64.charAt(i + 2)) << 6) +
139                         getValue(base64.charAt(i + 3));
140 
141             for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
142                 raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
143             }
144 
145             rawindex += 3;
146         }
147 
148         return raw;
149     }
150 
151     public static String objectToString(Object o) {
152         if (o == null) {
153             return null;
154         }
155 
156         ByteArrayMaker bam = new ByteArrayMaker(32000);
157 
158         try {
159             ObjectOutputStream os = new ObjectOutputStream(
160                 new BufferedOutputStream(bam));
161 
162             os.flush();
163             os.writeObject(o);
164             os.flush();
165         }
166         catch (IOException e) {
167             _log.error(e.getMessage());
168         }
169 
170         return encode(bam.toByteArray());
171     }
172 
173     public static Object stringToObject(String s) {
174         if (s == null) {
175             return null;
176         }
177 
178         byte bytes[] = decode(s);
179 
180         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
181 
182         try {
183             ObjectInputStream is = new ObjectInputStream(
184                 new BufferedInputStream(bais));
185 
186             return is.readObject();
187         }
188         catch (Exception e) {
189             _log.error(e.getMessage());
190         }
191 
192         return null;
193     }
194 
195     private static Log _log = LogFactoryUtil.getLog(Base64.class);
196 
197 }