< prev index next >

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

Print this page
rev 13124 : [mq]: XXXXXXX-ASB-shared-value

@@ -409,13 +409,19 @@
     }
 
     @Override
     @HotSpotIntrinsicCandidate
     public String toString() {
-        // Create a copy, don't share the array
-        return isLatin1() ? StringLatin1.newString(value, 0, count)
-                          : StringUTF16.newStringSB(value, 0, count);
+        final byte[] value = this.value;
+        if (isLatin1()) {
+            if ((count << coder) < value.length) {
+                return StringLatin1.newString(value, 0, count);
+            }
+            shared = true;
+            return new String(value, String.LATIN1);
+        }
+        return StringUTF16.newStringSB(value, 0, count);
     }
 
     /**
      * Save the state of the {@code StringBuilder} instance to a stream
      * (that is, serialize it).

@@ -448,8 +454,9 @@
         throws java.io.IOException, ClassNotFoundException {
         s.defaultReadObject();
         count = s.readInt();
         char[] val = (char[]) s.readObject();
         initBytes(val, 0, val.length);
+        shared = false;
     }
 
 }
< prev index next >