1 package org.jahia.utils; 2 3 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 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 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; int accum = 0; int index = 0; 63 64 for (int ix=0; ix<data.length; ix++) 65 { 66 int value = codes[ data[ix] & 0xFF ]; if ( value >= 0 ) { accum <<= 6; shift += 6; accum |= value; if ( shift >= 8 ) { shift -= 8; out[index++] = (byte) ((accum >> shift) & 0xff); 75 } } } 76 if (index != out.length) 77 throw new Error ("miscalculated data length!"); 78 79 return out; 80 } 81 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 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 static String fromBytes(byte[] data) { 115 StringBuffer buf = new StringBuffer (data.length*3); 116 for (int i=0; i<data.length; i++) { 117 if (i>0) buf.append(' '); 118 String hex = Integer.toHexString(0xff&data[i]); 119 if (hex.length() < 2) buf.append(' '); 120 buf.append(hex); 121 } 122 return new String (buf); 123 } 124 125 static String fromBytes(char[] data) { 127 return new String (data); 128 } 129 } 130 | Popular Tags |