diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java index d3e0c3abe9..3f49be60bf 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java @@ -1651,7 +1651,7 @@ public final void writeName5Raw(long name) { @Override public final void writeName6Raw(long name) { int off = this.off; - int minCapacity = off + 10 + indent; + int minCapacity = off + 11 + indent; if (minCapacity >= this.chars.length) { ensureCapacity(minCapacity); } @@ -1674,7 +1674,7 @@ public final void writeName6Raw(long name) { @Override public final void writeName7Raw(long name) { int off = this.off; - int minCapacity = off + 10 + indent; + int minCapacity = off + 12 + indent; if (minCapacity >= this.chars.length) { ensureCapacity(minCapacity); } @@ -1698,7 +1698,7 @@ public final void writeName7Raw(long name) { @Override public final void writeName8Raw(long name) { int off = this.off; - int minCapacity = off + 10 + indent; + int minCapacity = off + 13 + indent; if (minCapacity >= this.chars.length) { ensureCapacity(minCapacity); } diff --git a/core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF16Test.java b/core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF16Test.java index e16ebf29bb..9e0b0dc7f0 100644 --- a/core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF16Test.java +++ b/core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF16Test.java @@ -576,4 +576,68 @@ public void testLHex256BigEndian() { ); assertEquals("abcd", new String(buf)); } + + @Test + public void writeName8() { + JSONWriterUTF16 jsonWriter = new JSONWriterUTF16(JSONFactory.createWriteContext()); + + char[] name = "a123".toCharArray(); + long nameValue = UNSAFE.getLong(name, ARRAY_CHAR_BASE_OFFSET); + + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName6Raw(nameValue); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName7Raw(nameValue); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName8Raw(nameValue); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName9Raw(nameValue, 1); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName10Raw(nameValue, 1); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName11Raw(nameValue, 1); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName12Raw(nameValue, 1); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName13Raw(nameValue, 1); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName14Raw(nameValue, 1); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName15Raw(nameValue, 1); + } + { + jsonWriter.chars = new char[0]; + jsonWriter.off = 0; + jsonWriter.writeName16Raw(nameValue, 1); + } + } } diff --git a/core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF8Test.java b/core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF8Test.java index 943c4135f7..8c37f02a40 100644 --- a/core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF8Test.java +++ b/core/src/test/java/com/alibaba/fastjson2/JSONWriterUTF8Test.java @@ -15,6 +15,8 @@ import static com.alibaba.fastjson2.JSONWriter.Feature.NullAsDefaultValue; import static com.alibaba.fastjson2.JSONWriter.Feature.PrettyFormat; +import static com.alibaba.fastjson2.util.JDKUtils.ARRAY_CHAR_BASE_OFFSET; +import static com.alibaba.fastjson2.util.JDKUtils.UNSAFE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -669,4 +671,64 @@ public void writeStringLatin1Pretty() { Object parse = JSON.parse(json); assertEquals(str, parse); } + + @Test + public void writeName8() { + JSONWriterUTF8 jsonWriter = new JSONWriterUTF8(JSONFactory.createWriteContext()); + + char[] name = "a123".toCharArray(); + long nameValue = UNSAFE.getLong(name, ARRAY_CHAR_BASE_OFFSET); + + { + jsonWriter.bytes = new byte[0]; + jsonWriter.writeName6Raw(nameValue); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.writeName7Raw(nameValue); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.writeName8Raw(nameValue); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.writeName9Raw(nameValue, 1); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.off = 0; + jsonWriter.writeName10Raw(nameValue, 1); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.off = 0; + jsonWriter.writeName11Raw(nameValue, 1); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.off = 0; + jsonWriter.writeName12Raw(nameValue, 1); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.off = 0; + jsonWriter.writeName13Raw(nameValue, 1); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.off = 0; + jsonWriter.writeName14Raw(nameValue, 1); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.off = 0; + jsonWriter.writeName15Raw(nameValue, 1); + } + { + jsonWriter.bytes = new byte[0]; + jsonWriter.off = 0; + jsonWriter.writeName16Raw(nameValue, 1); + } + } }