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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.io.BufferedInputStream;
29  import java.io.BufferedOutputStream;
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  /**
36   * <a href="Base64.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class Base64 {
41  
42      protected static char getChar(int sixbit) {
43          if (sixbit >= 0 && sixbit <= 25) {
44              return (char)(65 + sixbit);
45          }
46  
47          if (sixbit >= 26 && sixbit <= 51) {
48              return (char)(97 + (sixbit - 26));
49          }
50  
51          if (sixbit >= 52 && sixbit <= 61) {
52              return (char)(48 + (sixbit - 52));
53          }
54  
55          if (sixbit == 62) {
56              return '+';
57          }
58  
59          return sixbit != 63 ? '?' : '/';
60      }
61  
62      protected static int getValue(char c) {
63          if (c >= 'A' && c <= 'Z') {
64              return c - 65;
65          }
66  
67          if (c >= 'a' && c <= 'z') {
68              return (c - 97) + 26;
69          }
70  
71          if (c >= '0' && c <= '9') {
72              return (c - 48) + 52;
73          }
74  
75          if (c == '+') {
76              return 62;
77          }
78  
79          if (c == '/') {
80              return 63;
81          }
82  
83          return c != '=' ? -1 : 0;
84      }
85  
86      public static String encode(byte raw[]) {
87          StringBuilder encoded = new StringBuilder();
88  
89          for (int i = 0; i < raw.length; i += 3) {
90              encoded.append(encodeBlock(raw, i));
91          }
92  
93          return encoded.toString();
94      }
95  
96      protected static char[] encodeBlock(byte raw[], int offset) {
97          int block = 0;
98          int slack = raw.length - offset - 1;
99          int end = slack < 2 ? slack : 2;
100 
101         for (int i = 0; i <= end; i++) {
102             byte b = raw[offset + i];
103 
104             int neuter = b >= 0 ? ((int) (b)) : b + 256;
105             block += neuter << 8 * (2 - i);
106         }
107 
108         char base64[] = new char[4];
109 
110         for (int i = 0; i < 4; i++) {
111             int sixbit = block >>> 6 * (3 - i) & 0x3f;
112             base64[i] = getChar(sixbit);
113         }
114 
115         if (slack < 1) {
116             base64[2] = '=';
117         }
118 
119         if (slack < 2) {
120             base64[3] = '=';
121         }
122 
123         return base64;
124     }
125 
126     public static byte[] decode(String base64) {
127         if (Validator.isNull(base64)) {
128             return new byte[0];
129         }
130 
131         int pad = 0;
132 
133         for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) {
134             pad++;
135         }
136 
137         int length = (base64.length() * 6) / 8 - pad;
138         byte raw[] = new byte[length];
139         int rawindex = 0;
140 
141         for (int i = 0; i < base64.length(); i += 4) {
142             int block = (getValue(base64.charAt(i)) << 18) +
143                         (getValue(base64.charAt(i + 1)) << 12) +
144                         (getValue(base64.charAt(i + 2)) << 6) +
145                         getValue(base64.charAt(i + 3));
146 
147             for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
148                 raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff);
149             }
150 
151             rawindex += 3;
152         }
153 
154         return raw;
155     }
156 
157     public static String objectToString(Object o) {
158         if (o == null) {
159             return null;
160         }
161 
162         ByteArrayMaker bam = new ByteArrayMaker(32000);
163 
164         try {
165             ObjectOutputStream os = new ObjectOutputStream(
166                 new BufferedOutputStream(bam));
167 
168             os.flush();
169             os.writeObject(o);
170             os.flush();
171         }
172         catch (IOException e) {
173             _log.error(e.getMessage());
174         }
175 
176         return encode(bam.toByteArray());
177     }
178 
179     public static Object stringToObject(String s) {
180         if (s == null) {
181             return null;
182         }
183 
184         byte bytes[] = decode(s);
185 
186         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
187 
188         try {
189             ObjectInputStream is = new ObjectInputStream(
190                 new BufferedInputStream(bais));
191 
192             return is.readObject();
193         }
194         catch (Exception e) {
195             _log.error(e.getMessage());
196         }
197 
198         return null;
199     }
200 
201     private static Log _log = LogFactoryUtil.getLog(Base64.class);
202 
203 }