< prev index next >

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

Print this page

        

@@ -23,10 +23,11 @@
  * questions.
  */
 
 package java.lang;
 
+import sun.misc.ConstantLengthCharSequence;
 import sun.misc.FloatingDecimal;
 import java.util.Arrays;
 import java.util.Spliterator;
 import java.util.stream.IntStream;
 import java.util.stream.StreamSupport;

@@ -458,14 +459,28 @@
             return appendNull();
         if (s instanceof String)
             return this.append((String)s);
         if (s instanceof AbstractStringBuilder)
             return this.append((AbstractStringBuilder)s);
+        if (s instanceof ConstantLengthCharSequence)
+            return this.append((ConstantLengthCharSequence)s);
 
         return this.append(s, 0, s.length());
     }
 
+    /**
+     * @since 1.9
+     */
+    AbstractStringBuilder append(ConstantLengthCharSequence ccs) {
+        // assert ccs != null;
+        int len = ccs.length();
+        ensureCapacityInternal(count + len);
+        ccs.getChars(0, len, value, count);
+        count += len;
+        return this;
+    }
+
     private AbstractStringBuilder appendNull() {
         int c = count;
         ensureCapacityInternal(c + 4);
         final char[] value = this.value;
         value[c++] = 'n';

@@ -513,12 +528,16 @@
             throw new IndexOutOfBoundsException(
                 "start " + start + ", end " + end + ", s.length() "
                 + s.length());
         int len = end - start;
         ensureCapacityInternal(count + len);
+        if (s instanceof ConstantLengthCharSequence) {
+            ((ConstantLengthCharSequence)s).getChars(start, end, value, count);
+        } else {
         for (int i = start, j = count; i < end; i++, j++)
             value[j] = s.charAt(i);
+        }
         count += len;
         return this;
     }
 
     /**

@@ -1075,14 +1094,32 @@
     public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
         if (s == null)
             s = "null";
         if (s instanceof String)
             return this.insert(dstOffset, (String)s);
+        if (s instanceof ConstantLengthCharSequence)
+            return this.insert(dstOffset, (ConstantLengthCharSequence)s);
         return this.insert(dstOffset, s, 0, s.length());
     }
 
     /**
+     * @since 1.9
+     */
+    AbstractStringBuilder insert(int offset, ConstantLengthCharSequence ccs) {
+        if ((offset < 0) || (offset > length()))
+            throw new StringIndexOutOfBoundsException(offset);
+        if (ccs == null)
+            ccs = "null";
+        int len = ccs.length();
+        ensureCapacityInternal(count + len);
+        System.arraycopy(value, offset, value, offset + len, count - offset);
+        ccs.getChars(0, len, value, offset);
+        count += len;
+        return this;
+    }
+
+    /**
      * Inserts a subsequence of the specified {@code CharSequence} into
      * this sequence.
      * <p>
      * The subsequence of the argument {@code s} specified by
      * {@code start} and {@code end} are inserted,

@@ -1136,12 +1173,16 @@
                 + s.length());
         int len = end - start;
         ensureCapacityInternal(count + len);
         System.arraycopy(value, dstOffset, value, dstOffset + len,
                          count - dstOffset);
-        for (int i=start; i<end; i++)
+        if (s instanceof ConstantLengthCharSequence) {
+            ((ConstantLengthCharSequence)s).getChars(start, end, value, dstOffset);
+        } else {
+            for (int i = start; i < end; i++)
             value[dstOffset++] = s.charAt(i);
+        }
         count += len;
         return this;
     }
 
     /**
< prev index next >