src/share/classes/java/util/StringJoiner.java
Print this page
rev 7730 : 8020977: StringJoiner merges with itself not as expected
Reviewed-by: psandoz, chegar, mduigou, smarks
*** 204,218 ****
* @throws NullPointerException if the other {@code StringJoiner} is null
*/
public StringJoiner merge(StringJoiner other) {
Objects.requireNonNull(other);
if (other.value != null) {
StringBuilder builder = prepareBuilder();
! StringBuilder otherBuilder = other.value;
! if (other.prefix.length() < otherBuilder.length()) {
! builder.append(otherBuilder, other.prefix.length(), otherBuilder.length());
! }
}
return this;
}
private StringBuilder prepareBuilder() {
--- 204,219 ----
* @throws NullPointerException if the other {@code StringJoiner} is null
*/
public StringJoiner merge(StringJoiner other) {
Objects.requireNonNull(other);
if (other.value != null) {
+ final int length = other.value.length();
+ // lock the length so that we can seize the data to be appended
+ // before initiate copying to avoid interference, especially when
+ // merge 'this'
StringBuilder builder = prepareBuilder();
! builder.append(other.value, other.prefix.length(), length);
}
return this;
}
private StringBuilder prepareBuilder() {