1 19 package com.mysql.jdbc; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.UnsupportedEncodingException ; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.StringTokenizer ; 27 28 29 35 public class StringUtils { 36 private static final int BYTE_RANGE = (1 + Byte.MAX_VALUE) - Byte.MIN_VALUE; 37 private static byte[] allBytes = new byte[BYTE_RANGE]; 38 private static char[] byteToChars = new char[BYTE_RANGE]; 39 40 static { 41 for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { 42 allBytes[i - Byte.MIN_VALUE] = (byte) i; 43 } 44 45 String allBytesString = new String (allBytes, 0, 46 Byte.MAX_VALUE - Byte.MIN_VALUE); 47 48 int allBytesStringLen = allBytesString.length(); 49 50 for (int i = 0; 51 (i < (Byte.MAX_VALUE - Byte.MIN_VALUE)) 52 && (i < allBytesStringLen); i++) { 53 byteToChars[i] = allBytesString.charAt(i); 54 } 55 } 56 57 69 public static final byte[] getBytes(String s, String encoding) 70 throws UnsupportedEncodingException { 71 SingleByteCharsetConverter converter = SingleByteCharsetConverter 72 .getInstance(encoding); 73 74 return getBytes(s, converter, encoding); 75 } 76 77 90 public static final byte[] getBytes(String s, 91 SingleByteCharsetConverter converter, String encoding) 92 throws UnsupportedEncodingException { 93 byte[] b = null; 94 95 if (converter != null) { 96 b = converter.toBytes(s); 97 } else if (encoding == null) { 98 b = s.getBytes(); 99 } else { 100 b = s.getBytes(encoding); 101 102 if (encoding.equalsIgnoreCase("SJIS") 103 || encoding.equalsIgnoreCase("BIG5") 104 || encoding.equalsIgnoreCase("GBK")) { 105 b = escapeSJISByteStream(b, s, 0, s.length()); 106 } 107 } 108 109 return b; 110 } 111 112 125 public static final byte[] getBytes(String s, 126 SingleByteCharsetConverter converter, String encoding, int offset, 127 int length) throws UnsupportedEncodingException { 128 byte[] b = null; 129 130 if (converter != null) { 131 b = converter.toBytes(s, offset, length); 132 } else if (encoding == null) { 133 byte[] temp = s.getBytes(); 134 135 b = new byte[length]; 136 System.arraycopy(temp, offset, b, 0, length); 137 } else { 138 byte[] temp = s.getBytes(encoding); 139 140 b = new byte[length]; 141 System.arraycopy(temp, offset, b, 0, length); 142 143 if (encoding.equalsIgnoreCase("SJIS") 144 || encoding.equalsIgnoreCase("BIG5") 145 || encoding.equalsIgnoreCase("GBK")) { 146 b = escapeSJISByteStream(b, s, offset, length); 147 } 148 } 149 150 return b; 151 } 152 153 159 public static final void dumpAsHex(byte[] byteBuffer, int length) { 160 int p = 0; 161 int rows = length / 8; 162 163 for (int i = 0; i < rows; i++) { 164 int ptemp = p; 165 166 for (int j = 0; j < 8; j++) { 167 String hexVal = Integer.toHexString((int) byteBuffer[ptemp] 168 & 0xff); 169 170 if (hexVal.length() == 1) { 171 hexVal = "0" + hexVal; 172 } 173 174 System.out.print(hexVal + " "); 175 ptemp++; 176 } 177 178 System.out.print(" "); 179 180 for (int j = 0; j < 8; j++) { 181 if ((byteBuffer[p] > 32) && (byteBuffer[p] < 127)) { 182 System.out.print((char) byteBuffer[p] + " "); 183 } else { 184 System.out.print(". "); 185 } 186 187 p++; 188 } 189 190 System.out.println(); 191 } 192 193 int n = 0; 194 195 for (int i = p; i < length; i++) { 196 String hexVal = Integer.toHexString((int) byteBuffer[i] & 0xff); 197 198 if (hexVal.length() == 1) { 199 hexVal = "0" + hexVal; 200 } 201 202 System.out.print(hexVal + " "); 203 n++; 204 } 205 206 for (int i = n; i < 8; i++) { 207 System.out.print(" "); 208 } 209 210 System.out.print(" "); 211 212 for (int i = p; i < length; i++) { 213 if ((byteBuffer[i] > 32) && (byteBuffer[i] < 127)) { 214 System.out.print((char) byteBuffer[i] + " "); 215 } else { 216 System.out.print(". "); 217 } 218 } 219 220 System.out.println(); 221 } 222 223 230 public static final String toAsciiString(byte[] buffer) { 231 return toAsciiString(buffer, 0, buffer.length); 232 } 233 234 243 public static final String toAsciiString(byte[] buffer, int startPos, 244 int length) { 245 char[] charArray = new char[length]; 246 int readpoint = startPos; 247 248 for (int i = 0; i < length; i++) { 249 charArray[i] = (char) buffer[readpoint]; 250 readpoint++; 251 } 252 253 return new String (charArray); 254 } 255 256 267 public static byte[] escapeSJISByteStream(byte[] origBytes, 268 String origString, int offset, int length) { 269 if ((origBytes == null) || (origBytes.length == 0)) { 270 return origBytes; 271 } 272 273 int bytesLen = origBytes.length; 274 int bufIndex = 0; 275 int strIndex = 0; 276 277 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream (bytesLen); 278 279 while (true) { 280 if (strIndex<origString.length() && origString.charAt(strIndex) == '\\') { 281 bytesOut.write(origBytes[bufIndex++]); 283 } else { 285 int loByte = (int) origBytes[bufIndex]; 287 288 if (loByte < 0) { 289 loByte += 256; } 291 292 bytesOut.write(loByte); 294 295 if (((loByte >= 0x81) && (loByte <= 0x9F)) 312 || ((loByte >= 0xE0) && (loByte <= 0xFC))) { 313 if (bufIndex < (bytesLen - 1)) { 314 int hiByte = (int) origBytes[bufIndex + 1]; 315 316 if (hiByte < 0) { 317 hiByte += 256; } 319 320 bytesOut.write(hiByte); 323 bufIndex++; 324 325 if (hiByte == 0x5C) { 327 bytesOut.write(hiByte); 328 } 329 } 330 } else if (loByte == 0x5c) { 331 if (bufIndex < (bytesLen - 1)) { 332 int hiByte = (int) origBytes[bufIndex + 1]; 333 334 if (hiByte < 0) { 335 hiByte += 256; } 337 338 if (hiByte == 0x62) { 339 bytesOut.write(0x5c); 341 bytesOut.write(0x62); 342 bufIndex++; 343 } 344 } 345 } 346 347 bufIndex++; 348 349 350 } 351 352 if (bufIndex >= bytesLen) { 353 break; 355 } 356 357 strIndex++; 358 } 359 360 return bytesOut.toByteArray(); 361 } 362 363 370 public static char firstNonWsCharUc(String searchIn) { 371 if (searchIn == null) { 372 return 0; 373 } 374 375 int length = searchIn.length(); 376 377 for (int i = 0; i < length; i++) { 378 char c = searchIn.charAt(i); 379 380 if (!Character.isWhitespace(c)) { 381 return Character.toUpperCase(c); 382 } 383 } 384 385 return 0; 386 } 387 388 396 public static int indexOfIgnoreCase(String searchIn, String searchFor) { 397 if ((searchIn == null) || (searchFor == null)) { 398 return -1; 399 } 400 401 int patternLength = searchFor.length(); 402 int stringLength = searchIn.length(); 403 404 int i = 0; 405 406 if (patternLength == 0) { 407 return -1; 408 } 409 410 char firstCharOfPattern = Character.toUpperCase(searchFor.charAt(0)); 412 413 lookForFirstChar: 414 while (true) { 415 while ((i <= stringLength) 416 && (Character.toUpperCase(searchIn.charAt(i)) != firstCharOfPattern)) { 417 i++; 418 } 419 420 if (i > stringLength) { 421 return -1; 422 } 423 424 int j = i + 1; 425 int end = (j + patternLength) - 1; 426 427 int k = 1; 429 while (j < end) { 430 if (Character.toUpperCase(searchIn.charAt(j++)) != Character 431 .toUpperCase(searchFor.charAt(k++))) { 432 i++; 433 434 continue lookForFirstChar; 436 } 437 } 438 439 return i; } 441 } 442 443 454 public static final List split(String stringToSplit, String delimitter, 455 boolean trim) { 456 if (stringToSplit == null) { 457 return new ArrayList (); 458 } 459 460 if (delimitter == null) { 461 throw new IllegalArgumentException (); 462 } 463 464 StringTokenizer tokenizer = new StringTokenizer (stringToSplit, 465 delimitter, false); 466 467 List splitTokens = new ArrayList (tokenizer.countTokens()); 468 469 while (tokenizer.hasMoreTokens()) { 470 String token = tokenizer.nextToken(); 471 472 if (trim) { 473 token = token.trim(); 474 } 475 476 splitTokens.add(token); 477 } 478 479 return splitTokens; 480 } 481 482 492 public static boolean startsWithIgnoreCase(String searchIn, String searchFor) { 493 return startsWithIgnoreCase(searchIn, 0, searchFor); 494 } 495 496 507 public static boolean startsWithIgnoreCase(String searchIn, int startAt, 508 String searchFor) { 509 return searchIn.regionMatches(true, 0, searchFor, startAt, 510 searchFor.length()); 511 } 512 513 522 public static boolean startsWithIgnoreCaseAndWs(String searchIn, 523 String searchFor) { 524 int beginPos = 0; 525 526 int inLength = searchIn.length(); 527 528 for (beginPos = 0; beginPos < inLength; beginPos++) { 529 if (!Character.isWhitespace(searchIn.charAt(beginPos))) { 530 break; 531 } 532 } 533 534 return startsWithIgnoreCase(searchIn, beginPos, searchFor); 535 } 536 } 537 | Popular Tags |