8365703: Refactor ZipCoder to use common JLA.uncheckedNewStringNoRepl

Reviewed-by: lancea, vyazici
This commit is contained in:
Roger Riggs 2025-08-19 23:33:40 +00:00
parent 3bbaa772b0
commit 55e7494dee
4 changed files with 27 additions and 24 deletions

View File

@ -690,13 +690,10 @@ public final class String
/*
* Throws iae, instead of replacing, if malformed or unmappable.
*
* @param noShare
* {@code true} if the resulting string MUST NOT share the byte array,
* {@code false} if the byte array can be exclusively used to construct
* the string and is not modified or used for any other purpose.
* The byte array can be exclusively used to construct
* the string and is not modified or used for any other purpose.
*/
static String newStringUTF8NoRepl(byte[] bytes, int offset, int length, boolean noShare) {
private static String newStringUTF8NoRepl(byte[] bytes, int offset, int length) {
checkBoundsOffCount(offset, length, bytes.length);
if (length == 0) {
return "";
@ -707,7 +704,7 @@ public final class String
dp = StringCoding.countPositives(bytes, offset, length);
int sl = offset + length;
if (dp == length) {
if (noShare || length != bytes.length) {
if (length != bytes.length) {
return new String(Arrays.copyOfRange(bytes, offset, offset + length), LATIN1);
} else {
return new String(bytes, LATIN1);
@ -774,6 +771,19 @@ public final class String
return new String(StringLatin1.inflate(src, 0, src.length), UTF16);
}
/**
* {@return a new {@code String} created using the given byte array that is
* encoded in specified charset}
* <p>
* <b>WARNING: The caller of this method is assumed to have relinquished
* and transferred the ownership of the byte array</b>. It can thus be
* exclusively used to construct the {@code String}.
*
* @param src byte array containing encoded characters
* @param cs charset the byte array encoded in
*
* @throws CharacterCodingException for malformed input or unmappable characters
*/
static String newStringNoRepl(byte[] src, Charset cs) throws CharacterCodingException {
try {
return newStringNoRepl1(src, cs);
@ -793,7 +803,7 @@ public final class String
return "";
}
if (cs == UTF_8.INSTANCE) {
return newStringUTF8NoRepl(src, 0, src.length, false);
return newStringUTF8NoRepl(src, 0, src.length);
}
if (cs == ISO_8859_1.INSTANCE) {
if (COMPACT_STRINGS)

View File

@ -2144,10 +2144,6 @@ public final class System {
return String.getBytesNoRepl(s, cs);
}
public String newStringUTF8NoRepl(byte[] bytes, int off, int len) {
return String.newStringUTF8NoRepl(bytes, off, len, true);
}
public byte[] getBytesUTF8NoRepl(String s) {
return String.getBytesUTF8NoRepl(s);
}

View File

@ -32,6 +32,7 @@ import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import jdk.internal.util.ArraysSupport;
@ -252,7 +253,13 @@ class ZipCoder {
@Override
String toString(byte[] ba, int off, int length) {
return JLA.newStringUTF8NoRepl(ba, off, length);
try {
// Copy subrange for exclusive use by the string being created
byte[] bytes = Arrays.copyOfRange(ba, off, off + length);
return JLA.uncheckedNewStringNoRepl(bytes, StandardCharsets.UTF_8);
} catch (CharacterCodingException cce) {
throw new IllegalArgumentException(cce);
}
}
@Override
@ -273,7 +280,7 @@ class ZipCoder {
// shared and that decoder is not thread safe.
// We use the JLA.newStringUTF8NoRepl variant to throw
// exceptions eagerly when opening ZipFiles
return hash(JLA.newStringUTF8NoRepl(a, off, len));
return hash(toString(a, off, len));
}
int h = ArraysSupport.hashCodeOfUnsigned(a, off, len, 0);
if (a[end - 1] != '/') {

View File

@ -363,16 +363,6 @@ public interface JavaLangAccess {
*/
byte[] uncheckedGetBytesNoRepl(String s, Charset cs) throws CharacterCodingException;
/**
* Returns a new string by decoding from the given UTF-8 bytes array.
*
* @param off the index of the first byte to decode
* @param len the number of bytes to decode
* @return the newly created string
* @throws IllegalArgumentException for malformed or unmappable bytes.
*/
String newStringUTF8NoRepl(byte[] bytes, int off, int len);
/**
* Get the {@code char} at {@code index} in a {@code byte[]} in internal
* UTF-16 representation.