KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > Base64


1 package org.jahia.utils;
2
3 /**
4  * Provides encoding of raw bytes to base64, and decoding of base64
5  * to raw bytes.
6  */

7 public class Base64 {
8
9     static char[] alphabet =
10         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
11         .toCharArray();
12
13     static byte[] codes = new byte[256];
14     static {
15         for (int i=0; i<256; i++) codes[i] = -1;
16         for (int i = 'A'; i <= 'Z'; i++) codes[i] = (byte)( i - 'A');
17         for (int i = 'a'; i <= 'z'; i++) codes[i] = (byte)(26 + i - 'a');
18         for (int i = '0'; i <= '9'; i++) codes[i] = (byte)(52 + i - '0');
19         codes['+'] = 62;
20         codes['/'] = 63;
21     }
22
23     //-------------------------------------------------------------------------
24
static public char[] encode(byte[] data) {
25         char[] out = new char[((data.length + 2) / 3) * 4];
26
27         for (int i=0, index=0; i<data.length; i+=3, index+=4) {
28             boolean quad = false;
29             boolean trip = false;
30
31             int val = (0xFF & (int) data[i]);
32             val <<= 8;
33             if ((i+1) < data.length) {
34                 val |= (0xFF & (int) data[i+1]);
35                 trip = true;
36             }
37             val <<= 8;
38             if ((i+2) < data.length) {
39                 val |= (0xFF & (int) data[i+2]);
40                 quad = true;
41             }
42             out[index+3] = alphabet[(quad? (val & 0x3F): 64)];
43             val >>= 6;
44             out[index+2] = alphabet[(trip? (val & 0x3F): 64)];
45             val >>= 6;
46             out[index+1] = alphabet[val & 0x3F];
47             val >>= 6;
48             out[index+0] = alphabet[val & 0x3F];
49         }
50         return out;
51     }
52
53     //-------------------------------------------------------------------------
54
static public byte[] decode(char[] data) {
55         int len = ((data.length + 3) / 4) * 3;
56         if (data.length>0 && data[len-1] == '=') --len;
57         if (data.length>0 && data[len-2] == '=') --len;
58         byte[] out = new byte[len];
59
60         int shift = 0; // # of excess bits stored in accum
61
int accum = 0; // excess bits
62
int index = 0;
63
64         for (int ix=0; ix<data.length; ix++)
65         {
66             int value = codes[ data[ix] & 0xFF ]; // ignore high byte of char
67
if ( value >= 0 ) { // skip over non-code
68
accum <<= 6; // bits shift up by 6 each time thru
69
shift += 6; // loop, with new bits being put in
70
accum |= value; // at the bottom.
71
if ( shift >= 8 ) { // whenever there are 8 or more shifted in,
72
shift -= 8; // write them out (from the top, leaving any
73
out[index++] = // excess at the bottom for next iteration.
74
(byte) ((accum >> shift) & 0xff);
75         } } }
76         if (index != out.length)
77             throw new Error JavaDoc("miscalculated data length!");
78
79         return out;
80     }
81     //-------------------------------------------------------------------------
82
static void compare(char[] b1, char[] b2) {
83         if (b1 == null || b2 == null) {
84             JahiaConsole.println("Base64.compare(char)","Null array!");
85         } else if (b1.length != b2.length) {
86             JahiaConsole.println("Base64.compare(char)","arrays are different lengths!");
87         } else for (int i=0; i<b1.length; i++) {
88             if (b1[i] != b2[i]) {
89                 JahiaConsole.println("Base64.compare(char)","arrays disagree at byte " + i);
90                 return;
91             }
92         }
93     }
94
95     //-------------------------------------------------------------------------
96
static void compare(byte[] b1, byte[] b2) {
97         if (b1 == null || b2 == null) {
98             JahiaConsole.println("Base64.compare(byte)","Null array!");
99             return;
100         }
101         if (b1.length != b2.length) {
102             JahiaConsole.println("Base64.compare(byte)","arrays are different lengths!");
103             return;
104         }
105         for (int i=0; i<b1.length; i++) {
106             if (b1[i] != b2[i]) {
107                 JahiaConsole.println("Base64.compare(byte)","arrays disagree at byte " + i);
108                 return;
109             }
110         }
111     }
112
113     //-------------------------------------------------------------------------
114
static String JavaDoc fromBytes(byte[] data) {
115         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(data.length*3);
116         for (int i=0; i<data.length; i++) {
117             if (i>0) buf.append(' ');
118             String JavaDoc hex = Integer.toHexString(0xff&data[i]);
119             if (hex.length() < 2) buf.append(' ');
120             buf.append(hex);
121         }
122         return new String JavaDoc(buf);
123     }
124
125     //-------------------------------------------------------------------------
126
static String JavaDoc fromBytes(char[] data) {
127         return new String JavaDoc(data);
128     }
129 }
130
Popular Tags