KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > algorithm > BASE64EncodingAlgorithm


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.algorithm;
41
42 import java.io.EOFException JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.OutputStream JavaDoc;
46 import java.nio.CharBuffer JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.List JavaDoc;
49 import org.jvnet.fastinfoset.EncodingAlgorithmException;
50 import com.sun.xml.fastinfoset.CommonResourceBundle;
51
52 public class BASE64EncodingAlgorithm extends BuiltInEncodingAlgorithm {
53     
54     protected static final char encodeBase64[] = {
55         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
56         'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
57         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
58         'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
59         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
60     };
61
62     protected static final int decodeBase64[] = {
63         /*'+'*/ 62,
64         -1, -1, -1,
65         /*'/'*/ 63,
66         /*'0'*/ 52,
67         /*'1'*/ 53,
68         /*'2'*/ 54,
69         /*'3'*/ 55,
70         /*'4'*/ 56,
71         /*'5'*/ 57,
72         /*'6'*/ 58,
73         /*'7'*/ 59,
74         /*'8'*/ 60,
75         /*'9'*/ 61,
76         -1, -1, -1, -1, -1, -1, -1,
77         /*'A'*/ 0,
78         /*'B'*/ 1,
79         /*'C'*/ 2,
80         /*'D'*/ 3,
81         /*'E'*/ 4,
82         /*'F'*/ 5,
83         /*'G'*/ 6,
84         /*'H'*/ 7,
85         /*'I'*/ 8,
86         /*'J'*/ 9,
87         /*'K'*/ 10,
88         /*'L'*/ 11,
89         /*'M'*/ 12,
90         /*'N'*/ 13,
91         /*'O'*/ 14,
92         /*'P'*/ 15,
93         /*'Q'*/ 16,
94         /*'R'*/ 17,
95         /*'S'*/ 18,
96         /*'T'*/ 19,
97         /*'U'*/ 20,
98         /*'V'*/ 21,
99         /*'W'*/ 22,
100         /*'X'*/ 23,
101         /*'Y'*/ 24,
102         /*'Z'*/ 25,
103         -1, -1, -1, -1, -1, -1,
104         /*'a'*/ 26,
105         /*'b'*/ 27,
106         /*'c'*/ 28,
107         /*'d'*/ 29,
108         /*'e'*/ 30,
109         /*'f'*/ 31,
110         /*'g'*/ 32,
111         /*'h'*/ 33,
112         /*'i'*/ 34,
113         /*'j'*/ 35,
114         /*'k'*/ 36,
115         /*'l'*/ 37,
116         /*'m'*/ 38,
117         /*'n'*/ 39,
118         /*'o'*/ 40,
119         /*'p'*/ 41,
120         /*'q'*/ 42,
121         /*'r'*/ 43,
122         /*'s'*/ 44,
123         /*'t'*/ 45,
124         /*'u'*/ 46,
125         /*'v'*/ 47,
126         /*'w'*/ 48,
127         /*'x'*/ 49,
128         /*'y'*/ 50,
129         /*'z'*/ 51
130     };
131
132     public final Object JavaDoc decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
133         final byte[] data = new byte[length];
134         System.arraycopy(b, start, data, 0, length);
135         return data;
136     }
137     
138     public final Object JavaDoc decodeFromInputStream(InputStream JavaDoc s) throws IOException JavaDoc {
139         throw new UnsupportedOperationException JavaDoc(CommonResourceBundle.getInstance().getString("message.notImplemented"));
140     }
141     
142     
143     public void encodeToOutputStream(Object JavaDoc data, OutputStream JavaDoc s) throws IOException JavaDoc {
144         if (!(data instanceof byte[])) {
145             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotByteArray"));
146         }
147         
148         s.write((byte[])data);
149     }
150     
151     public final Object JavaDoc convertFromCharacters(char[] ch, int start, int length) {
152         if (length == 0) {
153             return new byte[0];
154         }
155         
156         StringBuffer JavaDoc encodedValue = removeWhitespace(ch, start, length);
157         int encodedLength = encodedValue.length();
158         if (encodedLength == 0) {
159             return new byte[0];
160         }
161         
162         int blockCount = encodedLength / 4;
163         int partialBlockLength = 3;
164
165         if (encodedValue.charAt(encodedLength - 1) == '=') {
166             --partialBlockLength;
167             if (encodedValue.charAt(encodedLength - 2) == '=') {
168                 --partialBlockLength;
169             }
170         }
171
172         int valueLength = (blockCount - 1) * 3 + partialBlockLength;
173         byte[] value = new byte[valueLength];
174
175         int idx = 0;
176         int encodedIdx = 0;
177         for (int i = 0; i < blockCount; ++i) {
178             int x1 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
179             int x2 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
180             int x3 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
181             int x4 = decodeBase64[encodedValue.charAt(encodedIdx++) - '+'];
182
183             value[idx++] = (byte) ((x1 << 2) | (x2 >> 4));
184             if (idx < valueLength) {
185                 value[idx++] = (byte) (((x2 & 0x0f) << 4) | (x3 >> 2));
186             }
187             if (idx < valueLength) {
188                 value[idx++] = (byte) (((x3 & 0x03) << 6) | x4);
189             }
190         }
191
192         return value;
193     }
194     
195     public final void convertToCharacters(Object JavaDoc data, StringBuffer JavaDoc s) {
196         if (data == null) {
197             return;
198         }
199         final byte[] value = (byte[]) data;
200         if (value.length == 0) {
201             return;
202         }
203         
204         final int partialBlockLength = value.length % 3;
205         final int blockCount = (partialBlockLength != 0) ?
206             value.length / 3 + 1 :
207             value.length / 3;
208
209         final int encodedLength = blockCount * 4;
210         s.ensureCapacity(encodedLength);
211
212         int idx = 0;
213         char encodedChar;
214         for (int i = 0; i < blockCount; ++i) {
215             int b1 = value[idx++] & 0xFF;
216             int b2 = (idx < value.length) ? value[idx++] & 0xFF : 0;
217             int b3 = (idx < value.length) ? value[idx++] & 0xFF : 0;
218
219             s.append(encodeBase64[b1 >> 2]);
220
221             s.append(encodeBase64[((b1 & 0x03) << 4) | (b2 >> 4)]);
222
223             s.append(encodeBase64[((b2 & 0x0f) << 2) | (b3 >> 6)]);
224
225             s.append(encodeBase64[b3 & 0x3f]);
226         }
227
228         switch (partialBlockLength) {
229             case 1 :
230                 s.setCharAt(encodedLength - 1, '=');
231                 s.setCharAt(encodedLength - 2, '=');
232                 break;
233             case 2 :
234                 s.setCharAt(encodedLength - 1, '=');
235                 break;
236         }
237     }
238     
239     
240         
241     public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
242         return octetLength;
243     }
244
245     public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
246         return primitiveLength;
247     }
248     
249     public final void encodeToBytes(Object JavaDoc array, int astart, int alength, byte[] b, int start) {
250         System.arraycopy((byte[])array, astart, b, start, alength);
251     }
252 }
253
Popular Tags