src/share/classes/java/lang/StringBuffer.java

Print this page
rev 6406 : 4247235: (spec str) StringBuffer.insert(int, char[]) specification is inconsistent
Summary: Add blanket null-handling statement to StringBuilder and StringBuffer
Reviewed-by: mduigou


  60  * {@code sb.insert(sb.length(), x)}.
  61  * <p>
  62  * Whenever an operation occurs involving a source sequence (such as
  63  * appending or inserting from a source sequence), this class synchronizes
  64  * only on the string buffer performing the operation, not on the source.
  65  * Note that while {@code StringBuffer} is designed to be safe to use
  66  * concurrently from multiple threads, if the constructor or the
  67  * {@code append} or {@code insert} operation is passed a source sequence
  68  * that is shared across threads, the calling code must ensure
  69  * that the operation has a consistent and unchanging view of the source
  70  * sequence for the duration of the operation.
  71  * This could be satisfied by the caller holding a lock during the
  72  * operation's call, by using an immutable source sequence, or by not
  73  * sharing the source sequence across threads.
  74  * <p>
  75  * Every string buffer has a capacity. As long as the length of the
  76  * character sequence contained in the string buffer does not exceed
  77  * the capacity, it is not necessary to allocate a new internal
  78  * buffer array. If the internal buffer overflows, it is
  79  * automatically made larger.
  80  *




  81  * As of  release JDK 5, this class has been supplemented with an equivalent
  82  * class designed for use by a single thread, {@link StringBuilder}.  The
  83  * {@code StringBuilder} class should generally be used in preference to
  84  * this one, as it supports all of the same operations but it is faster, as
  85  * it performs no synchronization.
  86  *
  87  * @author      Arthur van Hoff
  88  * @see     java.lang.StringBuilder
  89  * @see     java.lang.String
  90  * @since   JDK1.0
  91  */
  92  public final class StringBuffer
  93     extends AbstractStringBuilder
  94     implements java.io.Serializable, CharSequence
  95 {
  96 
  97     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  98     static final long serialVersionUID = 3388685877147921107L;
  99 
 100     /**


 106     }
 107 
 108     /**
 109      * Constructs a string buffer with no characters in it and
 110      * the specified initial capacity.
 111      *
 112      * @param      capacity  the initial capacity.
 113      * @exception  NegativeArraySizeException  if the {@code capacity}
 114      *               argument is less than {@code 0}.
 115      */
 116     public StringBuffer(int capacity) {
 117         super(capacity);
 118     }
 119 
 120     /**
 121      * Constructs a string buffer initialized to the contents of the
 122      * specified string. The initial capacity of the string buffer is
 123      * {@code 16} plus the length of the string argument.
 124      *
 125      * @param   str   the initial contents of the buffer.
 126      * @exception NullPointerException if {@code str} is {@code null}
 127      */
 128     public StringBuffer(String str) {
 129         super(str.length() + 16);
 130         append(str);
 131     }
 132 
 133     /**
 134      * Constructs a string buffer that contains the same characters
 135      * as the specified {@code CharSequence}. The initial capacity of
 136      * the string buffer is {@code 16} plus the length of the
 137      * {@code CharSequence} argument.
 138      * <p>
 139      * If the length of the specified {@code CharSequence} is
 140      * less than or equal to zero, then an empty buffer of capacity
 141      * {@code 16} is returned.
 142      *
 143      * @param      seq   the sequence to copy.
 144      * @exception NullPointerException if {@code seq} is {@code null}
 145      * @since 1.5
 146      */
 147     public StringBuffer(CharSequence seq) {
 148         this(seq.length() + 16);
 149         append(seq);
 150     }
 151 
 152     @Override
 153     public synchronized int length() {
 154         return count;
 155     }
 156 
 157     @Override
 158     public synchronized int capacity() {
 159         return value.length;
 160     }
 161 
 162 
 163     @Override
 164     public synchronized void ensureCapacity(int minimumCapacity) {


 211         return super.codePointBefore(index);
 212     }
 213 
 214     /**
 215      * @since     1.5
 216      */
 217     @Override
 218     public synchronized int codePointCount(int beginIndex, int endIndex) {
 219         return super.codePointCount(beginIndex, endIndex);
 220     }
 221 
 222     /**
 223      * @since     1.5
 224      */
 225     @Override
 226     public synchronized int offsetByCodePoints(int index, int codePointOffset) {
 227         return super.offsetByCodePoints(index, codePointOffset);
 228     }
 229 
 230     /**
 231      * @throws NullPointerException {@inheritDoc}
 232      * @throws IndexOutOfBoundsException {@inheritDoc}
 233      */
 234     @Override
 235     public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
 236                                       int dstBegin)
 237     {
 238         super.getChars(srcBegin, srcEnd, dst, dstBegin);
 239     }
 240 
 241     /**
 242      * @throws IndexOutOfBoundsException {@inheritDoc}
 243      * @see        #length()
 244      */
 245     @Override
 246     public synchronized void setCharAt(int index, char ch) {
 247         if ((index < 0) || (index >= count))
 248             throw new StringIndexOutOfBoundsException(index);
 249         value[index] = ch;
 250     }
 251 


 567     @Override
 568     public StringBuffer insert(int offset, float f) {
 569         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
 570         // after conversion of f to String by super class method
 571         super.insert(offset, f);
 572         return this;
 573     }
 574 
 575     /**
 576      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 577      */
 578     @Override
 579     public StringBuffer insert(int offset, double d) {
 580         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
 581         // after conversion of d to String by super class method
 582         super.insert(offset, d);
 583         return this;
 584     }
 585 
 586     /**
 587      * @throws NullPointerException {@inheritDoc}
 588      * @since      1.4
 589      */
 590     @Override
 591     public int indexOf(String str) {
 592         // Note, synchronization achieved via invocations of other StringBuffer methods
 593         return super.indexOf(str);
 594     }
 595 
 596     /**
 597      * @throws NullPointerException {@inheritDoc}
 598      * @since      1.4
 599      */
 600     @Override
 601     public synchronized int indexOf(String str, int fromIndex) {
 602         return super.indexOf(str, fromIndex);
 603     }
 604 
 605     /**
 606      * @throws NullPointerException {@inheritDoc}
 607      * @since      1.4
 608      */
 609     @Override
 610     public int lastIndexOf(String str) {
 611         // Note, synchronization achieved via invocations of other StringBuffer methods
 612         return lastIndexOf(str, count);
 613     }
 614 
 615     /**
 616      * @throws NullPointerException {@inheritDoc}
 617      * @since      1.4
 618      */
 619     @Override
 620     public synchronized int lastIndexOf(String str, int fromIndex) {
 621         return super.lastIndexOf(str, fromIndex);
 622     }
 623 
 624     /**
 625      * @since   JDK1.0.2
 626      */
 627     @Override
 628     public synchronized StringBuffer reverse() {
 629         super.reverse();
 630         return this;
 631     }
 632 
 633     @Override
 634     public synchronized String toString() {
 635         return new String(value, 0, count);
 636     }




  60  * {@code sb.insert(sb.length(), x)}.
  61  * <p>
  62  * Whenever an operation occurs involving a source sequence (such as
  63  * appending or inserting from a source sequence), this class synchronizes
  64  * only on the string buffer performing the operation, not on the source.
  65  * Note that while {@code StringBuffer} is designed to be safe to use
  66  * concurrently from multiple threads, if the constructor or the
  67  * {@code append} or {@code insert} operation is passed a source sequence
  68  * that is shared across threads, the calling code must ensure
  69  * that the operation has a consistent and unchanging view of the source
  70  * sequence for the duration of the operation.
  71  * This could be satisfied by the caller holding a lock during the
  72  * operation's call, by using an immutable source sequence, or by not
  73  * sharing the source sequence across threads.
  74  * <p>
  75  * Every string buffer has a capacity. As long as the length of the
  76  * character sequence contained in the string buffer does not exceed
  77  * the capacity, it is not necessary to allocate a new internal
  78  * buffer array. If the internal buffer overflows, it is
  79  * automatically made larger.
  80  * <p>
  81  * Unless otherwise noted, passing a {@code null} argument to a constructor
  82  * or method in this class will cause a {@link NullPointerException} to be
  83  * thrown.
  84  * <p>
  85  * As of  release JDK 5, this class has been supplemented with an equivalent
  86  * class designed for use by a single thread, {@link StringBuilder}.  The
  87  * {@code StringBuilder} class should generally be used in preference to
  88  * this one, as it supports all of the same operations but it is faster, as
  89  * it performs no synchronization.
  90  *
  91  * @author      Arthur van Hoff
  92  * @see     java.lang.StringBuilder
  93  * @see     java.lang.String
  94  * @since   JDK1.0
  95  */
  96  public final class StringBuffer
  97     extends AbstractStringBuilder
  98     implements java.io.Serializable, CharSequence
  99 {
 100 
 101     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 102     static final long serialVersionUID = 3388685877147921107L;
 103 
 104     /**


 110     }
 111 
 112     /**
 113      * Constructs a string buffer with no characters in it and
 114      * the specified initial capacity.
 115      *
 116      * @param      capacity  the initial capacity.
 117      * @exception  NegativeArraySizeException  if the {@code capacity}
 118      *               argument is less than {@code 0}.
 119      */
 120     public StringBuffer(int capacity) {
 121         super(capacity);
 122     }
 123 
 124     /**
 125      * Constructs a string buffer initialized to the contents of the
 126      * specified string. The initial capacity of the string buffer is
 127      * {@code 16} plus the length of the string argument.
 128      *
 129      * @param   str   the initial contents of the buffer.

 130      */
 131     public StringBuffer(String str) {
 132         super(str.length() + 16);
 133         append(str);
 134     }
 135 
 136     /**
 137      * Constructs a string buffer that contains the same characters
 138      * as the specified {@code CharSequence}. The initial capacity of
 139      * the string buffer is {@code 16} plus the length of the
 140      * {@code CharSequence} argument.
 141      * <p>
 142      * If the length of the specified {@code CharSequence} is
 143      * less than or equal to zero, then an empty buffer of capacity
 144      * {@code 16} is returned.
 145      *
 146      * @param      seq   the sequence to copy.

 147      * @since 1.5
 148      */
 149     public StringBuffer(CharSequence seq) {
 150         this(seq.length() + 16);
 151         append(seq);
 152     }
 153 
 154     @Override
 155     public synchronized int length() {
 156         return count;
 157     }
 158 
 159     @Override
 160     public synchronized int capacity() {
 161         return value.length;
 162     }
 163 
 164 
 165     @Override
 166     public synchronized void ensureCapacity(int minimumCapacity) {


 213         return super.codePointBefore(index);
 214     }
 215 
 216     /**
 217      * @since     1.5
 218      */
 219     @Override
 220     public synchronized int codePointCount(int beginIndex, int endIndex) {
 221         return super.codePointCount(beginIndex, endIndex);
 222     }
 223 
 224     /**
 225      * @since     1.5
 226      */
 227     @Override
 228     public synchronized int offsetByCodePoints(int index, int codePointOffset) {
 229         return super.offsetByCodePoints(index, codePointOffset);
 230     }
 231 
 232     /**

 233      * @throws IndexOutOfBoundsException {@inheritDoc}
 234      */
 235     @Override
 236     public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
 237                                       int dstBegin)
 238     {
 239         super.getChars(srcBegin, srcEnd, dst, dstBegin);
 240     }
 241 
 242     /**
 243      * @throws IndexOutOfBoundsException {@inheritDoc}
 244      * @see        #length()
 245      */
 246     @Override
 247     public synchronized void setCharAt(int index, char ch) {
 248         if ((index < 0) || (index >= count))
 249             throw new StringIndexOutOfBoundsException(index);
 250         value[index] = ch;
 251     }
 252 


 568     @Override
 569     public StringBuffer insert(int offset, float f) {
 570         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
 571         // after conversion of f to String by super class method
 572         super.insert(offset, f);
 573         return this;
 574     }
 575 
 576     /**
 577      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 578      */
 579     @Override
 580     public StringBuffer insert(int offset, double d) {
 581         // Note, synchronization achieved via invocation of StringBuffer insert(int, String)
 582         // after conversion of d to String by super class method
 583         super.insert(offset, d);
 584         return this;
 585     }
 586 
 587     /**

 588      * @since      1.4
 589      */
 590     @Override
 591     public int indexOf(String str) {
 592         // Note, synchronization achieved via invocations of other StringBuffer methods
 593         return super.indexOf(str);
 594     }
 595 
 596     /**

 597      * @since      1.4
 598      */
 599     @Override
 600     public synchronized int indexOf(String str, int fromIndex) {
 601         return super.indexOf(str, fromIndex);
 602     }
 603 
 604     /**

 605      * @since      1.4
 606      */
 607     @Override
 608     public int lastIndexOf(String str) {
 609         // Note, synchronization achieved via invocations of other StringBuffer methods
 610         return lastIndexOf(str, count);
 611     }
 612 
 613     /**

 614      * @since      1.4
 615      */
 616     @Override
 617     public synchronized int lastIndexOf(String str, int fromIndex) {
 618         return super.lastIndexOf(str, fromIndex);
 619     }
 620 
 621     /**
 622      * @since   JDK1.0.2
 623      */
 624     @Override
 625     public synchronized StringBuffer reverse() {
 626         super.reverse();
 627         return this;
 628     }
 629 
 630     @Override
 631     public synchronized String toString() {
 632         return new String(value, 0, count);
 633     }