Skip to content

Commit

Permalink
8325590: Regression in round-tripping UTF-16 strings after JDK-8311906
Browse files Browse the repository at this point in the history
Reviewed-by: alanb, redestad
  • Loading branch information
Roger Riggs committed Feb 13, 2024
1 parent 57b04e1 commit 13d9e8f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/lang/String.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -841,7 +841,7 @@ private static String newStringNoRepl1(byte[] src, Charset cs) {
}
if (COMPACT_STRINGS) {
byte[] val = StringUTF16.compress(ca, 0, caLen);
int coder = StringUTF16.coderFromArrayLen(val, len);
byte coder = StringUTF16.coderFromArrayLen(val, caLen);
return new String(val, coder);
}
return new String(StringUTF16.toBytes(ca, 0, caLen), UTF16);
Expand Down
40 changes: 35 additions & 5 deletions test/jdk/java/nio/file/Files/ReadWriteString.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,8 +29,13 @@
import java.nio.charset.UnmappableCharacterException;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_16;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.charset.StandardCharsets.UTF_16;
import static java.nio.charset.StandardCharsets.UTF_16BE;
import static java.nio.charset.StandardCharsets.UTF_16LE;
import static java.nio.charset.StandardCharsets.UTF_32;
import static java.nio.charset.StandardCharsets.UTF_32BE;
import static java.nio.charset.StandardCharsets.UTF_32LE;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
Expand All @@ -40,15 +45,15 @@
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.Callable;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/* @test
* @bug 8201276 8205058 8209576 8287541 8288589
* @bug 8201276 8205058 8209576 8287541 8288589 8325590
* @build ReadWriteString PassThroughFileSystem
* @run testng ReadWriteString
* @summary Unit test for methods for Files readString and write methods.
Expand All @@ -61,6 +66,7 @@ public class ReadWriteString {
// data for text files
final String TEXT_UNICODE = "\u201CHello\u201D";
final String TEXT_ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n abcdefghijklmnopqrstuvwxyz\n 1234567890\n";
final static String TEXT_PERSON_CART_WHEELING = "\ud83e\udd38";
private static final String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217";
private static final Charset WINDOWS_1252 = Charset.forName("windows-1252");
private static final Charset WINDOWS_31J = Charset.forName("windows-31j");
Expand Down Expand Up @@ -154,7 +160,16 @@ public Object[][] getReadString() {
{testFiles[1], TEXT_ASCII, US_ASCII, US_ASCII},
{testFiles[1], TEXT_ASCII, US_ASCII, UTF_8},
{testFiles[1], TEXT_UNICODE, UTF_8, null},
{testFiles[1], TEXT_UNICODE, UTF_8, UTF_8}
{testFiles[1], TEXT_UNICODE, UTF_8, UTF_8},
{testFiles[1], TEXT_ASCII, US_ASCII, ISO_8859_1},
{testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16, UTF_16},
{testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16BE, UTF_16BE},
{testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_16LE, UTF_16LE},
{testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32, UTF_32},
{testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32BE, UTF_32BE},
{testFiles[1], TEXT_PERSON_CART_WHEELING, UTF_32LE, UTF_32LE},
{testFiles[1], TEXT_PERSON_CART_WHEELING, WINDOWS_1252, WINDOWS_1252},
{testFiles[1], TEXT_PERSON_CART_WHEELING, WINDOWS_31J, WINDOWS_31J}
};
}

Expand Down Expand Up @@ -304,6 +319,21 @@ public void testMalformedReadBytes(byte[] data, Charset csRead, Class<CharacterC
throw new RuntimeException("An instance of " + expected + " should be thrown");
}

// Verify File.readString with UTF16 to confirm proper string length and contents.
// A regression test for 8325590
@Test
public void testSingleUTF16() throws IOException {
String original = "🤸"; // "\ud83e\udd38";
Files.writeString(testFiles[0], original, UTF_16);
String actual = Files.readString(testFiles[0], UTF_16);
if (!actual.equals(original)) {
System.out.printf("expected (%s), was (%s)\n", original, actual);
System.out.printf("expected UTF_16 bytes: %s\n", Arrays.toString(original.getBytes(UTF_16)));
System.out.printf("actual UTF_16 bytes: %s\n", Arrays.toString(actual.getBytes(UTF_16)));
}
assertEquals(actual, original, "Round trip string mismatch with multi-byte encoding");
}

private void checkNullPointerException(Callable<?> c) {
try {
c.call();
Expand Down

7 comments on commit 13d9e8f

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RogerRiggs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk22

@openjdk
Copy link

@openjdk openjdk bot commented on 13d9e8f Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RogerRiggs the backport was successfully created on the branch backport-RogerRiggs-13d9e8ff in my personal fork of openjdk/jdk22. To create a pull request with this backport targeting openjdk/jdk22:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 13d9e8ff from the openjdk/jdk repository.

The commit being backported was authored by Roger Riggs on 13 Feb 2024 and was reviewed by Alan Bateman and Claes Redestad.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk22:

$ git fetch https://github.com/openjdk-bots/jdk22.git backport-RogerRiggs-13d9e8ff:backport-RogerRiggs-13d9e8ff
$ git checkout backport-RogerRiggs-13d9e8ff
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk22.git backport-RogerRiggs-13d9e8ff

⚠️ @RogerRiggs You are not yet a collaborator in my fork openjdk-bots/jdk22. An invite will be sent out and you need to accept it before you can proceed.

@RogerRiggs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk22u

@openjdk
Copy link

@openjdk openjdk bot commented on 13d9e8f Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RogerRiggs the backport was successfully created on the branch backport-RogerRiggs-13d9e8ff in my personal fork of openjdk/jdk22u. To create a pull request with this backport targeting openjdk/jdk22u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 13d9e8ff from the openjdk/jdk repository.

The commit being backported was authored by Roger Riggs on 13 Feb 2024 and was reviewed by Alan Bateman and Claes Redestad.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk22u:

$ git fetch https://github.com/openjdk-bots/jdk22u.git backport-RogerRiggs-13d9e8ff:backport-RogerRiggs-13d9e8ff
$ git checkout backport-RogerRiggs-13d9e8ff
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk22u.git backport-RogerRiggs-13d9e8ff

⚠️ @RogerRiggs You are not yet a collaborator in my fork openjdk-bots/jdk22u. An invite will be sent out and you need to accept it before you can proceed.

@jaikiran
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk22u

@openjdk
Copy link

@openjdk openjdk bot commented on 13d9e8f Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaikiran the backport was successfully created on the branch backport-jaikiran-13d9e8ff in my personal fork of openjdk/jdk22u. To create a pull request with this backport targeting openjdk/jdk22u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 13d9e8ff from the openjdk/jdk repository.

The commit being backported was authored by Roger Riggs on 13 Feb 2024 and was reviewed by Alan Bateman and Claes Redestad.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk22u:

$ git fetch https://github.com/openjdk-bots/jdk22u.git backport-jaikiran-13d9e8ff:backport-jaikiran-13d9e8ff
$ git checkout backport-jaikiran-13d9e8ff
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk22u.git backport-jaikiran-13d9e8ff

⚠️ @jaikiran You are not yet a collaborator in my fork openjdk-bots/jdk22u. An invite will be sent out and you need to accept it before you can proceed.

Please sign in to comment.