1 27 package org.objectweb.jac.util; 28 29 import java.io.*; 31 32 39 public class Base64 { 40 41 48 static public char[] encode(byte[] data) 49 { 50 char[] out = new char[((data.length + 2) / 3) * 4]; 51 52 for (int i=0, index=0; i<data.length; i+=3, index+=4) { 57 boolean quad = false; 58 boolean trip = false; 59 60 int val = (0xFF & (int) data[i]); 61 val <<= 8; 62 if ((i+1) < data.length) { 63 val |= (0xFF & (int) data[i+1]); 64 trip = true; 65 } 66 val <<= 8; 67 if ((i+2) < data.length) { 68 val |= (0xFF & (int) data[i+2]); 69 quad = true; 70 } 71 out[index+3] = alphabet[(quad? (val & 0x3F): 64)]; 72 val >>= 6; 73 out[index+2] = alphabet[(trip? (val & 0x3F): 64)]; 74 val >>= 6; 75 out[index+1] = alphabet[val & 0x3F]; 76 val >>= 6; 77 out[index+0] = alphabet[val & 0x3F]; 78 } 79 return out; 80 } 81 82 public static String encodeToString(byte[] data) { 83 return new String (encode(data)); 84 } 85 86 97 static public byte[] decode(char[] data) 98 { 99 106 int tempLen = data.length; 107 for( int ix=0; ix<data.length; ix++ ) 108 { 109 if( (data[ix] > 255) || codes[ data[ix] ] < 0 ) 110 --tempLen; } 112 117 int len = (tempLen / 4) * 3; 118 if ((tempLen % 4) == 3) len += 2; 119 if ((tempLen % 4) == 2) len += 1; 120 121 byte[] out = new byte[len]; 122 123 124 125 int shift = 0; int accum = 0; int index = 0; 128 129 for (int ix=0; ix<data.length; ix++) 131 { 132 int value = (data[ix]>255)? -1: codes[ data[ix] ]; 133 134 if ( value >= 0 ) { 136 accum <<= 6; shift += 6; accum |= value; if ( shift >= 8 ) { 141 shift -= 8; out[index++] = (byte) ((accum >> shift) & 0xff); 144 } 145 } 146 } 153 154 if( index != out.length) 156 { 157 throw new Error ("Miscalculated data length (wrote " + index + " instead of " + out.length + ")"); 158 } 159 160 return out; 161 } 162 163 public static byte[] decode(String data) { 164 return decode(data.toCharArray()); 165 } 166 167 static private char[] alphabet = 171 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" 172 .toCharArray(); 173 174 static private byte[] codes = new byte[256]; 178 static { 179 for (int i=0; i<256; i++) codes[i] = -1; 180 for (int i = 'A'; i <= 'Z'; i++) codes[i] = (byte)( i - 'A'); 181 for (int i = 'a'; i <= 'z'; i++) codes[i] = (byte)(26 + i - 'a'); 182 for (int i = '0'; i <= '9'; i++) codes[i] = (byte)(52 + i - '0'); 183 codes['+'] = 62; 184 codes['/'] = 63; 185 } 186 187 188 189 190 195 public static void main(String [] args) 196 { 197 boolean decode = false; 198 199 if (args.length == 0) { 200 System.out.println("usage: java Base64 [-d[ecode]] filename"); 201 System.exit(0); 202 } 203 for (int i=0; i<args.length; i++) { 204 if ("-decode".equalsIgnoreCase(args[i])) decode = true; 205 else if ("-d".equalsIgnoreCase(args[i])) decode = true; 206 } 207 208 String filename = args[args.length-1]; 209 File file = new File(filename); 210 if (!file.exists()) { 211 System.out.println("Error: file '" + filename + "' doesn't exist!"); 212 System.exit(0); 213 } 214 215 if (decode) 216 { 217 char[] encoded = readChars(file); 218 byte[] decoded = decode(encoded); 219 writeBytes(file, decoded); 220 } 221 else 222 { 223 byte[] decoded = readBytes(file); 224 char[] encoded = encode(decoded); 225 writeChars(file, encoded); 226 } 227 } 228 229 private static byte[] readBytes(File file) 230 { 231 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 232 try 233 { 234 InputStream fis = new FileInputStream(file); 235 InputStream is = new BufferedInputStream(fis); 236 int count = 0; 237 byte[] buf = new byte[16384]; 238 while ((count=is.read(buf)) != -1) { 239 if (count > 0) baos.write(buf, 0, count); 240 } 241 is.close(); 242 } 243 catch (Exception e) { e.printStackTrace(); } 244 245 return baos.toByteArray(); 246 } 247 248 private static char[] readChars(File file) 249 { 250 CharArrayWriter caw = new CharArrayWriter(); 251 try 252 { 253 Reader fr = new FileReader(file); 254 Reader in = new BufferedReader(fr); 255 int count = 0; 256 char[] buf = new char[16384]; 257 while ((count=in.read(buf)) != -1) { 258 if (count > 0) caw.write(buf, 0, count); 259 } 260 in.close(); 261 } 262 catch (Exception e) { e.printStackTrace(); } 263 264 return caw.toCharArray(); 265 } 266 267 private static void writeBytes(File file, byte[] data) { 268 try { 269 OutputStream fos = new FileOutputStream(file); 270 OutputStream os = new BufferedOutputStream(fos); 271 os.write(data); 272 os.close(); 273 } 274 catch (Exception e) { e.printStackTrace(); } 275 } 276 277 private static void writeChars(File file, char[] data) { 278 try { 279 Writer fos = new FileWriter(file); 280 Writer os = new BufferedWriter(fos); 281 os.write(data); 282 os.close(); 283 } 284 catch (Exception e) { e.printStackTrace(); } 285 } 286 290 } 291 | Popular Tags |