< prev index next >

src/java.base/share/classes/java/lang/AbstractStringBuilder.java

Print this page




 530         if (getCoder() != asb.getCoder()) {
 531             inflate();
 532         }
 533         asb.getBytes(value, count, coder);
 534         count += len;
 535         return this;
 536     }
 537 
 538     // Documentation in subclasses because of synchro difference
 539     @Override
 540     public AbstractStringBuilder append(CharSequence s) {
 541         if (s == null) {
 542             return appendNull();
 543         }
 544         if (s instanceof String) {
 545             return this.append((String)s);
 546         }
 547         if (s instanceof AbstractStringBuilder) {
 548             return this.append((AbstractStringBuilder)s);
 549         }




 550         return this.append(s, 0, s.length());
 551     }
 552 
 553     private AbstractStringBuilder appendNull() {
 554         ensureCapacityInternal(count + 4);
 555         int count = this.count;
 556         byte[] val = this.value;
 557         if (isLatin1()) {
 558             val[count++] = 'n';
 559             val[count++] = 'u';
 560             val[count++] = 'l';
 561             val[count++] = 'l';
 562         } else {
 563             checkOffset(count + 4, val.length >> 1);
 564             StringUTF16.putChar(val, count++, 'n');
 565             StringUTF16.putChar(val, count++, 'u');
 566             StringUTF16.putChar(val, count++, 'l');
 567             StringUTF16.putChar(val, count++, 'l');
 568         }
 569         this.count = count;


 721      * The length of this sequence increases by {@code 1}.
 722      * <p>
 723      * The overall effect is exactly as if the argument were converted
 724      * to a string by the method {@link String#valueOf(char)},
 725      * and the character in that string were then
 726      * {@link #append(String) appended} to this character sequence.
 727      *
 728      * @param   c   a {@code char}.
 729      * @return  a reference to this object.
 730      */
 731     @Override
 732     public AbstractStringBuilder append(char c) {
 733         ensureCapacityInternal(count + 1);
 734         if (isLatin1() && StringLatin1.canEncode(c)) {
 735             value[count++] = (byte)c;
 736         } else {
 737             if (isLatin1()) {
 738                 inflate();
 739             }
 740             StringUTF16.putCharSB(value, count++, c);















































 741         }
 742         return this;
 743     }
 744 
 745     /**
 746      * Appends the string representation of the {@code int}
 747      * argument to this sequence.
 748      * <p>
 749      * The overall effect is exactly as if the argument were converted
 750      * to a string by the method {@link String#valueOf(int)},
 751      * and the characters of that string were then
 752      * {@link #append(String) appended} to this character sequence.
 753      *
 754      * @param   i   an {@code int}.
 755      * @return  a reference to this object.
 756      */
 757     public AbstractStringBuilder append(int i) {
 758         int spaceNeeded = count + Integer.stringSize(i);
 759         ensureCapacityInternal(spaceNeeded);
 760         if (isLatin1()) {




 530         if (getCoder() != asb.getCoder()) {
 531             inflate();
 532         }
 533         asb.getBytes(value, count, coder);
 534         count += len;
 535         return this;
 536     }
 537 
 538     // Documentation in subclasses because of synchro difference
 539     @Override
 540     public AbstractStringBuilder append(CharSequence s) {
 541         if (s == null) {
 542             return appendNull();
 543         }
 544         if (s instanceof String) {
 545             return this.append((String)s);
 546         }
 547         if (s instanceof AbstractStringBuilder) {
 548             return this.append((AbstractStringBuilder)s);
 549         }
 550         if (s instanceof CharRepetitions) {
 551             CharRepetitions cr = (CharRepetitions) s;
 552             return this.appendN(cr.getChar(), cr.length());
 553         }
 554         return this.append(s, 0, s.length());
 555     }
 556 
 557     private AbstractStringBuilder appendNull() {
 558         ensureCapacityInternal(count + 4);
 559         int count = this.count;
 560         byte[] val = this.value;
 561         if (isLatin1()) {
 562             val[count++] = 'n';
 563             val[count++] = 'u';
 564             val[count++] = 'l';
 565             val[count++] = 'l';
 566         } else {
 567             checkOffset(count + 4, val.length >> 1);
 568             StringUTF16.putChar(val, count++, 'n');
 569             StringUTF16.putChar(val, count++, 'u');
 570             StringUTF16.putChar(val, count++, 'l');
 571             StringUTF16.putChar(val, count++, 'l');
 572         }
 573         this.count = count;


 725      * The length of this sequence increases by {@code 1}.
 726      * <p>
 727      * The overall effect is exactly as if the argument were converted
 728      * to a string by the method {@link String#valueOf(char)},
 729      * and the character in that string were then
 730      * {@link #append(String) appended} to this character sequence.
 731      *
 732      * @param   c   a {@code char}.
 733      * @return  a reference to this object.
 734      */
 735     @Override
 736     public AbstractStringBuilder append(char c) {
 737         ensureCapacityInternal(count + 1);
 738         if (isLatin1() && StringLatin1.canEncode(c)) {
 739             value[count++] = (byte)c;
 740         } else {
 741             if (isLatin1()) {
 742                 inflate();
 743             }
 744             StringUTF16.putCharSB(value, count++, c);
 745         }
 746         return this;
 747     }
 748 
 749     /**
 750      * Appends {@code n} copies of the string representation of
 751      * the {@code char} argument to this sequence.
 752      * <p>
 753      * The first argument is appended to the contents of this
 754      * sequence specified amount of times.
 755      * The length of this sequence increases by {@code n}.
 756      * <p>
 757      * The overall effect is exactly as if the first argument
 758      * were converted to a string by the method
 759      * {@link String#valueOf(char)}, and the character in that
 760      * string were then {@link #append(String) appended} to this
 761      * character sequence {@code n} times.
 762      *
 763      * @param   c   a {@code char}.
 764      * @param   n   a number of copies to append.
 765      * @return  a reference to this object.
 766      * @throws  IllegalArgumentException  if {@code n < 0}.
 767      */
 768     @Override
 769     public AbstractStringBuilder appendN(char c, int n) {
 770         if (n <= 0) {
 771             if (n < 0) {
 772                 throw new IllegalArgumentException(
 773                         "Negative number of copies: " + n);
 774             }
 775         } else {
 776             int count = this.count;
 777             ensureCapacityInternal(count + n);
 778             if (isLatin1() && StringLatin1.canEncode(c)) {
 779                 byte b = (byte)c;
 780                 do {
 781                     value[count++] = b;
 782                 } while (--n > 0);
 783             } else {
 784                 if (isLatin1()) {
 785                     inflate();
 786                 }
 787                 do {
 788                     StringUTF16.putCharSB(value, count++, c);
 789                 } while (--n > 0);
 790             }
 791             this.count = count;
 792         }
 793         return this;
 794     }
 795 
 796     /**
 797      * Appends the string representation of the {@code int}
 798      * argument to this sequence.
 799      * <p>
 800      * The overall effect is exactly as if the argument were converted
 801      * to a string by the method {@link String#valueOf(int)},
 802      * and the characters of that string were then
 803      * {@link #append(String) appended} to this character sequence.
 804      *
 805      * @param   i   an {@code int}.
 806      * @return  a reference to this object.
 807      */
 808     public AbstractStringBuilder append(int i) {
 809         int spaceNeeded = count + Integer.stringSize(i);
 810         ensureCapacityInternal(spaceNeeded);
 811         if (isLatin1()) {


< prev index next >