8340232: Optimize DataInputStream::readUTF
Reviewed-by: liach, bpb
This commit is contained in:
parent
ab81197d0d
commit
40fba14812
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -25,8 +26,11 @@
|
|||||||
|
|
||||||
package java.io;
|
package java.io;
|
||||||
|
|
||||||
|
import jdk.internal.access.JavaLangAccess;
|
||||||
|
import jdk.internal.access.SharedSecrets;
|
||||||
import jdk.internal.util.ByteArray;
|
import jdk.internal.util.ByteArray;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,6 +49,7 @@ import java.util.Objects;
|
|||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public class DataInputStream extends FilterInputStream implements DataInput {
|
public class DataInputStream extends FilterInputStream implements DataInput {
|
||||||
|
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
|
||||||
|
|
||||||
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
||||||
private static final char[] EMPTY_CHAR_ARRAY = new char[0];
|
private static final char[] EMPTY_CHAR_ARRAY = new char[0];
|
||||||
@ -573,18 +578,16 @@ loop: while (true) {
|
|||||||
*/
|
*/
|
||||||
public static final String readUTF(DataInput in) throws IOException {
|
public static final String readUTF(DataInput in) throws IOException {
|
||||||
int utflen = in.readUnsignedShort();
|
int utflen = in.readUnsignedShort();
|
||||||
byte[] bytearr;
|
byte[] bytearr = null;
|
||||||
char[] chararr;
|
|
||||||
if (in instanceof DataInputStream dis) {
|
if (in instanceof DataInputStream dis) {
|
||||||
if (dis.bytearr.length < utflen) {
|
if (dis.bytearr.length >= utflen) {
|
||||||
dis.bytearr = new byte[utflen*2];
|
bytearr = dis.bytearr;
|
||||||
dis.chararr = new char[utflen*2];
|
|
||||||
}
|
}
|
||||||
chararr = dis.chararr;
|
}
|
||||||
bytearr = dis.bytearr;
|
boolean trusted = false;
|
||||||
} else {
|
if (bytearr == null) {
|
||||||
bytearr = new byte[utflen];
|
bytearr = new byte[utflen];
|
||||||
chararr = new char[utflen];
|
trusted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int c, char2, char3;
|
int c, char2, char3;
|
||||||
@ -592,12 +595,35 @@ loop: while (true) {
|
|||||||
int chararr_count=0;
|
int chararr_count=0;
|
||||||
|
|
||||||
in.readFully(bytearr, 0, utflen);
|
in.readFully(bytearr, 0, utflen);
|
||||||
|
int ascii = JLA.countPositives(bytearr, 0, utflen);
|
||||||
|
if (ascii == utflen) {
|
||||||
|
String str;
|
||||||
|
if (trusted) {
|
||||||
|
str = JLA.newStringNoRepl(bytearr, StandardCharsets.ISO_8859_1);
|
||||||
|
} else {
|
||||||
|
str = new String(bytearr, 0, utflen, StandardCharsets.ISO_8859_1);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
if (trusted && in instanceof DataInputStream dis) {
|
||||||
|
dis.bytearr = bytearr;
|
||||||
|
trusted = false;
|
||||||
|
}
|
||||||
|
|
||||||
while (count < utflen) {
|
char[] chararr;
|
||||||
c = (int) bytearr[count] & 0xff;
|
if (in instanceof DataInputStream dis) {
|
||||||
if (c > 127) break;
|
if (dis.chararr.length < (utflen << 1)) {
|
||||||
count++;
|
dis.chararr = new char[utflen << 1];
|
||||||
chararr[chararr_count++]=(char)c;
|
}
|
||||||
|
chararr = dis.chararr;
|
||||||
|
} else {
|
||||||
|
chararr = new char[utflen];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ascii != 0) {
|
||||||
|
JLA.inflateBytesToChars(bytearr, 0, chararr, 0, ascii);
|
||||||
|
count += ascii;
|
||||||
|
chararr_count += ascii;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (count < utflen) {
|
while (count < utflen) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user