From 4c4913c5d0031ce3db33d9bedc97e12e2702f3d2 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Fri, 29 May 2020 13:43:47 +0800 Subject: [PATCH] --- .../convert/json/JsonByteBufferReader.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/org/redkale/convert/json/JsonByteBufferReader.java b/src/org/redkale/convert/json/JsonByteBufferReader.java index 9760d7203..698e786f9 100644 --- a/src/org/redkale/convert/json/JsonByteBufferReader.java +++ b/src/org/redkale/convert/json/JsonByteBufferReader.java @@ -69,6 +69,10 @@ public class JsonByteBufferReader extends JsonReader { */ @Override protected final char nextChar() { + return nextChar(null); + } + + protected final char nextChar(StringBuilder sb) { if (currentChar != 0) { char ch = currentChar; this.currentChar = 0; @@ -78,14 +82,18 @@ public class JsonByteBufferReader extends JsonReader { int remain = this.currentBuffer.remaining(); if (remain == 0 && this.currentIndex + 1 >= this.buffers.length) return 0; } - byte b1 = nextByte(); - if (b1 >= 0) {// 1 byte, 7 bits: 0xxxxxxx - return (char) b1; - } else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) { // 2 bytes, 11 bits: 110xxxxx 10xxxxxx - return (char) (((b1 << 6) ^ nextByte()) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80))); - } else if ((b1 >> 4) == -2) { // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx - return (char) ((b1 << 12) ^ (nextByte() << 6) ^ (nextByte() ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80)))); - } else { // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + byte b = nextByte(); + if (b >= 0) {// 1 byte, 7 bits: 0xxxxxxx + return (char) b; + } else if ((b >> 5) == -2 && (b & 0x1e) != 0) { // 2 bytes, 11 bits: 110xxxxx 10xxxxxx + return (char) (((b << 6) ^ nextByte()) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80))); + } else if ((b >> 4) == -2) { // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx + return (char) ((b << 12) ^ (nextByte() << 6) ^ (nextByte() ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80)))); + } else if ((b >> 3) == -2) {// 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + int uc = ((b << 18) ^ (nextByte() << 12) ^ (nextByte() << 6) ^ (nextByte() ^ (((byte) 0xF0 << 18) ^ ((byte) 0x80 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80)))); + if (sb != null) sb.append(Character.highSurrogate(uc)); + return Character.lowSurrogate(uc); + } else { throw new RuntimeException(new UnmappableCharacterException(4)); } } @@ -208,9 +216,9 @@ public class JsonByteBufferReader extends JsonReader { if (ch == '"' || ch == '\'') { final char quote = ch; for (;;) { - ch = nextChar(); + ch = nextChar(sb); if (ch == '\\') { - char c = nextChar(); + char c = nextChar(sb); switch (c) { case '"': case '\'': @@ -249,9 +257,9 @@ public class JsonByteBufferReader extends JsonReader { } else { sb.append(ch); for (;;) { - ch = nextChar(); + ch = nextChar(sb); if (ch == '\\') { - char c = nextChar(); + char c = nextChar(sb); switch (c) { case '"': case '\'':