1 65 66 67 package org.hsqldb.lib; 68 69 import java.io.IOException ; 70 import java.io.InputStream ; 71 import java.io.InputStreamReader ; 72 import java.io.StringWriter ; 73 import java.io.UTFDataFormatException ; 74 75 86 87 public class StringConverter { 89 90 private static final byte[] HEXBYTES = { 91 (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', 92 (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', 93 (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f' 94 }; 95 private static final String HEXINDEX = "0123456789abcdef0123456789ABCDEF"; 96 97 101 byte[] stringToFullByteArray(String s) { 102 103 int length = s.length(); 104 byte[] buffer = new byte[length * 2]; 105 int c; 106 107 for (int i = 0; i < length; i++) { 108 c = s.charAt(i); 109 buffer[i * 2] = (byte) ((c & 0x0000ff00) >> 8); 110 buffer[i * 2 + 1] = (byte) (c & 0x000000ff); 111 } 112 113 return buffer; 114 } 115 116 125 public static byte[] hexToByte(String s) throws IOException { 126 127 int l = s.length() / 2; 128 byte[] data = new byte[l]; 129 int j = 0; 130 131 if (s.length() % 2 != 0) { 132 throw new IOException ( 133 "hexadecimal string with odd number of characters"); 134 } 135 136 for (int i = 0; i < l; i++) { 137 char c = s.charAt(j++); 138 int n, b; 139 140 n = HEXINDEX.indexOf(c); 141 142 if (n == -1) { 143 throw new IOException ( 144 "hexadecimal string contains non hex character"); 145 } 146 147 b = (n & 0xf) << 4; 148 c = s.charAt(j++); 149 n = HEXINDEX.indexOf(c); 150 b += (n & 0xf); 151 data[i] = (byte) b; 152 } 153 154 return data; 155 } 156 157 165 public static String byteToHex(byte[] b) { 166 167 int len = b.length; 168 char[] s = new char[len * 2]; 169 170 for (int i = 0, j = 0; i < len; i++) { 171 int c = ((int) b[i]) & 0xff; 172 173 s[j++] = (char) HEXBYTES[c >> 4 & 0xf]; 174 s[j++] = (char) HEXBYTES[c & 0xf]; 175 } 176 177 return new String (s); 178 } 179 180 187 public static void writeHex(byte[] o, int from, byte[] b) { 188 189 int len = b.length; 190 191 for (int i = 0; i < len; i++) { 192 int c = ((int) b[i]) & 0xff; 193 194 o[from++] = HEXBYTES[c >> 4 & 0xf]; 195 o[from++] = HEXBYTES[c & 0xf]; 196 } 197 } 198 199 public static String byteToString(byte[] b, String charset) { 200 201 try { 202 return (charset == null) ? new String (b) 203 : new String (b, charset); 204 } catch (Exception e) {} 205 206 return null; 207 } 208 209 217 public static String unicodeToHexString(String s) { 218 219 HsqlByteArrayOutputStream bout = new HsqlByteArrayOutputStream(); 220 221 writeUTF(s, bout); 222 223 return byteToHex(bout.toByteArray()); 224 } 225 226 230 254 public static int unicodeToAscii(HsqlByteArrayOutputStream b, String s, 255 boolean doubleSingleQuotes) { 256 257 int count = 0; 258 259 if ((s == null) || (s.length() == 0)) { 260 return 0; 261 } 262 263 int len = s.length(); 264 265 for (int i = 0; i < len; i++) { 266 char c = s.charAt(i); 267 268 if (c == '\\') { 269 if ((i < len - 1) && (s.charAt(i + 1) == 'u')) { 270 b.write(c); b.write('u'); 272 b.write('0'); 273 b.write('0'); 274 b.write('5'); 275 b.write('c'); 276 277 count += 6; 278 } else { 279 b.write(c); 280 281 count++; 282 } 283 } else if ((c >= 0x0020) && (c <= 0x007f)) { 284 b.write(c); 286 count++; 287 288 if (c == '\'' && doubleSingleQuotes) { 289 b.write(c); 290 291 count++; 292 } 293 } else { 294 b.write('\\'); 295 b.write('u'); 296 b.write(HEXBYTES[(c >> 12) & 0xf]); 297 b.write(HEXBYTES[(c >> 8) & 0xf]); 298 b.write(HEXBYTES[(c >> 4) & 0xf]); 299 b.write(HEXBYTES[c & 0xf]); 300 301 count += 6; 302 } 303 } 304 305 return count; 306 } 307 308 313 325 public static String asciiToUnicode(byte[] s, int offset, int length) { 326 327 if (length == 0) { 328 return ""; 329 } 330 331 char[] b = new char[length]; 332 int j = 0; 333 334 for (int i = 0; i < length; i++) { 335 byte c = s[offset + i]; 336 337 if (c == '\\' && i < length - 5) { 338 byte c1 = s[offset + i + 1]; 339 340 if (c1 == 'u') { 341 i++; 342 343 int k = HEXINDEX.indexOf(s[offset + (++i)]) << 12; 345 346 k += HEXINDEX.indexOf(s[offset + (++i)]) << 8; 347 k += HEXINDEX.indexOf(s[offset + (++i)]) << 4; 348 k += HEXINDEX.indexOf(s[offset + (++i)]); 349 b[j++] = (char) k; 350 } else { 351 b[j++] = (char) c; 352 } 353 } else { 354 b[j++] = (char) c; 355 } 356 } 357 358 return new String (b, 0, j); 359 } 360 361 public static String asciiToUnicode(String s) { 362 363 if ((s == null) || (s.indexOf("\\u") == -1)) { 364 return s; 365 } 366 367 int len = s.length(); 368 char[] b = new char[len]; 369 int j = 0; 370 371 for (int i = 0; i < len; i++) { 372 char c = s.charAt(i); 373 374 if (c == '\\' && i < len - 5) { 375 char c1 = s.charAt(i + 1); 376 377 if (c1 == 'u') { 378 i++; 379 380 int k = HEXINDEX.indexOf(s.charAt(++i)) << 12; 382 383 k += HEXINDEX.indexOf(s.charAt(++i)) << 8; 384 k += HEXINDEX.indexOf(s.charAt(++i)) << 4; 385 k += HEXINDEX.indexOf(s.charAt(++i)); 386 b[j++] = (char) k; 387 } else { 388 b[j++] = c; 389 } 390 } else { 391 b[j++] = c; 392 } 393 } 394 395 return new String (b, 0, j); 396 } 397 398 public static String readUTF(byte[] bytearr, int offset, 399 int length) throws IOException { 400 401 char[] buf = new char[length]; 402 403 return readUTF(bytearr, offset, length, buf); 404 } 405 406 public static String readUTF(byte[] bytearr, int offset, int length, 407 char[] buf) throws IOException { 408 409 int bcount = 0; 410 int c, char2, char3; 411 int count = 0; 412 413 while (count < length) { 414 c = (int) bytearr[offset + count]; 415 416 if (bcount == buf.length) { 417 buf = (char[]) ArrayUtil.resizeArray(buf, length); 418 } 419 420 if (c > 0) { 421 422 423 count++; 424 425 buf[bcount++] = (char) c; 426 427 continue; 428 } 429 430 c &= 0xff; 431 432 switch (c >> 4) { 433 434 case 12 : 435 case 13 : 436 437 438 count += 2; 439 440 if (count > length) { 441 throw new UTFDataFormatException (); 442 } 443 444 char2 = (int) bytearr[offset + count - 1]; 445 446 if ((char2 & 0xC0) != 0x80) { 447 throw new UTFDataFormatException (); 448 } 449 450 buf[bcount++] = (char) (((c & 0x1F) << 6) 451 | (char2 & 0x3F)); 452 break; 453 454 case 14 : 455 456 457 count += 3; 458 459 if (count > length) { 460 throw new UTFDataFormatException (); 461 } 462 463 char2 = (int) bytearr[offset + count - 2]; 464 char3 = (int) bytearr[offset + count - 1]; 465 466 if (((char2 & 0xC0) != 0x80) 467 || ((char3 & 0xC0) != 0x80)) { 468 throw new UTFDataFormatException (); 469 } 470 471 buf[bcount++] = (char) (((c & 0x0F) << 12) 472 | ((char2 & 0x3F) << 6) 473 | ((char3 & 0x3F) << 0)); 474 break; 475 476 default : 477 478 479 throw new UTFDataFormatException (); 480 } 481 } 482 483 return new String (buf, 0, bcount); 485 } 486 487 495 public static int writeUTF(String str, HsqlByteArrayOutputStream out) { 496 497 int strlen = str.length(); 498 int c, 499 count = 0; 500 501 for (int i = 0; i < strlen; i++) { 502 c = str.charAt(i); 503 504 if (c >= 0x0001 && c <= 0x007F) { 505 out.write(c); 506 507 count++; 508 } else if (c > 0x07FF) { 509 out.write(0xE0 | ((c >> 12) & 0x0F)); 510 out.write(0x80 | ((c >> 6) & 0x3F)); 511 out.write(0x80 | ((c >> 0) & 0x3F)); 512 513 count += 3; 514 } else { 515 out.write(0xC0 | ((c >> 6) & 0x1F)); 516 out.write(0x80 | ((c >> 0) & 0x3F)); 517 518 count += 2; 519 } 520 } 521 522 return count; 523 } 524 525 public static int getUTFSize(String s) { 526 527 int len = (s == null) ? 0 528 : s.length(); 529 int l = 0; 530 531 for (int i = 0; i < len; i++) { 532 int c = s.charAt(i); 533 534 if ((c >= 0x0001) && (c <= 0x007F)) { 535 l++; 536 } else if (c > 0x07FF) { 537 l += 3; 538 } else { 539 l += 2; 540 } 541 } 542 543 return l; 544 } 545 546 549 public static String inputStreamToString(InputStream x, 550 int length) throws IOException { 551 552 InputStreamReader in = new InputStreamReader (x); 553 StringWriter writer = new StringWriter (); 554 int blocksize = 8 * 1024; 555 char[] buffer = new char[blocksize]; 556 557 for (int left = length; left > 0; ) { 558 int read = in.read(buffer, 0, left > blocksize ? blocksize 559 : left); 560 561 if (read == -1) { 562 break; 563 } 564 565 writer.write(buffer, 0, read); 566 567 left -= read; 568 } 569 570 writer.close(); 571 572 return writer.toString(); 573 } 574 575 577 587 public static String toQuotedString(String s, char quoteChar, 588 boolean extraQuote) { 589 590 if (s == null) { 591 return null; 592 } 593 594 int count = extraQuote ? count(s, quoteChar) 595 : 0; 596 int len = s.length(); 597 char[] b = new char[2 + count + len]; 598 int i = 0; 599 int j = 0; 600 601 b[j++] = quoteChar; 602 603 for (; i < len; i++) { 604 char c = s.charAt(i); 605 606 b[j++] = c; 607 608 if (extraQuote && c == quoteChar) { 609 b[j++] = c; 610 } 611 } 612 613 b[j] = quoteChar; 614 615 return new String (b); 616 } 617 618 625 static int count(final String s, final char c) { 626 627 int pos = 0; 628 int count = 0; 629 630 if (s != null) { 631 while ((pos = s.indexOf(c, pos)) > -1) { 632 count++; 633 pos++; 634 } 635 } 636 637 return count; 638 } 639 } 640 | Popular Tags |