Skip to content

Commit

Permalink
Use JLA string utilities for utf8 entry reading
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Sep 20, 2023
1 parent ec74194 commit 44028cb
Showing 1 changed file with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.classfile.Classfile;
import jdk.internal.classfile.constantpool.ClassEntry;
import jdk.internal.classfile.constantpool.ConstantDynamicEntry;
Expand All @@ -52,6 +54,8 @@
import jdk.internal.classfile.constantpool.PoolEntry;
import jdk.internal.classfile.constantpool.StringEntry;
import jdk.internal.classfile.constantpool.Utf8Entry;
import jdk.internal.util.ArraysSupport;

import java.lang.constant.ModuleDesc;
import java.lang.constant.PackageDesc;

Expand Down Expand Up @@ -142,6 +146,8 @@ public static final class Utf8EntryImpl extends AbstractPoolEntry implements Utf

enum State { RAW, BYTE, CHAR, STRING }

private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();

private State state;
private final byte[] rawBytes; // null if initialized directly from a string
private final int offset;
Expand Down Expand Up @@ -226,22 +232,9 @@ enum State { RAW, BYTE, CHAR, STRING }
* two-times-three-byte format instead.
*/
private void inflate() {
int hash = 0;
boolean foundHigh = false;

int px = offset;
int utfend = px + rawLen;
while (px < utfend) {
int c = (int) rawBytes[px] & 0xff;
if (c > 127) {
foundHigh = true;
break;
}
hash = 31 * hash + c;
px++;
}

if (!foundHigh) {
int singleBytes = JLA.countPositives(rawBytes, offset, rawLen);
int hash = ArraysSupport.vectorizedHashCode(rawBytes, offset, singleBytes, 0, ArraysSupport.T_BOOLEAN);
if (singleBytes == rawLen) {
this.hash = hashString(hash);
charLen = rawLen;
state = State.BYTE;
Expand All @@ -250,10 +243,10 @@ private void inflate() {
char[] chararr = new char[rawLen];
int chararr_count = 0;
// Inflate prefix of bytes to characters
for (int i = offset; i < px; i++) {
int c = (int) rawBytes[i] & 0xff;
chararr[chararr_count++] = (char) c;
}
JLA.inflateBytesToChars(rawBytes, offset, chararr, 0, singleBytes);

int px = offset + singleBytes;
int utfend = offset + rawLen;
while (px < utfend) {
int c = (int) rawBytes[px] & 0xff;
switch (c >> 4) {
Expand Down Expand Up @@ -331,7 +324,7 @@ public String toString() {
if (state != State.STRING) {
stringValue = (chars != null)
? new String(chars, 0, charLen)
: new String(rawBytes, offset, charLen, StandardCharsets.UTF_8);
: new String(rawBytes, offset, charLen, StandardCharsets.ISO_8859_1);
state = State.STRING;
}
return stringValue;
Expand Down

0 comments on commit 44028cb

Please sign in to comment.