From a73960f462cb7f2a49c46145e5ec56739c5d8faf Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 14 Jan 2025 01:17:52 +0800 Subject: [PATCH] use IOUtils --- .../java/com/alibaba/fastjson2/JSONBDump.java | 7 +- .../alibaba/fastjson2/JSONReaderJSONB.java | 178 ++++++++---------- .../alibaba/fastjson2/JSONReaderUTF16.java | 7 +- .../alibaba/fastjson2/JSONWriterJSONB.java | 134 ++++--------- .../com/alibaba/fastjson2/util/IOUtils.java | 47 +++-- 5 files changed, 140 insertions(+), 233 deletions(-) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONBDump.java b/core/src/main/java/com/alibaba/fastjson2/JSONBDump.java index 246e291e3..dc55003b8 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONBDump.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONBDump.java @@ -1,5 +1,6 @@ package com.alibaba.fastjson2; +import com.alibaba.fastjson2.util.IOUtils; import com.alibaba.fastjson2.util.JDKUtils; import java.math.BigDecimal; @@ -279,11 +280,9 @@ private void dumpAny() { ); break; case BC_INT64: - long unscaledValueLong = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); unscaledValue = BigInteger.valueOf( - BIG_ENDIAN - ? unscaledValueLong - : Long.reverseBytes(unscaledValueLong)); + IOUtils.getLongBE(bytes, offset) + ); offset += 8; break; default: diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java index 9bc41337b..c85aa3bf6 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java @@ -376,9 +376,8 @@ public Map readObject() { } else if (valueType == BC_OBJECT) { value = readObject(); } else if (valueType == BC_INT64) { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset + 1); + value= IOUtils.getLongBE(bytes, offset + 1); offset += 9; - value = BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value); } else if (valueType >= BC_ARRAY_FIX_MIN && valueType <= BC_ARRAY) { offset++; int len; @@ -562,9 +561,9 @@ public Object readAny() { return (long) int32Value; } case BC_INT64: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value); + return int64Value; } case BC_BIGINT: { int len = readInt32Value(); @@ -582,9 +581,9 @@ public Object readAny() { return (float) readInt32Value(); } case BC_DOUBLE: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return Double.longBitsToDouble(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return Double.longBitsToDouble(int64Value); } case BC_DOUBLE_LONG: { return (double) readInt64Value(); @@ -728,9 +727,9 @@ public Object readAny() { return Instant.ofEpochSecond(epochSeconds, nano); } case BC_TIMESTAMP_MILLIS: { - long millis = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long millis = IOUtils.getLongBE(bytes, offset); offset += 8; - return new Date(BIG_ENDIAN ? millis : Long.reverseBytes(millis)); + return new Date(millis); } case BC_BIGINT_LONG: { return BigInteger.valueOf( @@ -1075,9 +1074,8 @@ public List readArray() { value = readObject(); } else if (valueType == BC_INT64) { offset++; - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + value = IOUtils.getLongBE(bytes, offset); offset += 8; - value = BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value); } else if (valueType >= BC_ARRAY_FIX_MIN && valueType <= BC_ARRAY) { offset++; int len = valueType == BC_ARRAY @@ -1136,9 +1134,8 @@ public List readArray() { offset += 3; value = int3; } else if (valueType == BC_INT32) { - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset + 1); + value = IOUtils.getIntBE(bytes, offset + 1); offset += 5; - value = BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value); } else if (valueType == BC_REFERENCE) { String reference = readReference(); if ("..".equals(reference)) { @@ -3223,49 +3220,52 @@ private String readStringTypeNotMatch() { return "1.0"; case BC_INT64_INT: case BC_INT32: { - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + int int32Value = IOUtils.getIntBE(bytes, offset); offset += 4; - return Long.toString(BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value)); + return Long.toString(int32Value); } case BC_FLOAT_INT: return Float.toString( readInt32Value()); case BC_FLOAT: { - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + int int32Value = IOUtils.getIntBE(bytes, offset); offset += 4; - float floatValue = Float.intBitsToFloat(BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value)); - return Float.toString(floatValue); + return Float.toString( + Float.intBitsToFloat(int32Value) + ); } case BC_DOUBLE: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - double doubleValue = Double.longBitsToDouble(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); - return Double.toString(doubleValue); + return Double.toString( + Double.longBitsToDouble(int64Value) + ); } case BC_TIMESTAMP_SECONDS: { - int seconds = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long millis = IOUtils.getIntBE(bytes, offset) * 1000L; offset += 4; - long millis = (BIG_ENDIAN ? seconds : Integer.reverseBytes(seconds)) * 1000L; - Date date = new Date(millis); - return DateUtils.toString(date); + return DateUtils.toString( + new Date(millis) + ); } case BC_TIMESTAMP_MINUTES: { - int minutes = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long millis = IOUtils.getIntBE(bytes, offset) * 60000L; offset += 4; - long millis = (BIG_ENDIAN ? minutes : Integer.reverseBytes(minutes)) * 60000L; - Date date = new Date(millis); - return DateUtils.toString(date); + return DateUtils.toString( + new Date(millis) + ); } case BC_TIMESTAMP_MILLIS: { - long millis = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long millis = IOUtils.getLongBE(bytes, offset); offset += 8; - Date date = new Date(BIG_ENDIAN ? millis : Long.reverseBytes(millis)); - return DateUtils.toString(date); + return DateUtils.toString( + new Date(millis) + ); } case BC_INT64: - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return Long.toString(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return Long.toString(int64Value); case BC_BIGINT: { int len = readInt32Value(); byte[] bytes = new byte[len]; @@ -3416,14 +3416,10 @@ public long readInt64Value() { + (bytes[offset + 1] & 0xFF); offset += 2; } else if (type == BC_INT64_INT) { - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); - int64Value = BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value); + int64Value = IOUtils.getIntBE(bytes, offset); offset += 4; } else if (type == BC_INT64) { - int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); - if (!BIG_ENDIAN) { - int64Value = Long.reverseBytes(int64Value); - } + int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; } else { this.offset = offset; @@ -3471,14 +3467,14 @@ private long readInt64Value0(byte[] bytes, byte type) { offset += 2; return int16Value; case BC_INT32: { - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + int int32Value = IOUtils.getIntBE(bytes, offset); offset += 4; - return BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value); + return int32Value; } case BC_FLOAT: { - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + int int32Value = IOUtils.getIntBE(bytes, offset); offset += 4; - float floatValue = Float.intBitsToFloat(BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value)); + float floatValue = Float.intBitsToFloat(int32Value); return (long) floatValue; } case BC_DOUBLE: { @@ -3500,9 +3496,9 @@ private long readInt64Value0(byte[] bytes, byte type) { return seconds * 1000; } case BC_TIMESTAMP_MILLIS: - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value); + return int64Value; case BC_DECIMAL: { int scale = readInt32Value(); BigInteger unscaledValue = readBigInteger(); @@ -3577,10 +3573,7 @@ public int readInt32Value() { + (bytes[offset + 1] & 0xFF); offset += 2; } else if (type == BC_INT32) { - type = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); - if (!BIG_ENDIAN) { - type = Integer.reverseBytes(type); - } + type = IOUtils.getIntBE(bytes, offset); offset += 4; } else { this.offset = offset; @@ -3632,9 +3625,9 @@ private int readInt32Value0(byte[] bytes, byte type) { return (int) readInt64Value(); } case BC_INT64: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return (int) (BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return (int) int64Value; } case BC_FLOAT_INT: return (int) (float) readInt32Value(); @@ -3754,10 +3747,7 @@ public Integer readInt32() { + (bytes[offset + 1] & 0xFF); offset += 2; } else if (type == BC_INT32) { - int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); - if (!BIG_ENDIAN) { - int32Value = Integer.reverseBytes(int32Value); - } + int32Value = IOUtils.getIntBE(bytes, offset); offset += 4; } else { this.offset = offset; @@ -3791,14 +3781,10 @@ public Long readInt64() { + (bytes[offset + 1] & 0xFF); offset += 2; } else if (type == BC_INT64_INT) { - int int32Val = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); - int64Value = BIG_ENDIAN ? int32Val : Integer.reverseBytes(int32Val); + int64Value = IOUtils.getIntBE(bytes, offset); offset += 4; } else if (type == BC_INT64) { - int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); - if (!BIG_ENDIAN) { - int64Value = Long.reverseBytes(int64Value); - } + int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; } else { this.offset = offset; @@ -3851,9 +3837,9 @@ public float readFloatValue() { byte[] bytes = this.bytes; int offset = this.offset; if (bytes[offset] == BC_FLOAT) { - int int32Val = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset + 1); + int int32Val = IOUtils.getIntBE(bytes, offset + 1); this.offset = offset + 5; - return Float.intBitsToFloat(BIG_ENDIAN ? int32Val : Integer.reverseBytes(int32Val)); + return Float.intBitsToFloat(int32Val); } return readFloat0(); @@ -3878,9 +3864,9 @@ private float readFloat0() { offset += 2; return int16Value; case BC_INT64: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return (float) (BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return (float) int64Value; } case BC_INT64_INT: case BC_INT32: { @@ -3889,9 +3875,9 @@ private float readFloat0() { return int32Value; } case BC_DOUBLE: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return (float) Double.longBitsToDouble(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return (float) Double.longBitsToDouble(int64Value); } case BC_FLOAT_INT: { return (float) readInt32Value(); @@ -4033,20 +4019,20 @@ private double readDoubleValue0() { offset += 2; return int16Value; case BC_INT64: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return (double) (BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return (double) int64Value; } case BC_INT64_INT: case BC_INT32: { - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + int int32Value = IOUtils.getIntBE(bytes, offset); offset += 4; - return BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value); + return int32Value; } case BC_FLOAT: - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + int int32Value = IOUtils.getIntBE(bytes, offset); offset += 4; - return Float.intBitsToFloat(BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value)); + return Float.intBitsToFloat(int32Value); case BC_FLOAT_INT: { return (float) readInt32Value(); } @@ -4214,9 +4200,9 @@ public Number readNumber() { return (long) int32Value; } case BC_INT64: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value); + return int64Value; } case BC_BIGINT: { int len = readInt32Value(); @@ -4239,9 +4225,9 @@ public Number readNumber() { return (float) readInt32Value(); } case BC_DOUBLE: - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return Double.longBitsToDouble(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return Double.longBitsToDouble(int64Value); case BC_DOUBLE_LONG: return (double) readInt64Value(); case BC_DECIMAL: { @@ -4301,12 +4287,9 @@ public BigDecimal readBigDecimal() { decimal = BigDecimal.valueOf(getInt(bytes, offset + 1), scale); offset += 5; } else if (bytes[offset] == BC_INT64) { - long unscaledValue = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset + 1); decimal = BigDecimal.valueOf( - BIG_ENDIAN - ? unscaledValue - : Long.reverseBytes(unscaledValue), scale - ); + IOUtils.getLongBE(bytes, offset + 1), + scale); offset += 9; } else { BigInteger unscaledValue = readBigInteger(); @@ -4359,16 +4342,16 @@ private BigDecimal readDecimal0(byte type) { return BigDecimal.valueOf((long) floatValue); } case BC_DOUBLE: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - double doubleValue = Double.longBitsToDouble(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + double doubleValue = Double.longBitsToDouble(int64Value); return BigDecimal.valueOf( (long) doubleValue); } case BC_INT64: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return BigDecimal.valueOf(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return BigDecimal.valueOf(int64Value); } case BC_BIGINT: { BigInteger bigInt = readBigInteger(); @@ -4500,16 +4483,16 @@ private BigInteger readBigInteger0(byte type) { (long) floatValue); } case BC_DOUBLE: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - double doubleValue = Double.longBitsToDouble(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + double doubleValue = Double.longBitsToDouble(int64Value); return BigInteger.valueOf( (long) doubleValue); } case BC_INT64: { - long int64Value = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long int64Value = IOUtils.getLongBE(bytes, offset); offset += 8; - return BigInteger.valueOf(BIG_ENDIAN ? int64Value : Long.reverseBytes(int64Value)); + return BigInteger.valueOf(int64Value); } case BC_BINARY: { int len = readInt32Value(); @@ -4936,9 +4919,9 @@ public Instant readInstant() { } case BC_INT64: case BC_TIMESTAMP_MILLIS: { - long millis = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long millis = IOUtils.getLongBE(bytes, offset); offset += 8; - return Instant.ofEpochMilli(BIG_ENDIAN ? millis : Long.reverseBytes(millis)); + return Instant.ofEpochMilli(millis); } default: break; @@ -5036,9 +5019,9 @@ private ZonedDateTime readZonedDateTime0(int type) { } case BC_INT64: case BC_TIMESTAMP_MILLIS: { - long millis = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset); + long millis = IOUtils.getLongBE(bytes, offset); offset += 8; - Instant instant = Instant.ofEpochMilli(BIG_ENDIAN ? millis : Long.reverseBytes(millis)); + Instant instant = Instant.ofEpochMilli(millis); return ZonedDateTime.ofInstant(instant, DEFAULT_ZONE_ID); } default: @@ -5327,9 +5310,9 @@ public Date readDate() { null); break; case BC_TIMESTAMP_MILLIS: { - long millis = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset + 1); + long millis = IOUtils.getLongBE(bytes, offset + 1); this.offset += 9; - return new Date(BIG_ENDIAN ? millis : Long.reverseBytes(millis)); + return new Date(millis); } case BC_TIMESTAMP_MINUTES: { long minutes = getInt(bytes, offset + 1); @@ -6303,7 +6286,6 @@ public boolean nextIfName4Match43(long name1, long name2, long name3, long name4 } static int getInt(byte[] bytes, int offset) { - int int32Value = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset); - return BIG_ENDIAN ? int32Value : Integer.reverseBytes(int32Value); + return IOUtils.getIntBE(bytes, offset); } } diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java index cc2b97440..9c81a0060 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java @@ -5162,7 +5162,7 @@ public final int getRawInt() { } static int getInt(char[] chars, int offset) { - long int64Val = UNSAFE.getLong(chars, ARRAY_CHAR_BASE_OFFSET + (offset << 1)); + long int64Val = IOUtils.getLongUnaligned(chars, offset); if ((int64Val & CHAR_MASK) != 0) { return 0; @@ -5187,9 +5187,8 @@ public final long getRawLong() { } static long getLong(char[] chars, int offset) { - long arrayOffset = ARRAY_CHAR_BASE_OFFSET + (offset << 1); - long int64Val0 = UNSAFE.getLong(chars, arrayOffset); - long int64Val1 = UNSAFE.getLong(chars, arrayOffset + 8); + long int64Val0 = IOUtils.getLongUnaligned(chars, offset); + long int64Val1 = IOUtils.getLongUnaligned(chars, offset + 4); if (((int64Val0 | int64Val1) & CHAR_MASK) != 0) { return 0; diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java index 7045af4b3..e46127a68 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java @@ -559,10 +559,10 @@ private static int putStringSizeLarge(byte[] bytes, int off, int strlen) { bytes[off] = BC_STR_ASCII; bytes[off + 1] = BC_INT32; - UNSAFE.putInt( + IOUtils.putIntBE( bytes, - ARRAY_BYTE_BASE_OFFSET + off + 2, - BIG_ENDIAN ? strlen : Integer.reverseBytes(strlen) + off + 2, + strlen ); return 6; } @@ -1170,10 +1170,10 @@ public void writeMillis(long millis) { int secondsInt = (int) seconds; bytes[off] = BC_TIMESTAMP_SECONDS; - UNSAFE.putInt( + IOUtils.putIntBE( bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? secondsInt : Integer.reverseBytes(secondsInt) + off + 1, + secondsInt ); this.off = off + 5; return; @@ -1184,10 +1184,10 @@ public void writeMillis(long millis) { if (minutes >= Integer.MIN_VALUE && minutes <= Integer.MAX_VALUE) { int minutesInt = (int) minutes; bytes[off] = BC_TIMESTAMP_MINUTES; - UNSAFE.putInt( + IOUtils.putIntBE( bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? minutesInt : Integer.reverseBytes(minutesInt) + off + 1, + minutesInt ); this.off = off + 5; return; @@ -1196,10 +1196,10 @@ public void writeMillis(long millis) { } bytes[off] = BC_TIMESTAMP_MILLIS; - UNSAFE.putLong( + IOUtils.putLongBE( bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? millis : Long.reverseBytes(millis) + off + 1, + millis ); this.off = off + 9; } @@ -1236,10 +1236,10 @@ public void writeInt64(Long i) { size = 3; } else if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { bytes[off] = BC_INT64_INT; - UNSAFE.putInt( + IOUtils.putIntBE( bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? (int) val : Integer.reverseBytes((int) val) + off + 1, + (int) val ); size = 5; } else { @@ -1273,19 +1273,11 @@ public void writeInt64(long val) { size = 3; } else if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { bytes[off] = BC_INT64_INT; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? (int) val : Integer.reverseBytes((int) val) - ); + IOUtils.putIntBE(bytes, off + 1, (int) val); size = 5; } else { bytes[off] = BC_INT64; - UNSAFE.putLong( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Long.reverseBytes(val) - ); + IOUtils.putLongBE(bytes, off + 1, val); size = 9; } this.off = off + size; @@ -1337,21 +1329,13 @@ public void writeInt64(long[] value) { if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { bytes[off] = BC_INT64_INT; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? (int) val : Integer.reverseBytes((int) val) - ); + IOUtils.putIntBE(bytes, off + 1, (int) val); off += 5; continue; } bytes[off] = BC_INT64; - UNSAFE.putLong( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Long.reverseBytes(val) - ); + IOUtils.putLongBE(bytes, off + 1, val); off += 9; } this.off = off; @@ -1415,11 +1399,7 @@ public void writeListInt64(List values) { private static int writeInt64Large(byte[] bytes, int off, long val) { if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { bytes[off] = BC_INT64_INT; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? (int) val : Integer.reverseBytes((int) val) - ); + IOUtils.putIntBE(bytes, off + 1, (int) val); return 5; } @@ -1428,11 +1408,7 @@ private static int writeInt64Large(byte[] bytes, int off, long val) { private static int writeInt64Large8(byte[] bytes, int off, long val) { bytes[off] = BC_INT64; - UNSAFE.putLong( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Long.reverseBytes(val) - ); + IOUtils.putLongBE(bytes, off + 1, val); return 9; } @@ -1452,11 +1428,7 @@ public void writeFloat(float value) { } else { bytes[off] = BC_FLOAT; i = Float.floatToIntBits(value); - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? i : Integer.reverseBytes(i) - ); + IOUtils.putIntBE(bytes, off + 1, i); off += 5; } this.off = off; @@ -1506,11 +1478,7 @@ public void writeDouble(double value) { } bytes[off] = BC_DOUBLE; long i = Double.doubleToLongBits(value); - UNSAFE.putLong( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? i : Long.reverseBytes(i) - ); + IOUtils.putLongBE(bytes, off + 1, i); this.off = off + 9; } @@ -1585,11 +1553,7 @@ public void writeInt32(int[] values) { } bytes[off] = BC_INT32; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Integer.reverseBytes(val) - ); + IOUtils.putIntBE(bytes, off + 1, val); off += 5; } this.off = off; @@ -1707,11 +1671,7 @@ public void writeInt32(Integer i) { size = 3; } else { bytes[off] = BC_INT32; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Integer.reverseBytes(val) - ); + IOUtils.putIntBE(bytes, off + 1, val); size = 5; } } @@ -1741,11 +1701,7 @@ public void writeInt32(int val) { size = 3; } else { bytes[off] = BC_INT32; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Integer.reverseBytes(val) - ); + IOUtils.putIntBE(bytes, off + 1, val); size = 5; } this.off += size; @@ -1794,11 +1750,7 @@ public void writeListInt32(List values) { off += 3; } else { bytes[off] = BC_INT32; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Integer.reverseBytes(val) - ); + IOUtils.putIntBE(bytes, off + 1, val); off += 5; } } @@ -1820,11 +1772,7 @@ public static int writeInt32(byte[] bytes, int off, int val) { return 3; } else { bytes[off] = BC_INT32; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Integer.reverseBytes(val) - ); + IOUtils.putIntBE(bytes, off + 1, val); return 5; } } @@ -1844,19 +1792,11 @@ public static int writeInt64(byte[] bytes, int off, long val) { return 3; } else if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { bytes[off] = BC_INT64_INT; - UNSAFE.putInt( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? (int) val : Integer.reverseBytes((int) val) - ); + IOUtils.putIntBE(bytes, off + 1, (int) val); return 5; } else { bytes[off] = BC_INT64; - UNSAFE.putLong( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 1, - BIG_ENDIAN ? val : Long.reverseBytes(val) - ); + IOUtils.putLongBE(bytes, off + 1, val); return 9; } } @@ -2150,18 +2090,8 @@ public void writeUUID(UUID value) { bytes[off] = BC_BINARY; bytes[off + 1] = BC_INT32_NUM_16; - long msb = value.getMostSignificantBits(); - UNSAFE.putLong( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 2, - BIG_ENDIAN ? msb : Long.reverseBytes(msb) - ); - long lsb = value.getLeastSignificantBits(); - UNSAFE.putLong( - bytes, - ARRAY_BYTE_BASE_OFFSET + off + 10, - BIG_ENDIAN ? lsb : Long.reverseBytes(lsb) - ); + IOUtils.putLongBE(bytes, off + 2, value.getMostSignificantBits()); + IOUtils.putLongBE(bytes, off + 10, value.getLeastSignificantBits()); this.off = off + 18; } diff --git a/core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java index ac73bb595..84a224430 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java @@ -1408,27 +1408,19 @@ public static char getChar(byte[] str, int pos) { } public static void putShortLE(byte[] buf, int pos, short v) { - UNSAFE.putShort( - buf, - ARRAY_BYTE_BASE_OFFSET + pos, - convEndian(false, v) - ); + UNSAFE.putShort(buf, ARRAY_BYTE_BASE_OFFSET + pos, convEndian(false, v)); + } + + public static void putIntBE(byte[] buf, int pos, int v) { + UNSAFE.putInt(buf, ARRAY_BYTE_BASE_OFFSET + pos, convEndian(true, v)); } public static void putIntLE(byte[] buf, int pos, int v) { - UNSAFE.putInt( - buf, - ARRAY_BYTE_BASE_OFFSET + pos, - convEndian(false, v) - ); + UNSAFE.putInt(buf, ARRAY_BYTE_BASE_OFFSET + pos, convEndian(false, v)); } public static void putIntLE(char[] buf, int pos, int v) { - UNSAFE.putInt( - buf, - ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1), - convEndian(false, v) - ); + UNSAFE.putInt(buf, ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1), convEndian(false, v)); } public static void putShortUnaligned(byte[] buf, int pos, short v) { @@ -1444,23 +1436,19 @@ public static void putIntUnaligned(byte[] buf, int pos, int v) { } public static void putLongLE(char[] buf, int pos, long v) { - UNSAFE.putLong( - buf, - ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1), - convEndian(false, v) - ); + UNSAFE.putLong(buf, ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1), convEndian(false, v)); } public static void putLongUnaligned(char[] buf, int pos, long v) { UNSAFE.putLong(buf, ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1), v); } + public static void putLongBE(byte[] buf, int pos, long v) { + UNSAFE.putLong(buf, ARRAY_CHAR_BASE_OFFSET + pos, convEndian(true, v)); + } + public static void putLongLE(byte[] buf, int pos, long v) { - UNSAFE.putLong( - buf, - ARRAY_CHAR_BASE_OFFSET + pos, - BIG_ENDIAN ? Long.reverseBytes(v) : v - ); + UNSAFE.putLong(buf, ARRAY_CHAR_BASE_OFFSET + pos, convEndian(false, v)); } public static int digit4(char[] chars, int off) { @@ -1629,6 +1617,11 @@ public static short getShortE(byte[] bytes, int offset) { UNSAFE.getShort(bytes, ARRAY_BYTE_BASE_OFFSET + offset)); } + public static int getIntBE(byte[] bytes, int offset) { + return convEndian(true, + UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset)); + } + public static int getIntLE(byte[] bytes, int offset) { return convEndian(false, UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset)); @@ -1643,6 +1636,10 @@ public static long getLongBE(byte[] bytes, int offset) { UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset)); } + public static long getLongUnaligned(char[] bytes, int offset) { + return UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + ((long) offset << 1)); + } + public static long getLongLE(byte[] bytes, int offset) { return convEndian(false, UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset));