001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.kvem.midp.pim.formats;
028:
029: import java.io.ByteArrayOutputStream;
030:
031: /**
032: * Converter to and from Base64 encoding. Base64 encoding is defined in
033: * RFC 2045.
034: *
035: */
036: public class Base64Encoding {
037:
038: /** Constants for conversion of binary data to Base64. From RFC 2045. */
039: private static char[] BASE64_CHARS = { 'A', 'B', 'C', 'D', 'E',
040: 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
041: 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
042: 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
043: 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0',
044: '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
045:
046: /**
047: * Constants for conversion of Base64 data to binary. The inverse
048: * of the above table.
049: */
050: private static byte[] BASE64_BYTES = { -1, -1, -1, -1, -1, -1, -1,
051: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
052: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
053: -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55,
054: 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1, -1, 0, 1, 2,
055: 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
056: 19, 20, 21, 22, 23, 24, 25, 26, -1, -1, -1, -1, -1, 26, 27,
057: 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
058: 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };
059:
060: /**
061: * Converts a BASE64 string to a byte array.
062: *
063: * @param sdata the string to be converted
064: * @return the byte array content of the string
065: */
066: public static byte[] fromBase64(String sdata) {
067: if (sdata == null || sdata.length() < 2) {
068: return new byte[0];
069: }
070: ByteArrayOutputStream out = new ByteArrayOutputStream();
071: // copy the string to an array, and pad the end of the data with
072: // '=' characters.
073: int length = sdata.length();
074: char[] data = new char[length + 2];
075: sdata.getChars(0, length, data, 0);
076: data[length] = '=';
077: data[length + 1] = '=';
078: for (int i = nextCharIndex(data, 0); i < data.length;) {
079: int char0 = data[i = nextCharIndex(data, i)];
080: if (char0 == '=') {
081: break;
082: }
083: int char1 = data[i = nextCharIndex(data, i + 1)];
084: if (char1 == '=') {
085: // stop here. this is not a valid Base64 fragment
086: break;
087: }
088: int char2 = data[i = nextCharIndex(data, i + 1)];
089: int char3 = data[i = nextCharIndex(data, i + 1)];
090: i = nextCharIndex(data, i + 1);
091: out.write(BASE64_BYTES[char0] << 2
092: | BASE64_BYTES[char1] >> 4);
093: if (char2 == '=') {
094: // only one byte
095: } else {
096: int value = BASE64_BYTES[char1] << 4
097: | BASE64_BYTES[char2] >> 2;
098: out.write(value & 0xff);
099: if (char3 == '=') {
100: // only 2 bytes
101: } else {
102: value = BASE64_BYTES[char2] << 6
103: | BASE64_BYTES[char3];
104: out.write(value & 0xff);
105: }
106: }
107: }
108: return out.toByteArray();
109: }
110:
111: /**
112: * Gets the index of the first character in the given array that
113: * contains valid Base64 data. Starts searching from the given
114: * index "i".
115: * @param data input buffer
116: * @param i starting offset in the input buffer
117: * @return offset of first BASE64 char
118: */
119: private static int nextCharIndex(char[] data, int i) {
120: while (i < data.length
121: && (data[i] > 0x7f || BASE64_BYTES[data[i]] == -1)) {
122:
123: i++;
124: }
125: return i;
126: }
127:
128: /**
129: * Converts a byte array to a BASE64 string.
130: * @param data the binary data to be converted
131: * @param lineLength the length of lines to be created
132: * @param indent the number of blank spaces to write at the beginning
133: * of each line
134: * @return BASE64 string
135: */
136: public static String toBase64(byte[] data, int lineLength,
137: int indent) {
138: StringBuffer sb = new StringBuffer();
139: for (int i = 0, charsInLine = 0; i < data.length;) {
140: int byte0 = ((int) data[i++]) & 0xff;
141: int byte1 = (i < data.length) ? ((int) data[i++]) & 0xff
142: : 0x100;
143: int byte2 = (i < data.length) ? ((int) data[i++]) & 0xff
144: : 0x100;
145:
146: sb.append(BASE64_CHARS[byte0 >> 2]);
147: if (byte1 == 0x100) {
148: sb.append(BASE64_CHARS[(byte0 << 4) & 0x30]);
149: sb.append("==");
150: } else {
151: sb
152: .append(BASE64_CHARS[(byte0 << 4 | byte1 >> 4) & 0x3f]);
153: if (byte2 == 0x100) {
154: sb.append(BASE64_CHARS[(byte1 << 2) & 0x3f]);
155: sb.append('=');
156: } else {
157: sb
158: .append(BASE64_CHARS[(byte1 << 2 | byte2 >> 6) & 0x3f]);
159: sb.append(BASE64_CHARS[byte2 & 0x3f]);
160: }
161: }
162: charsInLine += 4;
163: if (charsInLine + 4 > lineLength && i < data.length) {
164: sb.append("\r\n");
165: for (int j = 0; j < indent; j++) {
166: sb.append(' ');
167: }
168: }
169: }
170: return sb.toString();
171: }
172:
173: }
|