8289908: Skip bounds check for cases when String is constructed from entirely used byte[]

Reviewed-by: prr, rriggs, aturbanov
This commit is contained in:
Sergey Tsypanov 2022-07-18 17:15:46 +00:00 committed by Phil Race
parent b2010a7481
commit efed7a7f65
5 changed files with 96 additions and 13 deletions

View file

@ -484,7 +484,7 @@ public final class String
*/
public String(byte[] bytes, int offset, int length, String charsetName)
throws UnsupportedEncodingException {
this(bytes, offset, length, lookupCharset(charsetName));
this(lookupCharset(charsetName), bytes, checkBoundsOffCount(offset, length, bytes.length), length);
}
/**
@ -517,10 +517,18 @@ public final class String
*
* @since 1.6
*/
@SuppressWarnings("removal")
public String(byte[] bytes, int offset, int length, Charset charset) {
Objects.requireNonNull(charset);
checkBoundsOffCount(offset, length, bytes.length);
this(Objects.requireNonNull(charset), bytes, checkBoundsOffCount(offset, length, bytes.length), length);
}
/**
* This method does not do any precondition checks on its arguments.
* <p>
* Important: parameter order of this method is deliberately changed in order to
* disambiguate it against other similar methods of this class.
*/
@SuppressWarnings("removal")
private String(Charset charset, byte[] bytes, int offset, int length) {
if (length == 0) {
this.value = "".value;
this.coder = "".coder;
@ -1370,7 +1378,7 @@ public final class String
*/
public String(byte[] bytes, String charsetName)
throws UnsupportedEncodingException {
this(bytes, 0, bytes.length, charsetName);
this(lookupCharset(charsetName), bytes, 0, bytes.length);
}
/**
@ -1394,7 +1402,7 @@ public final class String
* @since 1.6
*/
public String(byte[] bytes, Charset charset) {
this(bytes, 0, bytes.length, charset);
this(Objects.requireNonNull(charset), bytes, 0, bytes.length);
}
/**
@ -1424,7 +1432,7 @@ public final class String
* @since 1.1
*/
public String(byte[] bytes, int offset, int length) {
this(bytes, offset, length, Charset.defaultCharset());
this(Charset.defaultCharset(), bytes, checkBoundsOffCount(offset, length, bytes.length), length);
}
/**
@ -1444,7 +1452,7 @@ public final class String
* @since 1.1
*/
public String(byte[] bytes) {
this(bytes, 0, bytes.length);
this(Charset.defaultCharset(), bytes, 0, bytes.length);
}
/**
@ -4582,12 +4590,13 @@ public final class String
* Check {@code offset}, {@code count} against {@code 0} and {@code length}
* bounds.
*
* @return {@code offset} if the sub-range within bounds of the range
* @throws StringIndexOutOfBoundsException
* If {@code offset} is negative, {@code count} is negative,
* or {@code offset} is greater than {@code length - count}
*/
static void checkBoundsOffCount(int offset, int count, int length) {
Preconditions.checkFromIndexSize(offset, count, length, Preconditions.SIOOBE_FORMATTER);
static int checkBoundsOffCount(int offset, int count, int length) {
return Preconditions.checkFromIndexSize(offset, count, length, Preconditions.SIOOBE_FORMATTER);
}
/*