|
1 | 1 | /* |
2 | | - * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
28 | 28 | import jdk.internal.math.DoubleToDecimal; |
29 | 29 | import jdk.internal.math.FloatToDecimal; |
30 | 30 |
|
31 | | -import java.io.IOException; |
32 | 31 | import java.nio.CharBuffer; |
33 | 32 | import java.util.Arrays; |
34 | 33 | import java.util.Spliterator; |
@@ -356,8 +355,12 @@ public void setLength(int newLength) { |
356 | 355 | */ |
357 | 356 | @Override |
358 | 357 | public char charAt(int index) { |
| 358 | + byte coder = this.coder; |
| 359 | + byte[] value = this.value; |
| 360 | + // Ensure count is less than or equal to capacity (racy reads and writes can produce inconsistent values) |
| 361 | + int count = Math.min(this.count, value.length >> coder); |
359 | 362 | checkIndex(index, count); |
360 | | - if (isLatin1()) { |
| 363 | + if (coder == LATIN1) { |
361 | 364 | return (char)(value[index] & 0xff); |
362 | 365 | } |
363 | 366 | return StringUTF16.getChar(value, index); |
@@ -416,6 +419,7 @@ public int codePointAt(int index) { |
416 | 419 | * of this sequence. |
417 | 420 | */ |
418 | 421 | public int codePointBefore(int index) { |
| 422 | + byte[] value = this.value; |
419 | 423 | int i = index - 1; |
420 | 424 | checkIndex(i, count); |
421 | 425 | if (isLatin1()) { |
@@ -1720,7 +1724,7 @@ private final void putCharsAt(int index, CharSequence s, int off, int end) { |
1720 | 1724 | } else { |
1721 | 1725 | inflate(); |
1722 | 1726 | // store c to make sure it has a UTF16 char |
1723 | | - StringUTF16.putChar(this.value, j++, c); |
| 1727 | + StringUTF16.putCharSB(this.value, j++, c); |
1724 | 1728 | i++; |
1725 | 1729 | StringUTF16.putCharsSB(this.value, j, s, i, end); |
1726 | 1730 | return; |
@@ -1810,7 +1814,7 @@ private final void appendChars(CharSequence s, int off, int end) { |
1810 | 1814 | count = j; |
1811 | 1815 | inflate(); |
1812 | 1816 | // Store c to make sure sb has a UTF16 char |
1813 | | - StringUTF16.putChar(this.value, j++, c); |
| 1817 | + StringUTF16.putCharSB(this.value, j++, c); |
1814 | 1818 | count = j; |
1815 | 1819 | i++; |
1816 | 1820 | StringUTF16.putCharsSB(this.value, j, s, i, end); |
|
0 commit comments