001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.base.util;
019:
020: /**
021: * Base64 implements Base64 encoding and Base 64 decoding.
022: *
023: */
024:
025: public class Base64 {
026:
027: private static byte[] Base64EncMap, Base64DecMap;
028: static {
029: // rfc-2045: Base64 Alphabet
030: byte[] map = { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D',
031: (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
032: (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
033: (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',
034: (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
035: (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',
036: (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b',
037: (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
038: (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j',
039: (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
040: (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r',
041: (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
042: (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',
043: (byte) '0', (byte) '1', (byte) '2', (byte) '3',
044: (byte) '4', (byte) '5', (byte) '6', (byte) '7',
045: (byte) '8', (byte) '9', (byte) '+', (byte) '/' };
046: Base64EncMap = map;
047: Base64DecMap = new byte[128];
048: for (int idx = 0; idx < Base64EncMap.length; idx++) {
049: Base64DecMap[Base64EncMap[idx]] = (byte) idx;
050: }
051: }
052:
053: /**
054: * This method decodes the given byte[] using the base64-encoding
055: * specified in RFC-2045 (Section 6.8).
056: *
057: * @param data the base64-encoded data.
058: * @return the decoded data.
059: */
060: public final static byte[] base64Decode(byte[] data) {
061: if (data == null) {
062: return null;
063: }
064:
065: int tail = data.length;
066: while (data[tail - 1] == '=') {
067: tail--;
068: }
069:
070: byte dest[] = new byte[tail - data.length / 4];
071:
072: // ascii printable to 0-63 conversion
073: for (int idx = 0; idx < data.length; idx++) {
074: data[idx] = Base64DecMap[data[idx]];
075: }
076:
077: // 4-byte to 3-byte conversion
078: int sidx, didx;
079: for (sidx = 0, didx = 0; didx < dest.length - 2; sidx += 4, didx += 3) {
080: dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx + 1] >>> 4) & 003));
081: dest[didx + 1] = (byte) (((data[sidx + 1] << 4) & 255) | ((data[sidx + 2] >>> 2) & 017));
082: dest[didx + 2] = (byte) (((data[sidx + 2] << 6) & 255) | (data[sidx + 3] & 077));
083: }
084: if (didx < dest.length) {
085: dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx + 1] >>> 4) & 003));
086: }
087: if (++didx < dest.length) {
088: dest[didx] = (byte) (((data[sidx + 1] << 4) & 255) | ((data[sidx + 2] >>> 2) & 017));
089: }
090:
091: return dest;
092: }
093:
094: /**
095: * This method decodes the given string using the base64-encoding
096: * specified in RFC-2045 (Section 6.8).
097: *
098: * @param str the base64-encoded string.
099: * @return the decoded str.
100: */
101: public final static String base64Decode(String str) {
102: if (str == null) {
103: return null;
104: }
105:
106: byte data[] = new byte[str.length()];
107: data = str.getBytes();
108: return new String(base64Decode(data));
109: }
110:
111: /**
112: * This method encodes the given byte[] using the base64-encoding
113: * specified in RFC-2045 (Section 6.8).
114: *
115: * @param data the data
116: * @return the base64-encoded data
117: */
118: public final static byte[] base64Encode(byte[] data) {
119: if (data == null) {
120: return null;
121: }
122:
123: int sidx, didx;
124: byte dest[] = new byte[((data.length + 2) / 3) * 4];
125:
126: // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
127: for (sidx = 0, didx = 0; sidx < data.length - 2; sidx += 3) {
128: dest[didx++] = Base64EncMap[(data[sidx] >>> 2) & 077];
129: dest[didx++] = Base64EncMap[(data[sidx + 1] >>> 4) & 017
130: | (data[sidx] << 4) & 077];
131: dest[didx++] = Base64EncMap[(data[sidx + 2] >>> 6) & 003
132: | (data[sidx + 1] << 2) & 077];
133: dest[didx++] = Base64EncMap[data[sidx + 2] & 077];
134: }
135: if (sidx < data.length) {
136: dest[didx++] = Base64EncMap[(data[sidx] >>> 2) & 077];
137: if (sidx < data.length - 1) {
138: dest[didx++] = Base64EncMap[(data[sidx + 1] >>> 4)
139: & 017 | (data[sidx] << 4) & 077];
140: dest[didx++] = Base64EncMap[(data[sidx + 1] << 2) & 077];
141: } else
142: dest[didx++] = Base64EncMap[(data[sidx] << 4) & 077];
143: }
144:
145: // add padding
146: for (; didx < dest.length; didx++) {
147: dest[didx] = (byte) '=';
148: }
149:
150: return dest;
151: }
152:
153: /**
154: * This method encodes the given string using the base64-encoding
155: * specified in RFC-2045 (Section 6.8).
156: *
157: * @param str the string
158: * @return the base64-encoded str
159: */
160: public final static String base64Encode(String str) {
161: if (str == null) {
162: return null;
163: }
164: byte data[] = new byte[str.length()];
165:
166: data = str.getBytes();
167: return new String(base64Encode(data));
168: }
169: }
|