Skip to content

Commit

Permalink
NoRepl -> ReportError
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Oct 20, 2023
1 parent 1af50e9 commit 3a94643
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 69 deletions.
34 changes: 13 additions & 21 deletions src/java.base/share/classes/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ private String(Charset charset, byte[] bytes, int offset, int length) {
* {@code false} if the byte array can be exclusively used to construct
* the string and is not modified or used for any other purpose.
*/
static String newStringUTF8FailFast(byte[] bytes, int offset, int length, boolean noShare) {
static String newStringUTF8ReportError(byte[] bytes, int offset, int length, boolean noShare) {
checkBoundsOffCount(offset, length, bytes.length);
if (length == 0) {
return "";
Expand Down Expand Up @@ -769,27 +769,23 @@ static String newStringUTF8FailFast(byte[] bytes, int offset, int length, boolea
return new String(dst, UTF16);
}

static String newStringNoRepl(byte[] src, Charset cs) throws CharacterCodingException {
static String newStringReportError(byte[] src, Charset cs) throws CharacterCodingException {
try {
return newStringNoRepl1(src, cs);
return newStringReportErrorUnchecked(src, cs);
} catch (IllegalArgumentException e) {
//newStringNoRepl1 throws IAE with MalformedInputException or CCE as the cause
Throwable cause = e.getCause();
if (cause instanceof MalformedInputException mie) {
throw mie;
}
throw (CharacterCodingException)cause;
//newStringReportErrorUnchecked throws IAE with MalformedInputException or CCE as the cause
throw (CharacterCodingException) e.getCause();
}
}

@SuppressWarnings("removal")
private static String newStringNoRepl1(byte[] src, Charset cs) {
private static String newStringReportErrorUnchecked(byte[] src, Charset cs) {
int len = src.length;
if (len == 0) {
return "";
}
if (cs == UTF_8.INSTANCE) {
return newStringUTF8FailFast(src, 0, src.length, false);
return newStringUTF8ReportError(src, 0, src.length, false);
}
if (cs == ISO_8859_1.INSTANCE) {
if (COMPACT_STRINGS)
Expand Down Expand Up @@ -933,7 +929,7 @@ private static byte[] encodeWithEncoder(Charset cs, byte coder, byte[] val, bool
/*
* Throws iae, instead of replacing, if unmappable.
*/
static byte[] getBytesUTF8FailFast(String s) {
static byte[] getBytesUTF8ReportError(String s) {
return encodeUTF8(s.coder(), s.value(), false);
}

Expand All @@ -944,20 +940,16 @@ private static boolean isASCII(byte[] src) {
/*
* Throws CCE, instead of replacing, if unmappable.
*/
static byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException {
static byte[] getBytesReportError(String s, Charset cs) throws CharacterCodingException {
try {
return getBytesNoRepl1(s, cs);
return getBytesReportErrorUnchecked(s, cs);
} catch (IllegalArgumentException e) {
//getBytesNoRepl1 throws IAE with UnmappableCharacterException or CCE as the cause
Throwable cause = e.getCause();
if (cause instanceof UnmappableCharacterException) {
throw (UnmappableCharacterException)cause;
}
throw (CharacterCodingException)cause;
//getBytesReportErrorUnchecked throws IAE with UnmappableCharacterException or CCE as the cause
throw (CharacterCodingException) e.getCause();
}
}

private static byte[] getBytesNoRepl1(String s, Charset cs) {
private static byte[] getBytesReportErrorUnchecked(String s, Charset cs) {
byte[] val = s.value();
byte coder = s.coder();
if (cs == UTF_8.INSTANCE) {
Expand Down
16 changes: 8 additions & 8 deletions src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -2473,22 +2473,22 @@ public Stream<ModuleLayer> layers(ClassLoader loader) {
public int countPositives(byte[] bytes, int offset, int length) {
return StringCoding.countPositives(bytes, offset, length);
}
public String newStringNoRepl(byte[] bytes, Charset cs) throws CharacterCodingException {
return String.newStringNoRepl(bytes, cs);
public String newStringReportError(byte[] bytes, Charset cs) throws CharacterCodingException {
return String.newStringReportError(bytes, cs);
}
public char getUTF16Char(byte[] bytes, int index) {
return StringUTF16.getChar(bytes, index);
}
public byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException {
return String.getBytesNoRepl(s, cs);
public byte[] getBytesReportError(String s, Charset cs) throws CharacterCodingException {
return String.getBytesReportError(s, cs);
}

public String newStringUTF8FailFast(byte[] bytes, int off, int len) {
return String.newStringUTF8FailFast(bytes, off, len, true);
public String newStringUTF8ReportError(byte[] bytes, int off, int len) {
return String.newStringUTF8ReportError(bytes, off, len, true);
}

public byte[] getBytesUTF8FailFast(String s) {
return String.getBytesUTF8FailFast(s);
public byte[] getBytesUTF8ReportError(String s) {
return String.getBytesUTF8ReportError(s);
}

public void inflateBytesToChars(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/nio/file/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -3351,7 +3351,7 @@ public static String readString(Path path, Charset cs) throws IOException {
byte[] ba = readAllBytes(path);
if (path.getClass().getModule() != Object.class.getModule())
ba = ba.clone();
return JLA.newStringNoRepl(ba, cs);
return JLA.newStringReportError(ba, cs);
}

/**
Expand Down Expand Up @@ -3713,7 +3713,7 @@ public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOpti
Objects.requireNonNull(csq);
Objects.requireNonNull(cs);

byte[] bytes = JLA.getBytesNoRepl(String.valueOf(csq), cs);
byte[] bytes = JLA.getBytesReportError(String.valueOf(csq), cs);
if (path.getClass().getModule() != Object.class.getModule())
bytes = bytes.clone();
write(path, bytes, options);
Expand Down
12 changes: 6 additions & 6 deletions src/java.base/share/classes/java/util/HexFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ private String formatOptDelimiter(byte[] bytes, int fromIndex, int toIndex) {
}
try {
// Return a new string using the bytes without making a copy
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.newStringReportError(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -701,7 +701,7 @@ public String toHexDigits(byte value) {
rep[0] = (byte)toHighHexDigit(value);
rep[1] = (byte)toLowHexDigit(value);
try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.newStringReportError(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -737,7 +737,7 @@ public String toHexDigits(short value) {
rep[3] = (byte)toLowHexDigit((byte)value);

try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.newStringReportError(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -765,7 +765,7 @@ public String toHexDigits(int value) {
rep[7] = (byte)toLowHexDigit((byte)value);

try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.newStringReportError(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -801,7 +801,7 @@ public String toHexDigits(long value) {
rep[15] = (byte)toLowHexDigit((byte)value);

try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.newStringReportError(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -829,7 +829,7 @@ public String toHexDigits(long value, int digits) {
value = value >>> 4;
}
try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.newStringReportError(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/UUID.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public String toString() {
HexDigits.packDigits(((int) lsb) >> 8, (int) lsb));

try {
return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);
return jla.newStringReportError(buf, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down
10 changes: 5 additions & 5 deletions src/java.base/share/classes/java/util/zip/ZipCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ boolean isUTF8() {

@Override
String toString(byte[] ba, int off, int length) {
return JLA.newStringUTF8FailFast(ba, off, length);
return JLA.newStringUTF8ReportError(ba, off, length);
}

@Override
byte[] getBytes(String s) {
return JLA.getBytesUTF8FailFast(s);
return JLA.getBytesUTF8ReportError(s);
}

@Override
Expand All @@ -286,9 +286,9 @@ int checkedHash(byte[] a, int off, int len) throws Exception {
// Non-ASCII, fall back to decoding a String
// We avoid using decoder() here since the UTF8ZipCoder is
// shared and that decoder is not thread safe.
// We use the JLA.newStringUTF8FailFast variant to throw
// We use the JLA.newStringUTF8ReportError variant to throw
// exceptions eagerly when opening ZipFiles
return hash(JLA.newStringUTF8FailFast(a, off, len));
return hash(JLA.newStringUTF8ReportError(a, off, len));
}
// T_BOOLEAN to treat the array as unsigned bytes, in line with StringLatin1.hashCode
int h = ArraysSupport.vectorizedHashCode(a, off, len, 0, ArraysSupport.T_BOOLEAN);
Expand All @@ -306,7 +306,7 @@ boolean hasTrailingSlash(byte[] a, int end) {
@Override
Comparison compare(String str, byte[] b, int off, int len, boolean matchDirectory) {
try {
byte[] encoded = JLA.getBytesNoRepl(str, UTF_8.INSTANCE);
byte[] encoded = JLA.getBytesReportError(str, UTF_8.INSTANCE);
int mismatch = Arrays.mismatch(encoded, 0, encoded.length, b, off, off+len);
if (mismatch == -1) {
return Comparison.EXACT_MATCH;
Expand Down
39 changes: 23 additions & 16 deletions src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,44 +305,48 @@ public interface JavaLangAccess {
int countPositives(byte[] ba, int off, int len);

/**
* Constructs a new {@code String} by decoding the specified subarray of
* bytes using the specified {@linkplain java.nio.charset.Charset charset}.
*
* The caller of this method shall relinquish and transfer the ownership of
* the byte array to the callee since the later will not make a copy.
* Constructs a new String by decoding the specified sequence of
* bytes using the specified Charset.
* <p>
* This method throws CharacterCodingException instead of replacing when
* any malformed input or unmappable character is encountered.
* <p>
* The input byte array must not be accessible to user code or modified.
*
* @param bytes the byte array source
* @param cs the Charset
* @return the newly created string
* @throws CharacterCodingException for malformed or unmappable bytes
*/
String newStringNoRepl(byte[] bytes, Charset cs) throws CharacterCodingException;
String newStringReportError(byte[] bytes, Charset cs) throws CharacterCodingException;

/**
* Encode the given string into a sequence of bytes using the specified Charset.
*
* This method avoids copying the String's internal representation if the input
* is ASCII.
*
* <p>
* This method throws CharacterCodingException instead of replacing when
* malformed input or unmappable characters are encountered.
* any malformed input or unmappable character is encountered.
* <p>
* The returned byte array must not be accessible to user code or modified.
*
* @param s the string to encode
* @param cs the charset
* @return the encoded bytes
* @throws CharacterCodingException for malformed input or unmappable characters
*/
byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException;
byte[] getBytesReportError(String s, Charset cs) throws CharacterCodingException;

/**
* Returns a new string by decoding from the given utf8 bytes array.
* Returns a new string by decoding from the given utf8 range in the bytes array.
* <p>
* This method throws CharacterCodingException instead of replacing when
* any malformed input or unmappable character is encountered.
*
* @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 newStringUTF8FailFast(byte[] bytes, int off, int len);
String newStringUTF8ReportError(byte[] bytes, int off, int len);

/**
* Get the char at index in a byte[] in internal UTF-16 representation,
Expand All @@ -355,13 +359,16 @@ public interface JavaLangAccess {
char getUTF16Char(byte[] bytes, int index);

/**
* Encode the given string into a sequence of bytes using utf8.
* Encode the given string into a sequence of bytes in utf8.
* <p>
* This method throws CharacterCodingException instead of replacing when
* any malformed input or unmappable character is encountered.
*
* @param s the string to encode
* @return the encoded bytes in utf8
* @throws IllegalArgumentException for malformed surrogates
*/
byte[] getBytesUTF8FailFast(String s);
byte[] getBytesUTF8ReportError(String s);

/**
* Inflated copy from byte[] to char[], as defined by StringLatin1.inflate
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/unix/classes/sun/nio/fs/UnixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private static String normalize(String input, int len, int off) {
private static byte[] encode(UnixFileSystem fs, String input) {
input = fs.normalizeNativePath(input);
try {
return JLA.getBytesNoRepl(input, Util.jnuEncoding());
return JLA.getBytesReportError(input, Util.jnuEncoding());
} catch (CharacterCodingException cce) {
throw new InvalidPathException(input,
"Malformed input or input contains unmappable characters");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023, 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 All @@ -23,9 +23,9 @@

/*
* @test
* @bug 8286287 8288589
* @summary Tests for *NoRepl() shared secret methods.
* @run testng NoReplTest
* @bug 8286287 8288589 8318486
* @summary Tests for *ReportError() shared secret methods.
* @run testng ReportErrorTest
* @modules jdk.charsets
*/

Expand All @@ -39,17 +39,17 @@
import org.testng.annotations.Test;

@Test
public class NoReplTest {
public class ReportErrorTest {
private final static byte[] MALFORMED_UTF16 = {(byte)0x00, (byte)0x20, (byte)0x00};
private final static String MALFORMED_WINDOWS_1252 = "\u0080\u041e";
private final static Charset WINDOWS_1252 = Charset.forName("windows-1252");

/**
* Verifies newStringNoRepl() throws a CharacterCodingException.
* Verifies newStringReportError() throws a CharacterCodingException.
* The method is invoked by `Files.readString()` method.
*/
@Test
public void newStringNoReplTest() throws IOException {
public void newStringReportErrorTest() throws IOException {
var f = Files.createTempFile(null, null);
try (var fos = Files.newOutputStream(f)) {
fos.write(MALFORMED_UTF16);
Expand All @@ -67,11 +67,11 @@ public void newStringNoReplTest() throws IOException {
}

/**
* Verifies getBytesNoRepl() throws a CharacterCodingException.
* Verifies getBytesReportError() throws a CharacterCodingException.
* The method is invoked by `Files.writeString()` method.
*/
@Test
public void getBytesNoReplTest() throws IOException {
public void getBytesReportErrorTest() throws IOException {
var f = Files.createTempFile(null, null);
try {
Files.writeString(f, MALFORMED_WINDOWS_1252, WINDOWS_1252);
Expand Down

0 comments on commit 3a94643

Please sign in to comment.