1 20 21 package org.jivesoftware.smack.util; 22 23 import java.security.MessageDigest ; 24 import java.security.NoSuchAlgorithmException ; 25 import java.io.UnsupportedEncodingException ; 26 import java.util.Random ; 27 28 31 public class StringUtils { 32 33 private static final char[] QUOTE_ENCODE = """.toCharArray(); 34 private static final char[] AMP_ENCODE = "&".toCharArray(); 35 private static final char[] LT_ENCODE = "<".toCharArray(); 36 private static final char[] GT_ENCODE = ">".toCharArray(); 37 38 46 public static String parseName(String XMPPAddress) { 47 if (XMPPAddress == null) { 48 return null; 49 } 50 int atIndex = XMPPAddress.indexOf("@"); 51 if (atIndex <= 0) { 52 return ""; 53 } 54 else { 55 return XMPPAddress.substring(0, atIndex); 56 } 57 } 58 59 67 public static String parseServer(String XMPPAddress) { 68 if (XMPPAddress == null) { 69 return null; 70 } 71 int atIndex = XMPPAddress.indexOf("@"); 72 if (atIndex + 1 > XMPPAddress.length()) { 74 return ""; 75 } 76 int slashIndex = XMPPAddress.indexOf("/"); 77 if (slashIndex > 0) { 78 return XMPPAddress.substring(atIndex + 1, slashIndex); 79 } 80 else { 81 return XMPPAddress.substring(atIndex + 1); 82 } 83 } 84 85 93 public static String parseResource(String XMPPAddress) { 94 if (XMPPAddress == null) { 95 return null; 96 } 97 int slashIndex = XMPPAddress.indexOf("/"); 98 if (slashIndex + 1 > XMPPAddress.length() || slashIndex < 0) { 99 return ""; 100 } 101 else { 102 return XMPPAddress.substring(slashIndex + 1); 103 } 104 } 105 106 114 public static String parseBareAddress(String XMPPAddress) { 115 if (XMPPAddress == null) { 116 return null; 117 } 118 int slashIndex = XMPPAddress.indexOf("/"); 119 if (slashIndex < 0) { 120 return XMPPAddress; 121 } 122 else if (slashIndex == 0) { 123 return ""; 124 } 125 else { 126 return XMPPAddress.substring(0, slashIndex); 127 } 128 } 129 130 137 public static String escapeForXML(String string) { 138 if (string == null) { 139 return null; 140 } 141 char ch; 142 int i=0; 143 int last=0; 144 char[] input = string.toCharArray(); 145 int len = input.length; 146 StringBuffer out = new StringBuffer ((int)(len*1.3)); 147 for (; i < len; i++) { 148 ch = input[i]; 149 if (ch > '>') { 150 continue; 151 } 152 else if (ch == '<') { 153 if (i > last) { 154 out.append(input, last, i - last); 155 } 156 last = i + 1; 157 out.append(LT_ENCODE); 158 } 159 else if (ch == '>') { 160 if (i > last) { 161 out.append(input, last, i - last); 162 } 163 last = i + 1; 164 out.append(GT_ENCODE); 165 } 166 167 else if (ch == '&') { 168 if (i > last) { 169 out.append(input, last, i - last); 170 } 171 if (!(len > i + 5 173 && input[i + 1] == '#' 174 && Character.isDigit(input[i + 2]) 175 && Character.isDigit(input[i + 3]) 176 && Character.isDigit(input[i + 4]) 177 && input[i + 5] == ';')) { 178 last = i + 1; 179 out.append(AMP_ENCODE); 180 } 181 } 182 else if (ch == '"') { 183 if (i > last) { 184 out.append(input, last, i - last); 185 } 186 last = i + 1; 187 out.append(QUOTE_ENCODE); 188 } 189 } 190 if (last == 0) { 191 return string; 192 } 193 if (i > last) { 194 out.append(input, last, i - last); 195 } 196 return out.toString(); 197 } 198 199 202 private static MessageDigest digest = null; 203 204 220 public synchronized static String hash(String data) { 221 if (digest == null) { 222 try { 223 digest = MessageDigest.getInstance("SHA-1"); 224 } 225 catch (NoSuchAlgorithmException nsae) { 226 System.err.println("Failed to load the SHA-1 MessageDigest. " + 227 "Jive will be unable to function normally."); 228 } 229 } 230 try { 232 digest.update(data.getBytes("UTF-8")); 233 } 234 catch (UnsupportedEncodingException e) { 235 System.err.println(e); 236 } 237 return encodeHex(digest.digest()); 238 } 239 240 246 public static String encodeHex(byte[] bytes) { 247 StringBuffer hex = new StringBuffer (bytes.length * 2); 248 249 for (int i=0; i<bytes.length; i++) { 250 if (((int) bytes[i] & 0xff) < 0x10) { 251 hex.append("0"); 252 } 253 hex.append(Integer.toString((int) bytes[i] & 0xff, 16)); 254 } 255 256 return hex.toString(); 257 } 258 259 269 private static final int fillchar = '='; 270 private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 271 + "0123456789+/"; 272 273 279 public static String encodeBase64(String data) { 280 byte [] bytes = null; 281 try { 282 bytes = data.getBytes("ISO-8859-1"); 283 } 284 catch (UnsupportedEncodingException uee) { 285 uee.printStackTrace(); 286 } 287 return encodeBase64(bytes); 288 } 289 290 296 public static String encodeBase64(byte[] data) { 297 int c; 298 int len = data.length; 299 StringBuffer ret = new StringBuffer (((len / 3) + 1) * 4); 300 for (int i = 0; i < len; ++i) { 301 c = (data[i] >> 2) & 0x3f; 302 ret.append(cvt.charAt(c)); 303 c = (data[i] << 4) & 0x3f; 304 if (++i < len) 305 c |= (data[i] >> 4) & 0x0f; 306 307 ret.append(cvt.charAt(c)); 308 if (i < len) { 309 c = (data[i] << 2) & 0x3f; 310 if (++i < len) 311 c |= (data[i] >> 6) & 0x03; 312 313 ret.append(cvt.charAt(c)); 314 } 315 else { 316 ++i; 317 ret.append((char) fillchar); 318 } 319 320 if (i < len) { 321 c = data[i] & 0x3f; 322 ret.append(cvt.charAt(c)); 323 } 324 else { 325 ret.append((char) fillchar); 326 } 327 } 328 return ret.toString(); 329 } 330 331 337 public static byte[] decodeBase64(String data) { 338 byte [] bytes = null; 339 try { 340 bytes = data.getBytes("ISO-8859-1"); 341 return decodeBase64(bytes).getBytes("ISO-8859-1"); 342 } 343 catch (UnsupportedEncodingException uee) { 344 uee.printStackTrace(); 345 } 346 return new byte[] { }; 347 } 348 349 355 private static String decodeBase64(byte[] data) { 356 int c, c1; 357 int len = data.length; 358 StringBuffer ret = new StringBuffer ((len * 3) / 4); 359 for (int i = 0; i < len; ++i) { 360 c = cvt.indexOf(data[i]); 361 ++i; 362 c1 = cvt.indexOf(data[i]); 363 c = ((c << 2) | ((c1 >> 4) & 0x3)); 364 ret.append((char) c); 365 if (++i < len) { 366 c = data[i]; 367 if (fillchar == c) 368 break; 369 370 c = cvt.indexOf(c); 371 c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); 372 ret.append((char) c1); 373 } 374 375 if (++i < len) { 376 c1 = data[i]; 377 if (fillchar == c1) 378 break; 379 380 c1 = cvt.indexOf(c1); 381 c = ((c << 6) & 0xc0) | c1; 382 ret.append((char) c); 383 } 384 } 385 return ret.toString(); 386 } 387 388 393 private static Random randGen = new Random (); 394 395 401 private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + 402 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); 403 404 417 public static final String randomString(int length) { 418 if (length < 1) { 419 return null; 420 } 421 char [] randBuffer = new char[length]; 423 for (int i=0; i<randBuffer.length; i++) { 424 randBuffer[i] = numbersAndLetters[randGen.nextInt(71)]; 425 } 426 return new String (randBuffer); 427 } 428 429 private StringUtils() { 430 } 432 } | Popular Tags |