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