KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > io > impl > Base64


1 package com.sun.syndication.io.impl;
2
3 /*
4  * Copyright 2004 Sun Microsystems, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may 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,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20 /**
21  * Encodes/decodes byte arrays and Strings into/from a base 64 String.
22  * <p>
23  * @author Alejandro Abdelnur
24  *
25  */

26 public class Base64 {
27
28     /**
29      * Encodes a String into a base 64 String. The resulting encoding is chunked at 76 bytes.
30      * <p>
31      * @param s String to encode.
32      * @return encoded string.
33      *
34      */

35     public static String JavaDoc encode(String JavaDoc s) {
36         byte[] sBytes = s.getBytes();
37         sBytes = encode(sBytes);
38         s = new String JavaDoc(sBytes);
39         return s;
40     }
41
42     /**
43      * Decodes a base 64 String into a String.
44      * <p>
45      * @param s String to decode.
46      * @return encoded string.
47      * @throws java.lang.IllegalArgumentException thrown if the given byte array was not valid com.sun.syndication.io.impl.Base64 encoding.
48      *
49      */

50     public static String JavaDoc decode(String JavaDoc s) throws IllegalArgumentException JavaDoc {
51         byte[] sBytes = s.getBytes();
52         sBytes = decode(sBytes);
53         s = new String JavaDoc(sBytes);
54         return s;
55     }
56
57
58     private static final byte[] ALPHASET =
59         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".getBytes();
60
61     private static final int I6O2 = 255 - 3;
62     private static final int O6I2 = 3;
63     private static final int I4O4 = 255 - 15;
64     private static final int O4I4 = 15;
65     private static final int I2O6 = 255 - 63;
66     private static final int O2I6 = 63;
67
68     /**
69      * Encodes a byte array into a base 64 byte array.
70      * <p>
71      * @param dData byte array to encode.
72      * @return encoded byte array.
73      *
74      */

75     public static byte[] encode(byte[] dData) {
76         if (dData==null) {
77             throw new IllegalArgumentException JavaDoc("Cannot encode null");
78         }
79         byte[] eData = new byte[((dData.length+2)/3)*4];
80
81         int eIndex = 0;
82         for (int i = 0; i<dData.length; i += 3) {
83             int d1;
84             int d2=0;
85             int d3=0;
86             int e1;
87             int e2;
88             int e3;
89             int e4;
90             int pad=0;
91
92             d1 = dData[i];
93             if ((i+1)<dData.length) {
94                 d2 = dData[i+1];
95                 if ((i+2)<dData.length) {
96                     d3 = dData[i+2];
97                 }
98                 else {
99                     pad =1;
100                 }
101             }
102             else {
103                 pad =2;
104             }
105
106             e1 = ALPHASET[(d1&I6O2)>>2];
107             e2 = ALPHASET[(d1&O6I2)<<4 | (d2&I4O4)>>4];
108             e3 = ALPHASET[(d2&O4I4)<<2 | (d3&I2O6)>>6];
109             e4 = ALPHASET[(d3&O2I6)];
110
111             eData[eIndex++] = (byte)e1;
112             eData[eIndex++] = (byte)e2;
113             eData[eIndex++] = (pad<2) ?(byte)e3 : (byte)'=';
114             eData[eIndex++] = (pad<1) ?(byte)e4 : (byte)'=';
115
116         }
117         return eData;
118     }
119
120     private final static int[] CODES = new int[256];
121
122     static {
123         for (int i=0;i<CODES.length;i++) {
124             CODES[i] = 64;
125         }
126         for (int i=0;i<ALPHASET.length;i++) {
127             CODES[ALPHASET[i]] = i;
128         }
129     }
130
131     /**
132      * Dencodes a com.sun.syndication.io.impl.Base64 byte array.
133      * <p>
134      * @param eData byte array to decode.
135      * @return decoded byte array.
136      * @throws java.lang.IllegalArgumentException thrown if the given byte array was not valid com.sun.syndication.io.impl.Base64 encoding.
137      *
138      */

139     public static byte[] decode(byte[] eData) {
140         if (eData==null) {
141             throw new IllegalArgumentException JavaDoc("Cannot decode null");
142         }
143         byte[] cleanEData = (byte[]) eData.clone();
144         int cleanELength = 0;
145         for (int i=0;i<eData.length;i++) {
146             if (eData[i]<256 && CODES[eData[i]]<64) {
147                 cleanEData[cleanELength++] = eData[i];
148             }
149         }
150
151         int dLength = (cleanELength/4)*3;
152         switch (cleanELength%4) {
153             case 3:
154                 dLength += 2;
155                 break;
156             case 2:
157                 dLength++;
158                 break;
159         }
160
161         byte[] dData = new byte[dLength];
162         int dIndex = 0;
163         for (int i=0;i<cleanELength;i+=4) {
164             if ((i+3)>cleanELength) {
165                 throw new IllegalArgumentException JavaDoc("byte array is not a valid com.sun.syndication.io.impl.Base64 encoding");
166             }
167             int e1 = CODES[cleanEData[i]];
168             int e2 = CODES[cleanEData[i+1]];
169             int e3 = CODES[cleanEData[i+2]];
170             int e4 = CODES[cleanEData[i+3]];
171             dData[dIndex++] = (byte) ((e1<<2)|(e2>>4));
172             if (dIndex<dData.length) {
173                 dData[dIndex++] = (byte) ((e2<<4) | (e3>>2));
174             }
175             if (dIndex<dData.length) {
176                 dData[dIndex++] = (byte) ((e3<<6) | (e4));
177             }
178         }
179         return dData;
180     }
181
182     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
183         String JavaDoc s =
184                 "\nPGRpdiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94aHRtbCI+V2UncmUgcHJvcG9zaW5nIDxhIGhy\n"+
185 "ZWY9Imh0dHA6Ly93d3cuZ29vZ2xlLmNvbS9jb3Jwb3JhdGUvc29mdHdhcmVfcHJpbmNpcGxlcy5odG1sIj5z\n"+
186 "b21lIGd1aWRlbGluZXMgPC9hPnRvIGhlbHAgY3VyYiB0aGUgcHJvYmxlbSBvZiBJbnRlcm5ldCBzb2Z0d2Fy\n"+
187 "ZSB0aGF0IGluc3RhbGxzIGl0c2VsZiB3aXRob3V0IHRlbGxpbmcgeW91LCBvciBiZWhhdmVzIGJhZGx5IG9u\n"+
188 "Y2UgaXQgZ2V0cyBvbiB5b3VyIGNvbXB1dGVyLiBXZSd2ZSBiZWVuIGhlYXJpbmcgYSBsb3Qgb2YgY29tcGxh\n"+
189 "aW50cyBhYm91dCB0aGlzIGxhdGVseSBhbmQgaXQgc2VlbXMgdG8gYmUgZ2V0dGluZyB3b3JzZS4gV2UgdGhp\n"+
190 "bmsgaXQncyBpbXBvcnRhbnQgdGhhdCB5b3UgcmV0YWluIGNvbnRyb2wgb2YgeW91ciBjb21wdXRlciBhbmQg\n"+
191 "dGhhdCB0aGVyZSBiZSBzb21lIGNsZWFyIHN0YW5kYXJkcyBpbiBvdXIgaW5kdXN0cnkuIExldCB1cyBrbm93\n"+
192 "IGlmIHlvdSB0aGluayB0aGVzZSBndWlkZWxpbmVzIGFyZSB1c2VmdWwgb3IgaWYgeW91IGhhdmUgc3VnZ2Vz\n"+
193 "dGlvbnMgdG8gaW1wcm92ZSB0aGVtLgo8YnIgLz4KPGJyIC8+Sm9uYXRoYW4gUm9zZW5iZXJnCjxiciAvPgo8\n"+
194 "L2Rpdj4K\n";
195
196         System.out.println(decode(s));
197     }
198 }
199
200
Popular Tags