| 1 7 8 package java.lang; 9 10 import java.io.ObjectStreamClass ; 11 import java.io.ObjectStreamField ; 12 import java.io.UnsupportedEncodingException ; 13 import java.util.ArrayList ; 14 import java.util.Comparator ; 15 import java.util.Formatter ; 16 import java.util.Locale ; 17 import java.util.regex.Matcher ; 18 import java.util.regex.Pattern ; 19 import java.util.regex.PatternSyntaxException ; 20 21 22 89 90 public final class String 91 implements java.io.Serializable , Comparable <String >, CharSequence  92 { 93 94 private final char value[]; 95 96 97 private final int offset; 98 99 100 private final int count; 101 102 103 private int hash; 105 106 private static final long serialVersionUID = -6849794470754667710L; 107 108 120 private static final ObjectStreamField [] serialPersistentFields = 121 new ObjectStreamField [0]; 122 123 128 public String() { 129 this.offset = 0; 130 this.count = 0; 131 this.value = new char[0]; 132 } 133 134 143 public String(String original) { 144 int size = original.count; 145 char[] originalValue = original.value; 146 char[] v; 147 if (originalValue.length > size) { 148 v = new char[size]; 152 System.arraycopy(originalValue, original.offset, v, 0, size); 153 } else { 154 v = originalValue; 157 } 158 this.offset = 0; 159 this.count = size; 160 this.value = v; 161 } 162 163 172 public String(char value[]) { 173 int size = value.length; 174 char[] v = new char[size]; 175 System.arraycopy(value, 0, v, 0, size); 176 this.offset = 0; 177 this.count = size; 178 this.value = v; 179 } 180 181 197 public String(char value[], int offset, int count) { 198 if (offset < 0) { 199 throw new StringIndexOutOfBoundsException (offset); 200 } 201 if (count < 0) { 202 throw new StringIndexOutOfBoundsException (count); 203 } 204 if (offset > value.length - count) { 206 throw new StringIndexOutOfBoundsException (offset + count); 207 } 208 char[] v = new char[count]; 209 System.arraycopy(value, offset, v, 0, count); 210 this.offset = 0; 211 this.count = count; 212 this.value = v; 213 } 214 215 235 public String(int[] codePoints, int offset, int count) { 236 if (offset < 0) { 237 throw new StringIndexOutOfBoundsException (offset); 238 } 239 if (count < 0) { 240 throw new StringIndexOutOfBoundsException (count); 241 } 242 if (offset > codePoints.length - count) { 244 throw new StringIndexOutOfBoundsException (offset + count); 245 } 246 247 int expansion = 0; 248 int margin = 1; 249 char[] v = new char[count + margin]; 250 int x = offset; 251 int j = 0; 252 for (int i = 0; i < count; i++) { 253 int c = codePoints[x++]; 254 if (c < 0) { 255 throw new IllegalArgumentException (); 256 } 257 if (margin <= 0 && (j+1) >= v.length) { 258 if (expansion == 0) { 259 expansion = (((-margin + 1) * count) << 10) / i; 260 expansion >>= 10; 261 if (expansion <= 0) { 262 expansion = 1; 263 } 264 } else { 265 expansion *= 2; 266 } 267 char[] tmp = new char[Math.min(v.length+expansion, count*2)]; 268 margin = (tmp.length - v.length) - (count - i); 269 System.arraycopy(v, 0, tmp, 0, j); 270 v = tmp; 271 } 272 if (c < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 273 v[j++] = (char) c; 274 } else if (c <= Character.MAX_CODE_POINT) { 275 Character.toSurrogates(c, v, j); 276 j += 2; 277 margin--; 278 } else { 279 throw new IllegalArgumentException (); 280 } 281 } 282 this.offset = 0; 283 this.value = v; 284 this.count = j; 285 } 286 287 315 @Deprecated  316 public String(byte ascii[], int hibyte, int offset, int count) { 317 checkBounds(ascii, offset, count); 318 char value[] = new char[count]; 319 320 if (hibyte == 0) { 321 for (int i = count ; i-- > 0 ;) { 322 value[i] = (char) (ascii[i + offset] & 0xff); 323 } 324 } else { 325 hibyte <<= 8; 326 for (int i = count ; i-- > 0 ;) { 327 value[i] = (char) (hibyte | (ascii[i + offset] & 0xff)); 328 } 329 } 330 this.offset = 0; 331 this.count = count; 332 this.value = value; 333 } 334 335 357 @Deprecated  358 public String(byte ascii[], int hibyte) { 359 this(ascii, hibyte, 0, ascii.length); 360 } 361 362 366 private static void checkBounds(byte[] bytes, int offset, int length) { 367 if (length < 0) 368 throw new StringIndexOutOfBoundsException (length); 369 if (offset < 0) 370 throw new StringIndexOutOfBoundsException (offset); 371 if (offset > bytes.length - length) 372 throw new StringIndexOutOfBoundsException (offset + length); 373 } 374 375 399 public String(byte bytes[], int offset, int length, String charsetName) 400 throws UnsupportedEncodingException  401 { 402 if (charsetName == null) 403 throw new NullPointerException ("charsetName"); 404 checkBounds(bytes, offset, length); 405 char[] v = StringCoding.decode(charsetName, bytes, offset, length); 406 this.offset = 0; 407 this.count = v.length; 408 this.value = v; 409 } 410 411 430 public String(byte bytes[], String charsetName) 431 throws UnsupportedEncodingException  432 { 433 this(bytes, 0, bytes.length, charsetName); 434 } 435 436 456 public String(byte bytes[], int offset, int length) { 457 checkBounds(bytes, offset, length); 458 char[] v = StringCoding.decode(bytes, offset, length); 459 this.offset = 0; 460 this.count = v.length; 461 this.value = v; 462 } 463 464 478 public String(byte bytes[]) { 479 this(bytes, 0, bytes.length); 480 } 481 482 490 public String(StringBuffer buffer) { 491 String result = buffer.toString(); 492 this.value = result.value; 493 this.count = result.count; 494 this.offset = result.offset; 495 } 496 497 511 public String(StringBuilder builder) { 512 String result = builder.toString(); 513 this.value = result.value; 514 this.count = result.count; 515 this.offset = result.offset; 516 } 517 518 519 String(int offset, int count, char value[]) { 521 this.value = value; 522 this.offset = offset; 523 this.count = count; 524 } 525 526 534 public int length() { 535 return count; 536 } 537 538 556 public char charAt(int index) { 557 if ((index < 0) || (index >= count)) { 558 throw new StringIndexOutOfBoundsException (index); 559 } 560 return value[index + offset]; 561 } 562 563 585 public int codePointAt(int index) { 586 if ((index < 0) || (index >= count)) { 587 throw new StringIndexOutOfBoundsException (index); 588 } 589 return Character.codePointAtImpl(value, offset + index, offset + count); 590 } 591 592 614 public int codePointBefore(int index) { 615 int i = index - 1; 616 if ((i < 0) || (i >= count)) { 617 throw new StringIndexOutOfBoundsException (index); 618 } 619 return Character.codePointBeforeImpl(value, offset + index, offset); 620 } 621 622 643 public int codePointCount(int beginIndex, int endIndex) { 644 if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) { 645 throw new IndexOutOfBoundsException (); 646 } 647 return Character.codePointCountImpl(value, offset+beginIndex, endIndex-beginIndex); 648 } 649 650 670 public int offsetByCodePoints(int index, int codePointOffset) { 671 if (index < 0 || index > count) { 672 throw new IndexOutOfBoundsException (); 673 } 674 return Character.offsetByCodePointsImpl(value, offset, count, 675 offset+index, codePointOffset); 676 } 677 678 682 void getChars(char dst[], int dstBegin) { 683 System.arraycopy(value, offset, dst, dstBegin, count); 684 } 685 686 716 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { 717 if (srcBegin < 0) { 718 throw new StringIndexOutOfBoundsException (srcBegin); 719 } 720 if (srcEnd > count) { 721 throw new StringIndexOutOfBoundsException (srcEnd); 722 } 723 if (srcBegin > srcEnd) { 724 throw new StringIndexOutOfBoundsException (srcEnd - srcBegin); 725 } 726 System.arraycopy(value, offset + srcBegin, dst, dstBegin, 727 srcEnd - srcBegin); 728 } 729 730 767 @Deprecated  768 public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) { 769 if (srcBegin < 0) { 770 throw new StringIndexOutOfBoundsException (srcBegin); 771 } 772 if (srcEnd > count) { 773 throw new StringIndexOutOfBoundsException (srcEnd); 774 } 775 if (srcBegin > srcEnd) { 776 throw new StringIndexOutOfBoundsException (srcEnd - srcBegin); 777 } 778 int j = dstBegin; 779 int n = offset + srcEnd; 780 int i = offset + srcBegin; 781 char[] val = value; 782 783 while (i < n) { 784 dst[j++] = (byte)val[i++]; 785 } 786 } 787 788 808 public byte[] getBytes(String charsetName) 809 throws UnsupportedEncodingException  810 { 811 if (charsetName == null) throw new NullPointerException (); 812 return StringCoding.encode(charsetName, value, offset, count); 813 } 814 815 828 public byte[] getBytes() { 829 return StringCoding.encode(value, offset, count); 830 } 831 832 845 public boolean equals(Object anObject) { 846 if (this == anObject) { 847 return true; 848 } 849 if (anObject instanceof String ) { 850 String anotherString = (String )anObject; 851 int n = count; 852 if (n == anotherString.count) { 853 char v1[] = value; 854 char v2[] = anotherString.value; 855 int i = offset; 856 int j = anotherString.offset; 857 while (n-- != 0) { 858 if (v1[i++] != v2[j++]) 859 return false; 860 } 861 return true; 862 } 863 } 864 return false; 865 } 866 867 878 public boolean contentEquals(StringBuffer sb) { 879 synchronized(sb) { 880 return contentEquals((CharSequence )sb); 881 } 882 } 883 884 895 public boolean contentEquals(CharSequence cs) { 896 if (count != cs.length()) 897 return false; 898 if (cs instanceof AbstractStringBuilder ) { 900 char v1[] = value; 901 char v2[] = ((AbstractStringBuilder )cs).getValue(); 902 int i = offset; 903 int j = 0; 904 int n = count; 905 while (n-- != 0) { 906 if (v1[i++] != v2[j++]) 907 return false; 908 } 909 } 910 if (cs.equals(this)) 912 return true; 913 char v1[] = value; 915 int i = offset; 916 int j = 0; 917 int n = count; 918 while (n-- != 0) { 919 if (v1[i++] != cs.charAt(j++)) 920 return false; 921 } 922 return true; 923 } 924 925 |