1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
41  public class Base64 {
42  
43      protected static char getChar(int sixbit) {
44          if (sixbit >= 0 && sixbit <= 25) {
45              return (char)(65 + sixbit);
46          }
47  
48          if (sixbit >= 26 && sixbit <= 51) {
49              return (char)(97 + (sixbit - 26));
50          }
51  
52          if (sixbit >= 52 && sixbit <= 61) {
53              return (char)(48 + (sixbit - 52));
54          }
55  
56          if (sixbit == 62) {
57              return '+';
58          }
59  
60          return sixbit != 63 ? '?' : '/';
61      }
62  
63      protected static int getValue(char c) {
64          if (c >= 'A' && c <= 'Z') {
65              return c - 65;
66          }
67  
68          if (c >= 'a' && c <= 'z') {
69              return (c - 97) + 26;
70          }
71  
72          if (c >= '0' && c <= '9') {
73              return (c - 48) + 52;
74          }
75  
76          if (c == '+') {
77              return 62;
78          }
79  
80          if (c == '/') {
81              return 63;
82          }
83  
84          return c != '=' ? -1 : 0;
85      }
86  
87      public static String encode(byte raw[]) {
88          StringBuilder encoded = new StringBuilder();
89  
90          for (int i = 0; i < raw.length; i += 3) {
91              encoded.append(encodeBlock(raw, i));
92          }
93  
94          return encoded.toString();
95      }
96  
97      protected static char[] encodeBlock(byte raw[], int offset) {
98          int block = 0;
99          int slack = raw.length - offset - 1;
100         int end = slack < 2 ? slack : 2;
101 
102         for (int i = 0; i <= end; i++) {
103             byte b = raw[offset + i];
104 
105             int neuter = b >= 0 ? ((int) (b)) : b + 256;
106             block += neuter << 8 * (2 - i);
107         }
108 
109         char base64[] = new char[4];
110 
111         for (int i = 0; i < 4; i++) {
112             int sixbit = block >>> 6 * (3 - i) & 0x3f;
113             base64[i] = getChar(sixbit);
114         }
115 
116         if (slack < 1) {
117             base64[2] = '=';
118         }
119 
120         if (slack < 2) {
121             base64[3] = '=';
122         }
123 
124         return base64;
125     }
126 
127     public static byte[] decode(String base64) {
128         if (Validator.isNull(base64)) {
129             return new byte[0];
130         }
131 
132         int pad = 0;
133 
134         for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) {
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         ByteArrayMaker bam = new ByteArrayMaker(32000);
164 
165         try {
166             ObjectOutputStream os = new ObjectOutputStream(
167                 new BufferedOutputStream(bam));
168 
169             os.flush();
170             os.writeObject(o);
171             os.flush();
172         }
173         catch (IOException e) {
174             _log.error(e.getMessage());
175         }
176 
177         return encode(bam.toByteArray());
178     }
179 
180     public static Object stringToObject(String s) {
181         if (s == null) {
182             return null;
183         }
184 
185         byte bytes[] = decode(s);
186 
187         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
188 
189         try {
190             ObjectInputStream is = new ObjectInputStream(
191                 new BufferedInputStream(bais));
192 
193             return is.readObject();
194         }
195         catch (Exception e) {
196             _log.error(e.getMessage());
197         }
198 
199         return null;
200     }
201 
202     private static Log _log = LogFactoryUtil.getLog(Base64.class);
203 
204 }