1 2 24 25 26 27 28 package com.lutris.util; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.DataOutputStream ; 31 import java.io.IOException ; 32 import java.text.CharacterIterator ; 33 import java.text.StringCharacterIterator ; 34 35 46 public class Convert { 47 50 private static final char[] HexChars = { 51 '0', '1', '2', '3', '4', '5', '6', '7', 52 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 53 }; 54 55 59 private static final char[] Base64Chars = { 60 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 61 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 62 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 63 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 64 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 65 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 66 'w', 'x', 'y', 'z', '0', '1', '2', '3', 67 '4', '5', '6', '7', '8', '9', '+', '/', 68 '=' 69 }; 70 71 84 public static final String toHexString(long value, int len, char pad) { 85 StringBuffer sb = new StringBuffer (Long.toHexString(value)); 86 int npad = len - sb.length(); 87 while (npad-- > 0) sb.insert(0, pad); 88 return new String (sb); 89 } 90 91 101 public static final String toHexString(byte[] bytes) { 102 StringBuffer sb = new StringBuffer (); 103 int i; 104 for (i=0; i < bytes.length; i++) { 105 sb.append(HexChars[(bytes[i] >> 4) & 0xf]); 106 sb.append(HexChars[bytes[i] & 0xf]); 107 } 108 return new String (sb); 109 } 110 111 125 public static final String toBase64String(byte[] bytes) { 126 return toBase64String(bytes, Base64Chars); 127 } 128 129 130 147 public static final String toBase64String(byte[] bytes, char[] chars) { 148 StringBuffer sb = new StringBuffer (); 149 int len = bytes.length, i=0, ival; 150 while (len >= 3) { 151 ival = ((int)bytes[i++] + 256) & 0xff; 152 ival <<= 8; 153 ival += ((int)bytes[i++] + 256) & 0xff; 154 ival <<= 8; 155 ival += ((int)bytes[i++] + 256) & 0xff; 156 len -= 3; 157 sb.append(chars[(ival >> 18) & 63]); 158 sb.append(chars[(ival >> 12) & 63]); 159 sb.append(chars[(ival >> 6) & 63]); 160 sb.append(chars[ival & 63]); 161 } 162 switch (len) { 163 case 0: break; 165 case 1: ival = ((int)bytes[i++] + 256) & 0xff; 167 ival <<= 16; 168 sb.append(chars[(ival >> 18) & 63]); 169 sb.append(chars[(ival >> 12) & 63]); 170 sb.append(chars[64]); 171 sb.append(chars[64]); 172 break; 173 case 2: ival = ((int)bytes[i++] + 256) & 0xff; 175 ival <<= 8; 176 ival += ((int)bytes[i] + 256) & 0xff; 177 ival <<= 8; 178 sb.append(chars[(ival >> 18) & 63]); 179 sb.append(chars[(ival >> 12) & 63]); 180 sb.append(chars[(ival >> 6) & 63]); 181 sb.append(chars[64]); 182 break; 183 } 184 return new String (sb); 185 } 186 187 195 public static final byte[] fromBase64String(String s) { 196 try { 197 StringCharacterIterator iter = new StringCharacterIterator (s); 198 ByteArrayOutputStream bytestr = new ByteArrayOutputStream (); 199 DataOutputStream outstr = new DataOutputStream (bytestr); 200 char c; 201 int d, i, group; 202 int[] bgroup = new int[4]; 203 decode: for (i=0, group=0, c = iter.first(); 204 c != CharacterIterator.DONE; 205 c = iter.next()) 206 { 207 switch (c) { 208 case 'A': d = 0; break; case 'B': d = 1; break; 209 case 'C': d = 2; break; case 'D': d = 3; break; 210 case 'E': d = 4; break; case 'F': d = 5; break; 211 case 'G': d = 6; break; case 'H': d = 7; break; 212 case 'I': d = 8; break; case 'J': d = 9; break; 213 case 'K': d = 10; break; case 'L': d = 11; break; 214 case 'M': d = 12; break; case 'N': d = 13; break; 215 case 'O': d = 14; break; case 'P': d = 15; break; 216 case 'Q': d = 16; break; case 'R': d = 17; break; 217 case 'S': d = 18; break; case 'T': d = 19; break; 218 case 'U': d = 20; break; case 'V': d = 21; break; 219 case 'W': d = 22; break; case 'X': d = 23; break; 220 case 'Y': d = 24; break; case 'Z': d = 25; break; 221 case 'a': d = 26; break; case 'b': d = 27; break; 222 case 'c': d = 28; break; case 'd': d = 29; break; 223 case 'e': d = 30; break; case 'f': d = 31; break; 224 case 'g': d = 32; break; case 'h': d = 33; break; 225 case 'i': d = 34; break; case 'j': d = 35; break; 226 case 'k': d = 36; break; case 'l': d = 37; break; 227 case 'm': d = 38; break; case 'n': d = 39; break; 228 case 'o': d = 40; break; case 'p': d = 41; break; 229 case 'q': d = 42; break; case 'r': d = 43; break; 230 case 's': d = 44; break; case 't': d = 45; break; 231 case 'u': d = 46; break; case 'v': d = 47; break; 232 case 'w': d = 48; break; case 'x': d = 49; break; 233 case 'y': d = 50; break; case 'z': d = 51; break; 234 case '0': d = 52; break; case '1': d = 53; break; 235 case '2': d = 54; break; case '3': d = 55; break; 236 case '4': d = 56; break; case '5': d = 57; break; 237 case '6': d = 58; break; case '7': d = 59; break; 238 case '8': d = 60; break; case '9': d = 61; break; 239 case '+': d = 62; break; case '/': d = 63; break; 240 default: 241 break decode; } 245 bgroup[i++] = d; 246 if (i >= 4) { 247 i = 0; 248 group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12)+ 249 ((bgroup[2] & 63) << 6) + (bgroup[3] & 63); 250 outstr.writeByte(((group >> 16) & 255)); 251 outstr.writeByte(((group >> 8) & 255)); 252 outstr.writeByte(group & 255); 253 } 254 } 255 switch (i) { 259 case 2: 260 group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12); 262 outstr.writeByte(((group >> 16) & 255)); 263 break; 264 case 3: 265 group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12)+ 267 ((bgroup[2] & 63) << 6); 268 outstr.writeByte(((group >> 16) & 255)); 269 outstr.writeByte(((group >> 8) & 255)); 270 break; 271 default: 272 break; 275 } 276 outstr.flush(); 277 return bytestr.toByteArray(); 278 } 279 catch (IOException e) {} return null; 281 } 282 } 283 284 | Popular Tags |