< prev index next >

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

Print this page
rev 12318 : [mq]: 8130646-AbstractStringBuilder-to-throw-StringIndexOutOfBoundsException


 492      * <i>n</i>; otherwise, it is equal to the character at index
 493      * <i>k+start-n</i> in the argument {@code s}.
 494      * <p>
 495      * If {@code s} is {@code null}, then this method appends
 496      * characters as if the s parameter was a sequence containing the four
 497      * characters {@code "null"}.
 498      *
 499      * @param   s the sequence to append.
 500      * @param   start   the starting index of the subsequence to be appended.
 501      * @param   end     the end index of the subsequence to be appended.
 502      * @return  a reference to this object.
 503      * @throws     IndexOutOfBoundsException if
 504      *             {@code start} is negative, or
 505      *             {@code start} is greater than {@code end} or
 506      *             {@code end} is greater than {@code s.length()}
 507      */
 508     @Override
 509     public AbstractStringBuilder append(CharSequence s, int start, int end) {
 510         if (s == null)
 511             s = "null";
 512         if ((start < 0) || (start > end) || (end > s.length()))
 513             throw new IndexOutOfBoundsException(
 514                 "start " + start + ", end " + end + ", s.length() "
 515                 + s.length());





 516         int len = end - start;
 517         ensureCapacityInternal(count + len);
 518         if (s instanceof String) {
 519             ((String)s).getChars(start, end, value, count);
 520         } else {
 521             for (int i = start, j = count; i < end; i++, j++)
 522                 value[j] = s.charAt(i);
 523         }
 524         count += len;
 525         return this;
 526     }
 527 
 528     /**
 529      * Appends the string representation of the {@code char} array
 530      * argument to this sequence.
 531      * <p>
 532      * The characters of the array argument are appended, in order, to
 533      * the contents of this sequence. The length of this sequence
 534      * increases by the length of the argument.
 535      * <p>


1116      * <p>If {@code s} is {@code null}, then this method inserts
1117      * characters as if the s parameter was a sequence containing the four
1118      * characters {@code "null"}.
1119      *
1120      * @param      dstOffset   the offset in this sequence.
1121      * @param      s       the sequence to be inserted.
1122      * @param      start   the starting index of the subsequence to be inserted.
1123      * @param      end     the end index of the subsequence to be inserted.
1124      * @return     a reference to this object.
1125      * @throws     IndexOutOfBoundsException  if {@code dstOffset}
1126      *             is negative or greater than {@code this.length()}, or
1127      *              {@code start} or {@code end} are negative, or
1128      *              {@code start} is greater than {@code end} or
1129      *              {@code end} is greater than {@code s.length()}
1130      */
1131      public AbstractStringBuilder insert(int dstOffset, CharSequence s,
1132                                          int start, int end) {
1133         if (s == null)
1134             s = "null";
1135         if ((dstOffset < 0) || (dstOffset > this.length()))
1136             throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
1137         if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
1138             throw new IndexOutOfBoundsException(
1139                 "start " + start + ", end " + end + ", s.length() "
1140                 + s.length());





1141         int len = end - start;
1142         ensureCapacityInternal(count + len);
1143         System.arraycopy(value, dstOffset, value, dstOffset + len,
1144                          count - dstOffset);



1145         for (int i=start; i<end; i++)
1146             value[dstOffset++] = s.charAt(i);

1147         count += len;
1148         return this;
1149     }
1150 
1151     /**
1152      * Inserts the string representation of the {@code boolean}
1153      * argument into this sequence.
1154      * <p>
1155      * The overall effect is exactly as if the second argument were
1156      * converted to a string by the method {@link String#valueOf(boolean)},
1157      * and the characters of that string were then
1158      * {@link #insert(int,String) inserted} into this character
1159      * sequence at the indicated offset.
1160      * <p>
1161      * The {@code offset} argument must be greater than or equal to
1162      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1163      * of this sequence.
1164      *
1165      * @param      offset   the offset.
1166      * @param      b        a {@code boolean}.




 492      * <i>n</i>; otherwise, it is equal to the character at index
 493      * <i>k+start-n</i> in the argument {@code s}.
 494      * <p>
 495      * If {@code s} is {@code null}, then this method appends
 496      * characters as if the s parameter was a sequence containing the four
 497      * characters {@code "null"}.
 498      *
 499      * @param   s the sequence to append.
 500      * @param   start   the starting index of the subsequence to be appended.
 501      * @param   end     the end index of the subsequence to be appended.
 502      * @return  a reference to this object.
 503      * @throws     IndexOutOfBoundsException if
 504      *             {@code start} is negative, or
 505      *             {@code start} is greater than {@code end} or
 506      *             {@code end} is greater than {@code s.length()}
 507      */
 508     @Override
 509     public AbstractStringBuilder append(CharSequence s, int start, int end) {
 510         if (s == null)
 511             s = "null";
 512         if ((start < 0) || (start > end) || (end > s.length())) {
 513             String msg = "start " + start + ", end " + end +
 514                     ", s.length() " + s.length()
 515             if (s instanceof String) {
 516                 throw new StringIndexOutOfBoundsException(msg);
 517             } else {
 518                 throw new IndexOutOfBoundsException(msg);
 519             }
 520         }
 521         int len = end - start;
 522         ensureCapacityInternal(count + len);
 523         if (s instanceof String) {
 524             ((String)s).getChars(start, end, value, count);
 525         } else {
 526             for (int i = start, j = count; i < end; i++, j++)
 527                 value[j] = s.charAt(i);
 528         }
 529         count += len;
 530         return this;
 531     }
 532 
 533     /**
 534      * Appends the string representation of the {@code char} array
 535      * argument to this sequence.
 536      * <p>
 537      * The characters of the array argument are appended, in order, to
 538      * the contents of this sequence. The length of this sequence
 539      * increases by the length of the argument.
 540      * <p>


1121      * <p>If {@code s} is {@code null}, then this method inserts
1122      * characters as if the s parameter was a sequence containing the four
1123      * characters {@code "null"}.
1124      *
1125      * @param      dstOffset   the offset in this sequence.
1126      * @param      s       the sequence to be inserted.
1127      * @param      start   the starting index of the subsequence to be inserted.
1128      * @param      end     the end index of the subsequence to be inserted.
1129      * @return     a reference to this object.
1130      * @throws     IndexOutOfBoundsException  if {@code dstOffset}
1131      *             is negative or greater than {@code this.length()}, or
1132      *              {@code start} or {@code end} are negative, or
1133      *              {@code start} is greater than {@code end} or
1134      *              {@code end} is greater than {@code s.length()}
1135      */
1136      public AbstractStringBuilder insert(int dstOffset, CharSequence s,
1137                                          int start, int end) {
1138         if (s == null)
1139             s = "null";
1140         if ((dstOffset < 0) || (dstOffset > this.length()))
1141             throw new StringIndexOutOfBoundsException("dstOffset "+dstOffset);
1142         if ((start < 0) || (end < 0) || (start > end) || (end > s.length())) {
1143             String msg = "start " + start + ", end " + end +
1144                     ", s.length() " + s.length();
1145             if (s instanceof String) {
1146                 throw new StringIndexOutOfBoundsException(msg);
1147             } else {
1148                 throw new IndexOutOfBoundsException(msg);
1149             }
1150         }
1151         int len = end - start;
1152         ensureCapacityInternal(count + len);
1153         System.arraycopy(value, dstOffset, value, dstOffset + len,
1154                          count - dstOffset);
1155         if (s instanceof String) {
1156             ((String)s).getChars(start, end, value, dstOffset);
1157         } else {
1158             for (int i=start; i<end; i++)
1159                 value[dstOffset++] = s.charAt(i);
1160         }
1161         count += len;
1162         return this;
1163     }
1164 
1165     /**
1166      * Inserts the string representation of the {@code boolean}
1167      * argument into this sequence.
1168      * <p>
1169      * The overall effect is exactly as if the second argument were
1170      * converted to a string by the method {@link String#valueOf(boolean)},
1171      * and the characters of that string were then
1172      * {@link #insert(int,String) inserted} into this character
1173      * sequence at the indicated offset.
1174      * <p>
1175      * The {@code offset} argument must be greater than or equal to
1176      * {@code 0}, and less than or equal to the {@linkplain #length() length}
1177      * of this sequence.
1178      *
1179      * @param      offset   the offset.
1180      * @param      b        a {@code boolean}.


< prev index next >