001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025:
026: import java.io.BufferedInputStream;
027: import java.io.BufferedOutputStream;
028: import java.io.ByteArrayInputStream;
029: import java.io.IOException;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032:
033: /**
034: * <a href="Base64.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Brian Wing Shun Chan
037: *
038: */
039: public class Base64 {
040:
041: protected static char getChar(int sixbit) {
042: if (sixbit >= 0 && sixbit <= 25) {
043: return (char) (65 + sixbit);
044: }
045:
046: if (sixbit >= 26 && sixbit <= 51) {
047: return (char) (97 + (sixbit - 26));
048: }
049:
050: if (sixbit >= 52 && sixbit <= 61) {
051: return (char) (48 + (sixbit - 52));
052: }
053:
054: if (sixbit == 62) {
055: return '+';
056: }
057:
058: return sixbit != 63 ? '?' : '/';
059: }
060:
061: protected static int getValue(char c) {
062: if (c >= 'A' && c <= 'Z') {
063: return c - 65;
064: }
065:
066: if (c >= 'a' && c <= 'z') {
067: return (c - 97) + 26;
068: }
069:
070: if (c >= '0' && c <= '9') {
071: return (c - 48) + 52;
072: }
073:
074: if (c == '+') {
075: return 62;
076: }
077:
078: if (c == '/') {
079: return 63;
080: }
081:
082: return c != '=' ? -1 : 0;
083: }
084:
085: public static String encode(byte raw[]) {
086: StringMaker encoded = new StringMaker();
087:
088: for (int i = 0; i < raw.length; i += 3) {
089: encoded.append(encodeBlock(raw, i));
090: }
091:
092: return encoded.toString();
093: }
094:
095: protected static char[] encodeBlock(byte raw[], int offset) {
096: int block = 0;
097: int slack = raw.length - offset - 1;
098: int end = slack < 2 ? slack : 2;
099:
100: for (int i = 0; i <= end; i++) {
101: byte b = raw[offset + i];
102:
103: int neuter = b >= 0 ? ((int) (b)) : b + 256;
104: block += neuter << 8 * (2 - i);
105: }
106:
107: char base64[] = new char[4];
108:
109: for (int i = 0; i < 4; i++) {
110: int sixbit = block >>> 6 * (3 - i) & 0x3f;
111: base64[i] = getChar(sixbit);
112: }
113:
114: if (slack < 1) {
115: base64[2] = '=';
116: }
117:
118: if (slack < 2) {
119: base64[3] = '=';
120: }
121:
122: return base64;
123: }
124:
125: public static byte[] decode(String base64) {
126: int pad = 0;
127:
128: for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) {
129: pad++;
130: }
131:
132: int length = (base64.length() * 6) / 8 - pad;
133: byte raw[] = new byte[length];
134: int rawindex = 0;
135:
136: for (int i = 0; i < base64.length(); i += 4) {
137: int block = (getValue(base64.charAt(i)) << 18)
138: + (getValue(base64.charAt(i + 1)) << 12)
139: + (getValue(base64.charAt(i + 2)) << 6)
140: + getValue(base64.charAt(i + 3));
141:
142: for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
143: raw[rawindex + j] = (byte) (block >> 8 * (2 - j) & 0xff);
144: }
145:
146: rawindex += 3;
147: }
148:
149: return raw;
150: }
151:
152: public static String objectToString(Object o) {
153: if (o == null) {
154: return null;
155: }
156:
157: ByteArrayMaker bam = new ByteArrayMaker(32000);
158:
159: try {
160: ObjectOutputStream os = new ObjectOutputStream(
161: new BufferedOutputStream(bam));
162:
163: os.flush();
164: os.writeObject(o);
165: os.flush();
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 byteArray[] = decode(s);
179:
180: ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
181:
182: try {
183: ObjectInputStream is = new ObjectInputStream(
184: new BufferedInputStream(bais));
185:
186: return is.readObject();
187: } catch (Exception e) {
188: _log.error(e.getMessage());
189: }
190:
191: return null;
192: }
193:
194: private static Log _log = LogFactoryUtil.getLog(Base64.class);
195:
196: }
|