Skip to content

Commit

Permalink
refactor writeInt32
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 15, 2025
1 parent d131f91 commit 0d984da
Showing 1 changed file with 66 additions and 70 deletions.
136 changes: 66 additions & 70 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ public void writeString(char[] chars, int coff, int len, boolean quote) {
if (len <= STR_ASCII_FIX_LEN) {
bytes[off++] = (byte) (len + BC_STR_ASCII_FIX_MIN);
} else {
bytes[off++] = BC_STR_ASCII;
off += writeInt32(bytes, off, len);
bytes[off] = BC_STR_ASCII;
off = writeInt32(bytes, off + 1, len);
}
for (int i = 0; i < len; ++i) {
bytes[off++] = (byte) chars[coff + i];
Expand Down Expand Up @@ -668,7 +668,7 @@ public void writeString(final char[] chars) {
bytes[off + 1] = (byte) (utf8len);
off += 2;
} else {
off += writeInt32(bytes, off, utf8len);
off = writeInt32(bytes, off, utf8len);
}
off += utf8len;
}
Expand Down Expand Up @@ -751,8 +751,8 @@ public void writeString(final char[] chars, final int charsOff, final int len) {
bytes[off + 2] = (byte) (len);
off += 3;
} else {
bytes[off++] = BC_STR_ASCII;
off += writeInt32(bytes, off, len);
bytes[off] = BC_STR_ASCII;
off = writeInt32(bytes, off + 1, len);
}
for (int i = 0; i < chars.length; i++) {
bytes[off++] = (byte) chars[i];
Expand All @@ -779,7 +779,7 @@ public void writeString(final char[] chars, final int charsOff, final int len) {
bytes[off + 1] = (byte) (utf8len);
off += 2;
} else {
off += writeInt32(bytes, off, utf8len);
off = writeInt32(bytes, off, utf8len);
}
off += utf8len;
}
Expand Down Expand Up @@ -853,7 +853,7 @@ public void writeTypeName(String typeName) {
bytes = grow(off + 1);
}

this.off = off + writeInt32(bytes, off, symbol);
this.off = writeInt32(bytes, off, symbol);
return;
}
this.off = off;
Expand Down Expand Up @@ -911,7 +911,7 @@ public boolean writeTypeName(byte[] typeName, long hash) {
if (symbol >= BC_INT32_NUM_MIN && symbol <= BC_INT32_NUM_MAX) {
bytes[off++] = (byte) symbol;
} else {
off += writeInt32(bytes, off, symbol);
off = writeInt32(bytes, off, symbol);
}
this.off = off;
return false;
Expand All @@ -924,8 +924,8 @@ private boolean writeTypeNameSymbol(int symbol) {
bytes = grow(off + 7);
}

bytes[off++] = BC_TYPED_ANY;
this.off = off + writeInt32(bytes, off, -symbol);
bytes[off] = BC_TYPED_ANY;
this.off = writeInt32(bytes, off + 1, -symbol);
return false;
}

Expand Down Expand Up @@ -985,8 +985,8 @@ public void writeString(List<String> list) {
bytes[off + 2] = (byte) (strlen);
off += 3;
} else {
bytes[off++] = BC_STR_ASCII;
off += writeInt32(bytes, off, strlen);
bytes[off] = BC_STR_ASCII;
off = writeInt32(bytes, off + 1, strlen);
}
byte[] value = STRING_VALUE.apply(str);
System.arraycopy(value, 0, bytes, off, value.length);
Expand Down Expand Up @@ -1056,8 +1056,8 @@ public void writeStringUTF16(byte[] value) {
if (minCapacity > bytes.length) {
bytes = grow(minCapacity);
}
bytes[off++] = JDKUtils.BIG_ENDIAN ? BC_STR_UTF16BE : BC_STR_UTF16LE;
off += writeInt32(bytes, off, strlen);
bytes[off] = JDKUtils.BIG_ENDIAN ? BC_STR_UTF16BE : BC_STR_UTF16LE;
off = writeInt32(bytes, off + 1, strlen);
System.arraycopy(value, 0, bytes, off, strlen);
this.off = off + strlen;
}
Expand Down Expand Up @@ -1130,15 +1130,18 @@ private static int writeUTF8(
if (lenByteCnt != utf8lenByteCnt) {
System.arraycopy(bytes, off + lenByteCnt + 1, bytes, off + utf8lenByteCnt + 1, utf8len);
}
int start = off;
bytes[off] = strtype;
return writeInt32(bytes, off + 1, utf8len) + utf8len + 1;
off = writeInt32(bytes, off + 1, utf8len);
return (off - start) + utf8len;
}

private static int writeUTF16(byte[] bytes, int off, byte[] value) {
int start = off;
bytes[off] = JDKUtils.BIG_ENDIAN ? BC_STR_UTF16BE : BC_STR_UTF16LE;
int size = writeInt32(bytes, off + 1, value.length);
System.arraycopy(value, 0, bytes, off + size + 1, value.length);
return value.length + size + 1;
off = writeInt32(bytes, off + 1, value.length);
System.arraycopy(value, 0, bytes, off, value.length);
return value.length + off - start;
}

void ensureCapacity(int minCapacity) {
Expand Down Expand Up @@ -1225,6 +1228,25 @@ public void writeInt64(Long i) {
this.off = off;
}

public static int writeInt32(byte[] bytes, int off, int i) {
if (i >= BC_INT32_NUM_MIN && i <= BC_INT32_NUM_MAX) {
bytes[off++] = (byte) i;
} else if (i >= INT32_BYTE_MIN && i <= INT32_BYTE_MAX) {
IOUtils.putShortBE(bytes, off, (short) ((BC_INT32_BYTE_ZERO << 8) + i));
off += 2;
} else if (i >= INT32_SHORT_MIN && i <= INT32_SHORT_MAX) {
bytes[off] = (byte) (BC_INT32_SHORT_ZERO + (i >> 16));
IOUtils.putShortBE(bytes, off + 1, (short) i);
off += 3;
} else {
bytes[off] = BC_INT32;
IOUtils.putIntBE(bytes, off + 1, i);
off += 5;
}

return off;
}

private static int writeInt64(byte[] bytes, int off, long i) {
if (i >= INT64_NUM_LOW_VALUE && i <= INT64_NUM_HIGH_VALUE) {
bytes[off++] = (byte) (BC_INT64_NUM_MIN + (i - INT64_NUM_LOW_VALUE));
Expand Down Expand Up @@ -1280,7 +1302,7 @@ public void writeInt64(long[] value) {
bytes[off++] = (byte) (BC_ARRAY_FIX_MIN + size);
} else {
bytes[off] = BC_ARRAY;
off += writeInt32(bytes, off + 1, size) + 1;
off = writeInt32(bytes, off + 1, size);
}

for (int i = 0; i < value.length; i++) {
Expand Down Expand Up @@ -1308,7 +1330,7 @@ public void writeListInt64(List<Long> values) {
bytes[off++] = (byte) (BC_ARRAY_FIX_MIN + size);
} else {
bytes[off] = BC_ARRAY;
off += writeInt32(bytes, off + 1, size) + 1;
off = writeInt32(bytes, off + 1, size);
}

for (int i = 0; i < size; i++) {
Expand Down Expand Up @@ -1437,8 +1459,8 @@ public void writeInt32(int[] values) {
if (size <= ARRAY_FIX_LEN) {
bytes[off++] = (byte) (BC_ARRAY_FIX_MIN + size);
} else {
bytes[off++] = BC_ARRAY;
off += writeInt32(bytes, off, size);
bytes[off] = BC_ARRAY;
off = writeInt32(bytes, off + 1, size);
}

for (int i = 0; i < values.length; i++) {
Expand Down Expand Up @@ -1487,8 +1509,8 @@ public void writeInt8(byte[] values) {
if (size <= ARRAY_FIX_LEN) {
bytes[off++] = (byte) (BC_ARRAY_FIX_MIN + size);
} else {
bytes[off++] = BC_ARRAY;
off += writeInt32(bytes, off, size);
bytes[off] = BC_ARRAY;
off = writeInt32(bytes, off + 1, size);
}

for (int val : values) {
Expand Down Expand Up @@ -1545,7 +1567,7 @@ public void writeEnum(Enum e) {
if (off + 5 > bytes.length) {
bytes = grow(off + 5);
}
this.off = off + writeInt32(bytes, off, ordinal);
this.off = writeInt32(bytes, off, ordinal);
}
}

Expand Down Expand Up @@ -1636,7 +1658,7 @@ public void writeListInt32(List<Integer> values) {
bytes[off++] = (byte) (BC_ARRAY_FIX_MIN + size);
} else {
bytes[off] = BC_ARRAY;
off += writeInt32(bytes, off + 1, size) + 1;
off = writeInt32(bytes, off + 1, size);
}

for (int i = 0; i < size; i++) {
Expand Down Expand Up @@ -1667,26 +1689,6 @@ public void writeListInt32(List<Integer> values) {
this.off = off;
}

public static int writeInt32(byte[] bytes, int off, int val) {
if (val >= BC_INT32_NUM_MIN && val <= BC_INT32_NUM_MAX) {
bytes[off] = (byte) val;
return 1;
} else if (val >= INT32_BYTE_MIN && val <= INT32_BYTE_MAX) {
bytes[off] = (byte) (BC_INT32_BYTE_ZERO + (val >> 8));
bytes[off + 1] = (byte) (val);
return 2;
} else if (val >= INT32_SHORT_MIN && val <= INT32_SHORT_MAX) {
bytes[off] = (byte) (BC_INT32_SHORT_ZERO + (val >> 16));
bytes[off + 1] = (byte) (val >> 8);
bytes[off + 2] = (byte) (val);
return 3;
} else {
bytes[off] = BC_INT32;
IOUtils.putIntBE(bytes, off + 1, val);
return 5;
}
}

@Override
public void writeArrayNull() {
if (off == bytes.length) {
Expand Down Expand Up @@ -1729,7 +1731,7 @@ public void writeSymbol(int symbol) {
bytes[off + 1] = (byte) (symbol);
off += 2;
} else {
off += writeInt32(bytes, off, symbol);
off = writeInt32(bytes, off, symbol);
}
this.off = off;
}
Expand Down Expand Up @@ -1772,7 +1774,7 @@ public void writeNameRaw(byte[] name, long nameHash) {
if (symbol >= BC_INT32_NUM_MIN && symbol <= BC_INT32_NUM_MAX) {
bytes[off++] = (byte) symbol;
} else {
off += writeInt32(bytes, off, symbol);
off = writeInt32(bytes, off, symbol);
}
this.off = off;
return;
Expand All @@ -1785,7 +1787,7 @@ public void writeNameRaw(byte[] name, long nameHash) {
if (intValue >= BC_INT32_NUM_MIN && intValue <= BC_INT32_NUM_MAX) {
bytes[off++] = (byte) intValue;
} else {
off += writeInt32(bytes, off, intValue);
off = writeInt32(bytes, off, intValue);
}
this.off = off;
}
Expand Down Expand Up @@ -1829,9 +1831,7 @@ public void writeLocalTime(LocalTime time) {
bytes[off + 1] = (byte) time.getHour();
bytes[off + 2] = (byte) time.getMinute();
bytes[off + 3] = (byte) time.getSecond();
off += 4;

this.off = off + writeInt32(bytes, off, time.getNano());
this.off = writeInt32(bytes, off + 4, time.getNano());
}

@Override
Expand All @@ -1856,9 +1856,7 @@ public void writeLocalDateTime(LocalDateTime dateTime) {
bytes[off + 5] = (byte) dateTime.getHour();
bytes[off + 6] = (byte) dateTime.getMinute();
bytes[off + 7] = (byte) dateTime.getSecond();
off += 8;

this.off = off + writeInt32(bytes, off, dateTime.getNano());
this.off = writeInt32(bytes, off + 8, dateTime.getNano());
}

@Override
Expand All @@ -1883,9 +1881,8 @@ public void writeZonedDateTime(ZonedDateTime dateTime) {
bytes[off + 5] = (byte) dateTime.getHour();
bytes[off + 6] = (byte) dateTime.getMinute();
bytes[off + 7] = (byte) dateTime.getSecond();
off += 8;

this.off = off + writeInt32(bytes, off, dateTime.getNano());
this.off = writeInt32(bytes, off + 8, dateTime.getNano());

ZoneId zoneId = dateTime.getZone();
String zoneIdStr = zoneId.getId();
Expand Down Expand Up @@ -1922,9 +1919,8 @@ public void writeOffsetDateTime(OffsetDateTime dateTime) {
bytes[off + 5] = (byte) dateTime.getHour();
bytes[off + 6] = (byte) dateTime.getMinute();
bytes[off + 7] = (byte) dateTime.getSecond();
off += 8;

off += writeInt32(bytes, off, dateTime.getNano());
off = writeInt32(bytes, off + 8, dateTime.getNano());

bytes[off++] = (byte) (strlen + BC_STR_ASCII_FIX_MIN);
zoneIdStr.getBytes(0, strlen, bytes, off);
Expand Down Expand Up @@ -1958,7 +1954,7 @@ public void writeInstant(Instant instant) {

bytes[off] = BC_TIMESTAMP;
off = writeInt64(bytes, off + 1, instant.getEpochSecond());
this.off = off + writeInt32(bytes, off, instant.getNano());
this.off = writeInt32(bytes, off, instant.getNano());
}

@Override
Expand Down Expand Up @@ -2005,8 +2001,8 @@ public void writeBigInt(BigInteger value, long features) {
bytes = grow(minCapacity);
}

bytes[off++] = BC_BIGINT;
off += writeInt32(bytes, off, valueBytes.length);
bytes[off] = BC_BIGINT;
off = writeInt32(bytes, off + 1, valueBytes.length);
System.arraycopy(valueBytes, 0, bytes, off, valueBytes.length);
this.off = off + valueBytes.length;
}
Expand All @@ -2025,8 +2021,8 @@ public void writeBinary(byte[] binary) {
if (minCapacity > bytes.length) {
bytes = grow(minCapacity);
}
bytes[off++] = BC_BINARY;
off += writeInt32(bytes, off, len);
bytes[off] = BC_BINARY;
off = writeInt32(bytes, off + 1, len);

System.arraycopy(binary, 0, bytes, off, len);
this.off = off + len;
Expand Down Expand Up @@ -2055,10 +2051,10 @@ public void writeDecimal(BigDecimal value, long features, DecimalFormat format)
return;
}

bytes[off++] = BC_DECIMAL;
off += writeInt32(bytes, off, scale);
bytes[off] = BC_DECIMAL;
off = writeInt32(bytes, off + 1, scale);
if (intCompact >= Integer.MIN_VALUE && intCompact <= Integer.MAX_VALUE) {
off += writeInt32(bytes, off, (int) intCompact);
off = writeInt32(bytes, off, (int) intCompact);
} else {
off = writeInt64(bytes, off, intCompact);
}
Expand All @@ -2074,11 +2070,11 @@ && isInt64(unscaledValue)) {
return;
}

bytes[off++] = BC_DECIMAL;
off += writeInt32(bytes, off, scale);
bytes[off] = BC_DECIMAL;
off = writeInt32(bytes, off + 1, scale);

if (isInt32(unscaledValue)) {
off += writeInt32(bytes, off, unscaledValue.intValue());
off = writeInt32(bytes, off, unscaledValue.intValue());
} else if (isInt64(unscaledValue)) {
off = writeInt64(bytes, off, unscaledValue.longValue());
} else {
Expand Down

0 comments on commit 0d984da

Please sign in to comment.