This commit is contained in:
Redkale
2020-05-29 13:43:47 +08:00
parent adb0dc5963
commit 4c4913c5d0

View File

@@ -69,6 +69,10 @@ public class JsonByteBufferReader extends JsonReader {
*/ */
@Override @Override
protected final char nextChar() { protected final char nextChar() {
return nextChar(null);
}
protected final char nextChar(StringBuilder sb) {
if (currentChar != 0) { if (currentChar != 0) {
char ch = currentChar; char ch = currentChar;
this.currentChar = 0; this.currentChar = 0;
@@ -78,14 +82,18 @@ public class JsonByteBufferReader extends JsonReader {
int remain = this.currentBuffer.remaining(); int remain = this.currentBuffer.remaining();
if (remain == 0 && this.currentIndex + 1 >= this.buffers.length) return 0; if (remain == 0 && this.currentIndex + 1 >= this.buffers.length) return 0;
} }
byte b1 = nextByte(); byte b = nextByte();
if (b1 >= 0) {// 1 byte, 7 bits: 0xxxxxxx if (b >= 0) {// 1 byte, 7 bits: 0xxxxxxx
return (char) b1; return (char) b;
} else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) { // 2 bytes, 11 bits: 110xxxxx 10xxxxxx } else if ((b >> 5) == -2 && (b & 0x1e) != 0) { // 2 bytes, 11 bits: 110xxxxx 10xxxxxx
return (char) (((b1 << 6) ^ nextByte()) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80))); return (char) (((b << 6) ^ nextByte()) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80)));
} else if ((b1 >> 4) == -2) { // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx } else if ((b >> 4) == -2) { // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx
return (char) ((b1 << 12) ^ (nextByte() << 6) ^ (nextByte() ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80)))); return (char) ((b << 12) ^ (nextByte() << 6) ^ (nextByte() ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80))));
} else { // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } 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)); throw new RuntimeException(new UnmappableCharacterException(4));
} }
} }
@@ -208,9 +216,9 @@ public class JsonByteBufferReader extends JsonReader {
if (ch == '"' || ch == '\'') { if (ch == '"' || ch == '\'') {
final char quote = ch; final char quote = ch;
for (;;) { for (;;) {
ch = nextChar(); ch = nextChar(sb);
if (ch == '\\') { if (ch == '\\') {
char c = nextChar(); char c = nextChar(sb);
switch (c) { switch (c) {
case '"': case '"':
case '\'': case '\'':
@@ -249,9 +257,9 @@ public class JsonByteBufferReader extends JsonReader {
} else { } else {
sb.append(ch); sb.append(ch);
for (;;) { for (;;) {
ch = nextChar(); ch = nextChar(sb);
if (ch == '\\') { if (ch == '\\') {
char c = nextChar(); char c = nextChar(sb);
switch (c) { switch (c) {
case '"': case '"':
case '\'': case '\'':