1 18 package org.apache.geronimo.interop.util; 19 20 public class Base64Binary { 21 24 public static String toString(byte[] bytes) { 25 return toString(bytes, 0, bytes.length); 26 } 27 28 public static String toString(byte[] bytes, int offset, int length) { 29 StringBuffer s = new StringBuffer ((length * 4) / 3 + 1); 30 int n = offset + length; 31 for (int i = offset; i < n; i += 3) { 32 39 int value; 40 int chars; 41 if (i < n - 2) { 42 value = (0x00FF0000 & (bytes[i] << 16)) 43 | (0x0000FF00 & (bytes[i + 1] << 8)) 44 | (0x000000FF & bytes[i + 2]); 45 chars = 4; 46 } else if (i < n - 1) { 47 value = (0x00FF0000 & (bytes[i] << 16)) 48 | (0x0000FF00 & (bytes[i + 1] << 8)); 49 chars = 3; 50 } else { 51 value = (0x00FF0000 & (bytes[i] << 16)); 52 chars = 2; 53 } 54 while (chars-- > 0) { 55 int x = (0x00FC0000 & value) >> 18; 56 char c = getChar(x); 57 s.append(c); 58 value = value << 6; 59 } 60 if (i == n - 1) { 61 s.append("=="); 62 } else if (i == n - 2) { 63 s.append('='); 64 } 65 } 66 return s.toString(); 67 } 68 69 private static char getChar(int c) { 70 if (c < 26) { 71 return (char) ('A' + c); 72 } else if (c < 52) { 73 return (char) ('a' + (c - 26)); 74 } else if (c < 62) { 75 return (char) ('0' + (c - 52)); 76 } else if (c == 62) { 77 return '+'; 78 } else { 80 return '/'; 81 } 82 } 83 } 84 | Popular Tags |