Skip to content

Commit

Permalink
Extract common string construction method in JDK 8 to reduce duplicat…
Browse files Browse the repository at this point in the history
…e code
  • Loading branch information
wenshao committed Jan 12, 2025
1 parent 4a2ed12 commit 2d75abe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 59 deletions.
49 changes: 10 additions & 39 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,7 @@ public String getString() {
charset = ISO_8859_1;
} else if (strtype >= BC_STR_ASCII_FIX_MIN && strtype <= BC_STR_ASCII_FIX_MAX) {
if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[strlen];
for (int i = 0; i < strlen; ++i) {
chars[i] = (char) (bytes[strBegin + i] & 0xff);
}
return STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
return latin1StringJDK8(bytes, strBegin, strlen);
} else if (STRING_CREATOR_JDK11 != null) {
byte[] chars = new byte[strlen];
System.arraycopy(bytes, strBegin, chars, 0, strlen);
Expand Down Expand Up @@ -962,13 +958,8 @@ public Object readAny() {
}

if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[strlen];
for (int i = 0; i < strlen; ++i) {
chars[i] = (char) (bytes[offset + i] & 0xff);
}
String str = latin1StringJDK8(bytes, offset, strlen);
offset += strlen;

String str = STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
if ((context.features & Feature.TrimString.mask) != 0) {
str = str.trim();
}
Expand Down Expand Up @@ -2800,11 +2791,7 @@ public String readFieldName() {
if (entry == null) {
String name;
if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[strlen];
for (int i = 0; i < strlen; ++i) {
chars[i] = (char) (bytes[offset + i] & 0xff);
}
name = STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
name = latin1StringJDK8(bytes, offset, strlen);
} else {
name = new String(bytes, offset, strlen, ISO_8859_1);
}
Expand All @@ -2822,11 +2809,7 @@ public String readFieldName() {
if (entry == null) {
String name;
if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[strlen];
for (int i = 0; i < strlen; ++i) {
chars[i] = (char) (bytes[offset + i] & 0xff);
}
name = STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
name = latin1StringJDK8(bytes, offset, strlen);
} else {
name = new String(bytes, offset, strlen, ISO_8859_1);
}
Expand All @@ -2844,12 +2827,8 @@ public String readFieldName() {
if (str == null) {
if (strlen >= 0) {
if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[strlen];
for (int i = 0; i < strlen; ++i) {
chars[i] = (char) (bytes[offset + i] & 0xff);
}
str = latin1StringJDK8(bytes, offset, strlen);
offset += strlen;
str = STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
} else if (STRING_CREATOR_JDK11 != null) {
byte[] chars = new byte[strlen];
System.arraycopy(bytes, offset, chars, 0, strlen);
Expand Down Expand Up @@ -2969,7 +2948,7 @@ public String readString() {
}

strBegin = offset;
String str = null;

if (strtype >= BC_STR_ASCII_FIX_MIN && strtype <= BC_STR_ASCII) {
final int strlen;
if (strtype == BC_STR_ASCII) {
Expand All @@ -2986,19 +2965,19 @@ public String readString() {
}
this.strlen = strlen;

String str = null;
if (strlen >= 0) {
if (STRING_CREATOR_JDK11 != null) {
byte[] chars = new byte[strlen];
System.arraycopy(bytes, offset, chars, 0, strlen);
str = STRING_CREATOR_JDK11.apply(chars, LATIN1);
offset += strlen;
} else if (STRING_CREATOR_JDK8 != null) {
str = readStringJDK8(strlen, bytes);
offset += strlen;
str = latin1StringJDK8(bytes, offset, strlen);
}
}

if (str != null) {
offset += strlen;
if ((context.features & Feature.TrimString.mask) != 0) {
str = str.trim();
}
Expand All @@ -3013,14 +2992,6 @@ public String readString() {
return readStringNonAscii();
}

private String readStringJDK8(int strlen, byte[] bytes) {
char[] chars = new char[strlen];
for (int i = 0; i < strlen; ++i) {
chars[i] = (char) (bytes[offset + i] & 0xff);
}
return STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
}

private String readStringNonAscii() {
String str = null;
Charset charset;
Expand Down Expand Up @@ -3849,7 +3820,7 @@ protected String readFixedAsciiString(int strlen) {
(char) (bytes[offset + 1] & 0xff)
);
} else if (STRING_CREATOR_JDK8 != null) {
str = readStringJDK8(strlen, bytes);
str = latin1StringJDK8(bytes, offset, strlen);
} else {
str = new String(bytes, offset, strlen, ISO_8859_1);
}
Expand Down
24 changes: 4 additions & 20 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -3060,11 +3060,7 @@ public String getFieldName() {
if (!nameEscape) {
if (nameAscii) {
if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[length];
for (int i = 0; i < length; ++i) {
chars[i] = (char) bytes[offset + i];
}
return STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
return JDKUtils.asciiStringJDK8(bytes, offset, length);
} else if (STRING_CREATOR_JDK11 != null) {
return STRING_CREATOR_JDK11.apply(
Arrays.copyOfRange(bytes, offset, nameEnd),
Expand Down Expand Up @@ -3434,11 +3430,7 @@ public String readFieldName() {
if (entry == null) {
String name;
if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[length];
for (int i = 0; i < length; ++i) {
chars[i] = (char) bytes[nameBegin + i];
}
name = STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
name = asciiStringJDK8(bytes, nameBegin, length);
} else if (ANDROID) {
name = getLatin1String(nameBegin, length);
} else {
Expand All @@ -3456,11 +3448,7 @@ public String readFieldName() {
if (entry == null) {
String name;
if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[length];
for (int i = 0; i < length; ++i) {
chars[i] = (char) bytes[nameBegin + i];
}
name = STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
name = asciiStringJDK8(bytes, nameBegin, length);
} else if (ANDROID) {
name = getLatin1String(nameBegin, length);
} else {
Expand All @@ -3476,11 +3464,7 @@ public String readFieldName() {
}

if (STRING_CREATOR_JDK8 != null) {
char[] chars = new char[length];
for (int i = 0; i < length; ++i) {
chars[i] = (char) bytes[nameBegin + i];
}
return STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
return asciiStringJDK8(bytes, nameBegin, length);
} else if (ANDROID) {
return getLatin1String(nameBegin, nameEnd - nameBegin);
} else if (STRING_CREATOR_JDK11 != null) {
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,20 @@ public static MethodHandles.Lookup trustedLookup(Class objectClass) {

return IMPL_LOOKUP.in(objectClass);
}

public static String asciiStringJDK8(byte[] bytes, int offset, int strlen) {
char[] chars = new char[strlen];
for (int i = 0; i < strlen; ++i) {
chars[i] = (char) bytes[offset + i];
}
return STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
}

public static String latin1StringJDK8(byte[] bytes, int offset, int strlen) {
char[] chars = new char[strlen];
for (int i = 0; i < strlen; ++i) {
chars[i] = (char) (bytes[offset + i] & 0xff);
}
return STRING_CREATOR_JDK8.apply(chars, Boolean.TRUE);
}
}

0 comments on commit 2d75abe

Please sign in to comment.