001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.algorithm;
040:
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.io.OutputStream;
044: import org.jvnet.fastinfoset.EncodingAlgorithmException;
045: import com.sun.xml.fastinfoset.CommonResourceBundle;
046:
047: public class BASE64EncodingAlgorithm extends BuiltInEncodingAlgorithm {
048:
049: /* package */static final char encodeBase64[] = { 'A', 'B', 'C',
050: 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
051: 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
052: 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
053: 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
054: 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+',
055: '/' };
056:
057: /* package */static final int decodeBase64[] = {
058: /*'+'*/62, -1, -1, -1,
059: /*'/'*/63,
060: /*'0'*/52,
061: /*'1'*/53,
062: /*'2'*/54,
063: /*'3'*/55,
064: /*'4'*/56,
065: /*'5'*/57,
066: /*'6'*/58,
067: /*'7'*/59,
068: /*'8'*/60,
069: /*'9'*/61, -1, -1, -1, -1, -1, -1, -1,
070: /*'A'*/0,
071: /*'B'*/1,
072: /*'C'*/2,
073: /*'D'*/3,
074: /*'E'*/4,
075: /*'F'*/5,
076: /*'G'*/6,
077: /*'H'*/7,
078: /*'I'*/8,
079: /*'J'*/9,
080: /*'K'*/10,
081: /*'L'*/11,
082: /*'M'*/12,
083: /*'N'*/13,
084: /*'O'*/14,
085: /*'P'*/15,
086: /*'Q'*/16,
087: /*'R'*/17,
088: /*'S'*/18,
089: /*'T'*/19,
090: /*'U'*/20,
091: /*'V'*/21,
092: /*'W'*/22,
093: /*'X'*/23,
094: /*'Y'*/24,
095: /*'Z'*/25, -1, -1, -1, -1, -1, -1,
096: /*'a'*/26,
097: /*'b'*/27,
098: /*'c'*/28,
099: /*'d'*/29,
100: /*'e'*/30,
101: /*'f'*/31,
102: /*'g'*/32,
103: /*'h'*/33,
104: /*'i'*/34,
105: /*'j'*/35,
106: /*'k'*/36,
107: /*'l'*/37,
108: /*'m'*/38,
109: /*'n'*/39,
110: /*'o'*/40,
111: /*'p'*/41,
112: /*'q'*/42,
113: /*'r'*/43,
114: /*'s'*/44,
115: /*'t'*/45,
116: /*'u'*/46,
117: /*'v'*/47,
118: /*'w'*/48,
119: /*'x'*/49,
120: /*'y'*/50,
121: /*'z'*/51 };
122:
123: public final Object decodeFromBytes(byte[] b, int start, int length)
124: throws EncodingAlgorithmException {
125: final byte[] data = new byte[length];
126: System.arraycopy(b, start, data, 0, length);
127: return data;
128: }
129:
130: public final Object decodeFromInputStream(InputStream s)
131: throws IOException {
132: throw new UnsupportedOperationException(CommonResourceBundle
133: .getInstance().getString("message.notImplemented"));
134: }
135:
136: public void encodeToOutputStream(Object data, OutputStream s)
137: throws IOException {
138: if (!(data instanceof byte[])) {
139: throw new IllegalArgumentException(CommonResourceBundle
140: .getInstance()
141: .getString("message.dataNotByteArray"));
142: }
143:
144: s.write((byte[]) data);
145: }
146:
147: public final Object convertFromCharacters(char[] ch, int start,
148: int length) {
149: if (length == 0) {
150: return new byte[0];
151: }
152:
153: StringBuffer encodedValue = removeWhitespace(ch, start, length);
154: int encodedLength = encodedValue.length();
155: if (encodedLength == 0) {
156: return new byte[0];
157: }
158:
159: int blockCount = encodedLength / 4;
160: int partialBlockLength = 3;
161:
162: if (encodedValue.charAt(encodedLength - 1) == '=') {
163: --partialBlockLength;
164: if (encodedValue.charAt(encodedLength - 2) == '=') {
165: --partialBlockLength;
166: }
167: }
168:
169: int valueLength = (blockCount - 1) * 3 + partialBlockLength;
170: byte[] value = new byte[valueLength];
171:
172: int idx = 0;
173: int encodedIdx = 0;
174: for (int i = 0; i < blockCount; ++i) {
175: int x1 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
176: int x2 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
177: int x3 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
178: int x4 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
179:
180: value[idx++] = (byte) ((x1 << 2) | (x2 >> 4));
181: if (idx < valueLength) {
182: value[idx++] = (byte) (((x2 & 0x0f) << 4) | (x3 >> 2));
183: }
184: if (idx < valueLength) {
185: value[idx++] = (byte) (((x3 & 0x03) << 6) | x4);
186: }
187: }
188:
189: return value;
190: }
191:
192: public final void convertToCharacters(Object data, StringBuffer s) {
193: if (data == null) {
194: return;
195: }
196: final byte[] value = (byte[]) data;
197:
198: convertToCharacters(value, 0, value.length, s);
199: }
200:
201: public final int getPrimtiveLengthFromOctetLength(int octetLength)
202: throws EncodingAlgorithmException {
203: return octetLength;
204: }
205:
206: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
207: return primitiveLength;
208: }
209:
210: public final void encodeToBytes(Object array, int astart,
211: int alength, byte[] b, int start) {
212: System.arraycopy((byte[]) array, astart, b, start, alength);
213: }
214:
215: public final void convertToCharacters(byte[] data, int offset,
216: int length, StringBuffer s) {
217: if (data == null) {
218: return;
219: }
220: final byte[] value = data;
221: if (length == 0) {
222: return;
223: }
224:
225: final int partialBlockLength = length % 3;
226: final int blockCount = (partialBlockLength != 0) ? length / 3 + 1
227: : length / 3;
228:
229: final int encodedLength = blockCount * 4;
230: final int originalBufferSize = s.length();
231: s.ensureCapacity(encodedLength + originalBufferSize);
232:
233: int idx = offset;
234: int lastIdx = offset + length;
235: for (int i = 0; i < blockCount; ++i) {
236: int b1 = value[idx++] & 0xFF;
237: int b2 = (idx < lastIdx) ? value[idx++] & 0xFF : 0;
238: int b3 = (idx < lastIdx) ? value[idx++] & 0xFF : 0;
239:
240: s.append(encodeBase64[b1 >> 2]);
241:
242: s.append(encodeBase64[((b1 & 0x03) << 4) | (b2 >> 4)]);
243:
244: s.append(encodeBase64[((b2 & 0x0f) << 2) | (b3 >> 6)]);
245:
246: s.append(encodeBase64[b3 & 0x3f]);
247: }
248:
249: switch (partialBlockLength) {
250: case 1:
251: s.setCharAt(originalBufferSize + encodedLength - 1, '=');
252: s.setCharAt(originalBufferSize + encodedLength - 2, '=');
253: break;
254: case 2:
255: s.setCharAt(originalBufferSize + encodedLength - 1, '=');
256: break;
257: }
258: }
259: }
|